Class: PaperTrail::RelatedChanges::Hierarchy
- Inherits:
-
Object
- Object
- PaperTrail::RelatedChanges::Hierarchy
- Defined in:
- lib/paper_trail/related_changes/hierarchy.rb
Defined Under Namespace
Modules: Query Classes: Node, SelfRelation
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
-
.build(*args) ⇒ Object
Builds downward relational hierarchy with 4 generations.
-
.model_type_children(model) ⇒ Object
Does filtering of relations.
Instance Method Summary collapse
- #find_by_id(id) ⇒ Object
-
#initialize(model) ⇒ Hierarchy
constructor
A new instance of Hierarchy.
- #model_type_children ⇒ Object
-
#search_hierarchy(item_type, source: send(:source), parent: nil) ⇒ Object
Find the node that matches the item_type The result has a parent reference instead of a child reference.
-
#shared_relation(versions) ⇒ Object
Finds a common root between versions.
- #source ⇒ Object
Constructor Details
#initialize(model) ⇒ Hierarchy
Returns a new instance of Hierarchy.
20 21 22 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 20 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
18 19 20 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 18 def model @model end |
Class Method Details
.build(*args) ⇒ Object
Builds downward relational hierarchy with 4 generations
8 9 10 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 8 def build(*args) self.new(*args).send(:build_source) end |
.model_type_children(model) ⇒ Object
Does filtering of relations
13 14 15 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 13 def model_type_children(model) self.new(model).model_type_children end |
Instance Method Details
#find_by_id(id) ⇒ Object
24 25 26 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 24 def find_by_id(id) Query.call(model, id) end |
#model_type_children ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 49 def model_type_children @model_type_children ||= model.reflections.each_with_object({}) do |(name, rel), result| next if source_reflection(rel).belongs_to? # Only Looking for downward relations. (Children not parents) next if name == PaperTrail::Version.table_name result[name.to_sym] = source_reflection(rel) end end |
#search_hierarchy(item_type, source: send(:source), parent: nil) ⇒ Object
Find the node that matches the item_type The result has a parent reference instead of a child reference.
30 31 32 33 34 35 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 30 def search_hierarchy(item_type, source: send(:source), parent: nil) parent_without_children = (parent || source).except(:children) return [source.except(:children).merge(parent: parent_without_children)] if source[:type] == item_type return if source[:children].empty? source[:children].flat_map { |child| search_hierarchy(item_type, source: child, parent: source) }.compact end |
#shared_relation(versions) ⇒ Object
Finds a common root between versions
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 38 def shared_relation(versions) result = versions.map do |version| [ version.item_type, # It can be it's self *search_hierarchy(version.item_type).map { |s| s.fetch(:parent, {}).fetch(:type, s[:type]) } ].uniq end.inject(:&)&.last search_hierarchy(result)&.first end |
#source ⇒ Object
57 58 59 |
# File 'lib/paper_trail/related_changes/hierarchy.rb', line 57 def source @source ||= build_source end |