Module: HideAncestry::InstanceMethods

Defined in:
lib/hide_ancestry/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#changes_appliedObject

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_hiddenObject



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_hiddenObject



53
54
55
# File 'lib/hide_ancestry/instance_methods.rb', line 53

def depth_with_hidden
  hide_ancestry_ids.size
end

#find_first_real_parentObject



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

Returns:

  • (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

Returns:

  • (Boolean)


20
21
22
# File 'lib/hide_ancestry/instance_methods.rb', line 20

def hidden?
  self.public_send(hidden_column) == true
end

#hidden_childrenObject



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

Returns:

  • (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_idsObject



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_idsObject



76
77
78
# File 'lib/hide_ancestry/instance_methods.rb', line 76

def hidden_ids
  self.class.hidden.pluck(:id)
end

#hidden_parentObject



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

Returns:

  • (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

#hideObject



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_idsObject



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

#restoreObject



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_hiddenObject



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