Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Add random selection
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiggs committed Feb 11, 2012
1 parent be26241 commit d49e48a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cataloguais.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit d49e48a

Please sign in to comment.