Created
April 19, 2014 16:51
-
-
Save rom3r4/11090091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
Bundler.require(:default) | |
require File.dirname(__FILE__) + "/main.rb" | |
map '/' do | |
run CORE::Main | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'rack' | |
gem 'rack-flash' | |
gem 'thin' | |
gem 'sinatra', :require => 'sinatra/base' | |
gem 'mongoid' | |
gem 'bson_ext' | |
gem 'slim' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CORE | |
class Task | |
include Mongoid::Document | |
field :title, type: String | |
field :body, type: String | |
end | |
class Main < Sinatra::Base | |
use Rack::Flash | |
configure :development do | |
enable :sessions, :logging, :dump_errors, :inline_templates | |
enable :methodoverride | |
set :root, File.dirname(__FILE__) | |
logger = Logger.new($stdout) | |
Mongoid.configure do |config| | |
name = "demoodb" | |
host = "localhost" | |
config.master = Mongo::Connection.new.db(name) | |
#config.logger = Logger.new($stdout, :warn) | |
config.logger = logger | |
config.persist_in_safe_mode = false | |
end | |
end | |
before do | |
@t = Task.all | |
if @t.empty? | |
flash[:notice] = "No documents found!" | |
end | |
end | |
get '/' do | |
slim :index | |
end | |
get '/new' do | |
t = Task.new | |
slim :new | |
end | |
post '/new' do | |
t = Task.new(params[:task]) | |
if t.save | |
redirect '/' | |
else | |
"Error saving doc" | |
end | |
end | |
get '/show/:id' do |id| | |
@t = Task.find(id) | |
slim :show | |
end | |
get '/edit/:id' do |id| | |
@t = Task.find(id) | |
slim :edit | |
end | |
put '/update/:id' do | |
@t = Task.find(params[:id]) | |
@t.update_attributes(params[:task]) | |
slim :show | |
end | |
delete '/delete/:id' do | |
t = Task.find(params[:id]) | |
if t.delete | |
redirect '/' | |
else | |
flash[:notify] = "Fudeu-se" | |
end | |
end | |
end | |
end | |
__END__ | |
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
@@ layout | |
doctype html | |
html | |
head | |
title Hell yea | |
body | |
h1 Sinatra + MongoDB through Mongoid | |
== yield | |
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
@@ index | |
h2 Home | |
p | |
a href='/new' new task | |
- if flash[:notice] | |
= flash[:notice] | |
- else | |
p Listing Tasks | |
table | |
tr | |
td id | |
td title | |
td body | |
td action | |
- @t.each do |t| | |
tr | |
td #{t.id} | |
td #{t.title} | |
td #{t.body} | |
td | |
a href="/show/#{t.id}" show | |
' | |
a href="/edit/#{t.id}" edit | |
p | |
a href="/" home | |
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
@@ new | |
h2 New task | |
form action="/new" method="POST" | |
input type="hidden" name="_method" value="POST" | |
|title | |
input type="text" name="task[title]" | |
|body | |
input type="text" name="task[body]" | |
input type="submit" name="submit" value="Save" | |
p | |
a href="/" home | |
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
@@ show | |
h2 Show | |
p id: #{@t.id} | |
p Title: #{@t.title} | |
p Body: #{@t.body} | |
form action="/delete/#{@t.id}" method="POST" | |
input type="hidden" name="_method" value="DELETE" | |
input type="submit" value="Delete" | |
a href="/edit/#{@t.id}" edit | |
p | |
a href="/" home | |
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
@@ edit | |
h2 Edit | |
form action="/update/#{@t.id}" method="POST" | |
input type="hidden" name="_method" value="PUT" | |
|title | |
input type="text" name="task[title]" value="#{@t.title}" | |
|body | |
input type="text" name="task[body]" value="#{@t.body}" | |
input type="submit" name="submit" value="Update" | |
p | |
a href="/" home |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment