Skip to content

Commit

Permalink
implications: remove memoization of parents, descendants, forum_updater.
Browse files Browse the repository at this point in the history
Not necessary for performance and flushing the cache at the right times
added some complexity.
  • Loading branch information
evazion committed Feb 22, 2020
1 parent c6de2ec commit 2d7f77c
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions app/models/tag_implication.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class TagImplication < TagRelationship
extend Memoist

array_attribute :descendant_names

has_many :child_implications, class_name: "TagImplication", primary_key: :consequent_name, foreign_key: :antecedent_name
has_many :parent_implications, class_name: "TagImplication", primary_key: :antecedent_name, foreign_key: :consequent_name

before_save :update_descendant_names
after_save :update_descendant_names_for_parents
after_destroy :update_descendant_names_for_parents
Expand All @@ -16,7 +17,6 @@ class TagImplication < TagRelationship

module DescendantMethods
extend ActiveSupport::Concern
extend Memoist

module ClassMethods
# assumes names are normalized
Expand All @@ -43,35 +43,24 @@ def descendants
end
end.sort.uniq
end
memoize :descendants

def update_descendant_names
self.descendant_names = descendants
end

def update_descendant_names!
flush_cache
update_descendant_names
update_attribute(:descendant_names, descendant_names)
end

def update_descendant_names_for_parents
parents.each do |parent|
parent_implications.each do |parent|
parent.update_descendant_names!
parent.update_descendant_names_for_parents
end
end
end

module ParentMethods
extend Memoist

def parents
self.class.where("consequent_name = ?", antecedent_name)
end
memoize :parents
end

module ValidationMethods
def absence_of_circular_relation
return if is_rejected?
Expand Down Expand Up @@ -124,8 +113,6 @@ def wiki_pages_present
end

module ApprovalMethods
extend Memoist

def process!(update_topic: true)
unless valid?
raise errors.full_messages.join("; ")
Expand Down Expand Up @@ -183,16 +170,9 @@ def forum_updater
skip_update: !TagRelationship::SUPPORT_HARD_CODED
)
end
memoize :forum_updater
end

include DescendantMethods
include ParentMethods
include ValidationMethods
include ApprovalMethods

def reload(options = {})
flush_cache
super
end
end

0 comments on commit 2d7f77c

Please sign in to comment.