Module: HideAncestry::InstanceMethods
- Defined in:
- lib/hide_ancestry/instance_methods.rb
Instance Method Summary collapse
-
#changes_applied ⇒ Object
Monkeypatching ActiveModel::Dirty method to correct work of #previous_changes in model.
- #children_of_hidden ⇒ Object
- #depth_with_hidden ⇒ Object
- #find_first_real_parent ⇒ Object
- #first_hiding? ⇒ Boolean
- #hidden? ⇒ Boolean
- #hidden_children ⇒ Object
- #hidden_children_present? ⇒ Boolean
- #hidden_descendants_ids ⇒ Object
- #hidden_ids ⇒ Object
- #hidden_parent ⇒ Object
- #hidden_parent_changed? ⇒ Boolean
- #hide ⇒ Object
- #hide_ancestry_ids ⇒ Object
- #restore ⇒ Object
- #subtree_with_hidden ⇒ Object
Instance Method Details
#changes_applied ⇒ Object
Monkeypatching ActiveModel::Dirty method to correct work of #previous_changes in model
88 89 90 91 |
# File 'lib/hide_ancestry/instance_methods.rb', line 88 def changes_applied @previously_changed = @record_changes @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end |
#children_of_hidden ⇒ Object
28 29 30 |
# File 'lib/hide_ancestry/instance_methods.rb', line 28 def children_of_hidden self.class.where id: old_child_ids end |
#depth_with_hidden ⇒ Object
53 54 55 |
# File 'lib/hide_ancestry/instance_methods.rb', line 53 def depth_with_hidden hide_ancestry_ids.size end |
#find_first_real_parent ⇒ Object
80 81 82 83 84 |
# File 'lib/hide_ancestry/instance_methods.rb', line 80 def find_first_real_parent parent_usr = self.class.find_by id: old_parent_id return parent unless parent_usr parent_usr.hidden? ? parent_usr.find_first_real_parent : parent_usr end |
#first_hiding? ⇒ Boolean
14 15 16 17 18 |
# File 'lib/hide_ancestry/instance_methods.rb', line 14 def first_hiding? old_parent_changes = previous_changes['old_parent_id'] return unless old_parent_changes old_parent_changes.first.nil? end |
#hidden? ⇒ Boolean
20 21 22 |
# File 'lib/hide_ancestry/instance_methods.rb', line 20 def hidden? self.public_send(hidden_column) == true end |
#hidden_children ⇒ Object
32 33 34 |
# File 'lib/hide_ancestry/instance_methods.rb', line 32 def hidden_children self.class.hidden.where old_parent_id: id end |
#hidden_children_present? ⇒ Boolean
65 66 67 |
# File 'lib/hide_ancestry/instance_methods.rb', line 65 def hidden_children_present? self.class.hidden_childs(id).present? end |
#hidden_descendants_ids ⇒ Object
69 70 71 72 73 74 |
# File 'lib/hide_ancestry/instance_methods.rb', line 69 def hidden_descendants_ids ids = [] iterate_desc_for_hidden(ids) iterate_hidden_desc(ids) ids.uniq end |
#hidden_ids ⇒ Object
76 77 78 |
# File 'lib/hide_ancestry/instance_methods.rb', line 76 def hidden_ids self.class.hidden.pluck(:id) end |
#hidden_parent ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/hide_ancestry/instance_methods.rb', line 57 def hidden_parent sought_parent = self.class.hidden.select do |u| u.old_child_ids.include? id end sought_parent.blank? ? nil : sought_parent.first end |
#hidden_parent_changed? ⇒ Boolean
36 37 38 39 40 41 42 43 44 |
# File 'lib/hide_ancestry/instance_methods.rb', line 36 def hidden_parent_changed? sought_parent = hidden_parent return false unless sought_parent # Existing parent (for hidden node and child of hidden) should be the same; # if not - means than child changed it parent grandparent = sought_parent.find_first_real_parent grandparent&.id != parent_id end |
#hide ⇒ Object
3 4 5 6 |
# File 'lib/hide_ancestry/instance_methods.rb', line 3 def hide return not_valid unless valid? HideAncestry::ModelManage::Hide.call(self) end |
#hide_ancestry_ids ⇒ Object
24 25 26 |
# File 'lib/hide_ancestry/instance_methods.rb', line 24 def hide_ancestry_ids hide_ancestry.split('/').map(&:to_i) if hide_ancestry end |
#restore ⇒ Object
8 9 10 11 12 |
# File 'lib/hide_ancestry/instance_methods.rb', line 8 def restore return not_valid unless valid? return already_restored unless public_send(hidden_column) == true HideAncestry::ModelManage::Restore.call(self) end |
#subtree_with_hidden ⇒ Object
46 47 48 49 50 51 |
# File 'lib/hide_ancestry/instance_methods.rb', line 46 def subtree_with_hidden sub_ids = subtree.pluck(:id) ids_for_search = sub_ids + hidden_descendants_ids relation = self.class.where id: ids_for_search relation.order(hide_ancestry: :asc) end |