module HideAncestry::InstanceMethods

Public Instance Methods

changes_applied() click to toggle source

Monkeypatching ActiveModel::Dirty method to correct work of previous_changes in model

# 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() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 28
def children_of_hidden
  self.class.where id: old_child_ids
end
depth_with_hidden() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 53
def depth_with_hidden
  hide_ancestry_ids.size
end
find_first_real_parent() click to toggle source
# 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?() click to toggle source
# 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?() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 20
def hidden?
  self.public_send(hidden_column) == true
end
hidden_children() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 32
def hidden_children
  self.class.hidden.where old_parent_id: id
end
hidden_children_present?() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 65
def hidden_children_present?
  self.class.hidden_childs(id).present?
end
hidden_descendants_ids() click to toggle source
# 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() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 76
def hidden_ids
  self.class.hidden.pluck(:id)
end
hidden_parent() click to toggle source
# 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?() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 24
def hide_ancestry_ids
  hide_ancestry.split('/').map(&:to_i) if hide_ancestry
end
restore() click to toggle source
# 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() click to toggle source
# 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

Protected Instance Methods

iterate_desc_for_hidden(array) click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 95
def iterate_desc_for_hidden(array)
  descendants.each do |user|
    user.hidden_children.each { |child| array << child.id }
  end
end
iterate_hidden_desc(array) click to toggle source
# File lib/hide_ancestry/instance_methods.rb, line 101
def iterate_hidden_desc(array)
  hidden_children.each do |hidden_child|
    array << hidden_child.id
    hidden_child.iterate_hidden_desc(array)
  end
end