Skip to content

Instantly share code, notes, and snippets.

@techarch
Created May 13, 2010 13:24
Show Gist options
  • Save techarch/399821 to your computer and use it in GitHub Desktop.
Save techarch/399821 to your computer and use it in GitHub Desktop.
gem 'camping' , '>= 2.0'
gem 'mongo', '= 1.0' #Using 1.0 since MongoMapper explicitly
%w(rack camping mongo mongo_mapper).each { | r | require r}
Camping.goes :MongoTest
module MongoTest
def self.create
MongoMapper.connection = Mongo::Connection.new
MongoMapper.database = 'test'
end
module Models
class Widget
include MongoMapper::Document
key :code, String
key :description, String
key :price, Float
validates_numericality_of :price
validates_true_for :price,
:logic=> lambda { | x | (0.05..9999.99) === x.price },
:message=>"must be in the following range: 0.05 to 9999.99"
set_collection_name 'widgets'
end
end
module Controllers
class Index
def get
@widgets = Widget.all
render :index
end
end
class AddWidget < R "/widget/new"
def get
@widget = Widget.new(:code=>'', :description=>'', :price=>0)
render :widget_form
end
def post
return(redirect('/index')) unless input.widget_code && input.widget_description && input.widget_price
@widget = Widget.create(:code => input.widget_code,
:description => input.widget_description,
:price => input.widget_price)
begin
@widget.save!
@widgets = Widget.all
return(render(:index))
rescue Exception => ex
@errors = @widget.errors || [ ex.to_s ]
return(render(:widget_form))
end
end
end
class SearchWidgetByCode < R '/widget/search'
def get
render :search_form
end
def post
return(redirect('/index')) unless input.widget_code
w = Widget.find_by_code input.widget_code
@widgets = [ ]
@widgets << w if w
render :index
end
end
end
module Views
def layout
html do
head do
style :type => 'text/css' do
<<-STYLE
body {
padding:0 0 0 0;
margin:5px;
font-family:'Lucida Grande','Lucida Sans Unicode',sans-serif;
font-size: 0.8em;
color:#303030;
background-color: #fbf9b5;
}
a {
color:#303030;
text-decoration:none;
border-bottom:1px dotted #505050;
}
a:hover {
color:#303030;
background: yellow;
text-decoration:none;
border-bottom:4px solid orange;
}
h1 {
font-size: 14px;
color: #cc3300;
}
table {
font-size:0.9em;
width: 400px;
}
tr
{
background:lightgoldenRodYellow;
vertical-align:top;
}
th
{
font-size: 0.9em;
font-weight:bold;
background:lightBlue none repeat scroll 0 0;
text-align:left;
}
.url
{
width: 300px;
}
STYLE
end
end
body do
self << yield
a "Home", :href=>"/"
div.footer! do
hr
span.copyright_notice! { "Copyright &copy; 2010 &nbsp; - #{ a('Philippe Monnet (@techarch)', :href => 'http://blog.monnet-usa.com/') } " }
end
end
end
end
def index
h1 "Widgets"
if @widgets
table do
tr { th 'id'; th 'Code'; th 'Description'; th 'Price'; }
@widgets.each do | w |
tr { td w._id;
td w.code;
td w.description;
td w.price; }
end
end
else
p "No widgets yets"
end
ul do
li { a "Search", :href => "/widget/search" }
li { a "Create", :href => "/widget/new" }
end
end
def widget_form
h2 "New Widget"
if @errors
h3 "Errors:"
ol do
@errors.each do | e |
li { div "#{e[0]}: #{e[1].join(' ')}" }
end
end
end
form :action => R(AddWidget), :method => 'post' do
label 'Code:', :for => 'widget_code'; br
input :name => 'widget_code', :type => 'text', :value => @widget.code; br
label 'Description:', :for => 'widget_description'; br
input :name => 'widget_description', :type => 'text', :value => @widget.description; br
label 'Price:', :for => 'widget_price'; br
input :name => 'widget_price', :type => 'text', :value => @widget.price.to_s; br
input :type => 'submit', :value => 'Save'
end
end
def search_form
h2 "Search"
p @info if @info
form :action => R(SearchWidgetByCode), :method => 'post' do
label 'Code', :for => 'widget_code'; br
input :name => 'widget_code', :type => 'text'; br
input :type => 'submit', :value => 'Search'
end
end
end
end
MongoTest.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment