Class: Poll

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/poll.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



25
26
27
28
29
30
31
32
33
# File 'app/models/poll.rb', line 25

def self.find_popular(options = {})
  options.reverse_merge! :limit => 5, :since => 10.days.ago
  
  find(:all, :order => "polls.votes_count desc", 
    :limit => options[:limit], 
    :include => [:post => :user],
    :conditions => ["polls.created_at > ?", options[:since]]
  )
end


35
36
37
38
# File 'app/models/poll.rb', line 35

def self.find_popular_in_category(category, options = {})
  options.reverse_merge! :limit => 5
  self.includes(:post).order('polls.votes_count desc').limit(options[:limit]).where('posts.category_id = ?', category.id)
end

.find_recent(options = {}) ⇒ Object



20
21
22
23
# File 'app/models/poll.rb', line 20

def self.find_recent(options = {})
  options.reverse_merge! :limit => 5
  find(:all, :order => "polls.created_at desc", :limit => options[:limit], :include => [:post => :user])
end

Instance Method Details

#add_choices(choices) ⇒ Object



13
14
15
16
17
18
# File 'app/models/poll.rb', line 13

def add_choices(choices)
  choices.each do |description|
    choice = self.choices.new(:description => description)
    choice.save
  end
end

#voted?(user) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/models/poll.rb', line 9

def voted?(user)
  !self.votes.find_by_user_id(user.id).nil?
end