Sha256: 32392fc1bd797c2c25067b273742ad50b5e327965ac4b2624464543f8024023b
Contents?: true
Size: 1.96 KB
Versions: 1
Compression:
Stored size: 1.96 KB
Contents
module FlexibleFeeds class Event < ActiveRecord::Base belongs_to :ancestor, polymorphic: true belongs_to :eventable, polymorphic: true belongs_to :parent, polymorphic: true belongs_to :creator, polymorphic: true has_many :event_joins, dependent: :destroy has_many :feeds, through: :event_joins has_many :votes validates :children_count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates :controversy, presence: true validates :eventable, presence: true validates :votes_sum, presence: true validates :popularity, presence: true scope :newest, -> { order("updated_at DESC") } scope :oldest, -> { order("updated_at ASC") } scope :loudest, -> { order("children_count DESC") } scope :quietest, -> { order("children_count ASC") } scope :simple_popular, -> { order("votes_sum DESC") } scope :simple_unpopular, -> { order("votes_sum ASC") } scope :popular, -> { order("popularity DESC") } scope :unpopular, -> { order("popularity ASC") } scope :controversial, -> { order("controversy DESC") } scope :uncontroversial, -> { order("controversy ASC") } def cast_vote(params) Vote.cast_vote(params.merge({event: self})) end def calculate_stats self.votes_for = votes.where(value: 1).count self.votes_against = votes.where(value: -1).count self.votes_sum = votes_for - votes_against self.controversy = calculate_controversy(votes_for, votes_against) self.popularity = calculate_popularity(votes_for, votes_for + votes_against) save end private def calculate_controversy(pos, neg) return 0 if pos == 0 || neg == 0 100.0 * (pos > neg ? neg.to_f / pos.to_f : pos.to_f / neg.to_f) end # Thanks to Evan Miller # http://www.evanmiller.org/how-not-to-sort-by-average-rating.html def calculate_popularity(pos, n) PopularityCalculator.new(pos, n).get_popularity end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
flexible_feeds-0.2.1 | app/models/flexible_feeds/event.rb |