app/models/flexible_feeds/event.rb in flexible_feeds-0.2.1 vs app/models/flexible_feeds/event.rb in flexible_feeds-0.3.0

- old
+ new

@@ -1,12 +1,14 @@ module FlexibleFeeds class Event < ActiveRecord::Base - belongs_to :ancestor, polymorphic: true - belongs_to :eventable, polymorphic: true - belongs_to :parent, polymorphic: true + belongs_to :ancestor, class_name: "FlexibleFeeds::Event" belongs_to :creator, polymorphic: true + belongs_to :eventable, polymorphic: true + belongs_to :parent, class_name: "FlexibleFeeds::Event" + has_many :children, class_name: "FlexibleFeeds::Event", + foreign_key: :parent_id has_many :event_joins, dependent: :destroy has_many :feeds, through: :event_joins has_many :votes validates :children_count, presence: true, @@ -39,18 +41,40 @@ self.popularity = calculate_popularity(votes_for, votes_for + votes_against) save end + def increment_parent_counter + ancestors.each do |this_ancestor| + this_ancestor.increment(:children_count) + end + end + + def decrement_parent_counter + ancestors.each do |this_ancestor| + this_ancestor.decrement(:children_count, children_count + 1) + end + 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 + + def ancestors + ancestors = [] + next_parent = parent + until next_parent.nil? do + ancestors.push next_parent + next_parent = next_parent.parent + end + ancestors end end end