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



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

def self.find_popular(options = {})
  options.reverse_merge! :limit => 5, :since => 10.days.ago

  self.includes(:post => :user)
    .where("polls.created_at > ?", options[:since])
    .order("polls.votes_count desc")
    .limit(options[:limit])
end


33
34
35
36
# File 'app/models/poll.rb', line 33

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

.find_recent(options = {}) ⇒ Object



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

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

Instance Method Details

#add_choices(choices) ⇒ Object



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

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

#voted?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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