Permalink
Browse files

Add random selection

  • Loading branch information...
1 parent be26241 commit d49e48a1637dea294321cdedf0717e7db1d94dbc @GordonDiggs committed Feb 11, 2012
Showing with 12 additions and 0 deletions.
  1. +7 −0 cataloguais.rb
  2. +5 −0 models/item.rb
View
@@ -83,6 +83,13 @@
end
end
+get '/random' do
+ @items = [Item.get_random]
+ @sort = settings.sort_order
+ @direction = :asc
+ haml :index
+end
+
post '/new' do
item = Item.create(params[:item])
{ :status => 'success', :message => 'Item successfully added.', :item_markup => item_table_row(item) }.to_json
View
@@ -21,6 +21,11 @@ def self.fields
self.properties.collect{ |p| p.name }[1..-1]
end
+ def self.get_random
+ id = Item.all(:fields => [:id]).to_a.map{|i| i.id}.shuffle[0]
+ Item.first(:id => id)
+ end
+
def self.search_and_sort(sort, direction = :asc, search = '')
sort = sort.collect { |s| s.to_sym.send(direction) }
Item.all(:order => sort).select { |item| item.to_s.downcase.include? search.to_s.downcase }

0 comments on commit d49e48a

Please sign in to comment.