Module: BeyondAlbert::Acts::TextBasedNestedSet::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/text_based_nested_set/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



137
138
139
140
# File 'lib/text_based_nested_set/model.rb', line 137

def ancestors
  parent_ids = self.path.split('/').select {|v| v != "" && v != "0"}
  current_class.where(id: parent_ids)
end

#childrenObject



147
148
149
150
# File 'lib/text_based_nested_set/model.rb', line 147

def children
  children_path = self.path + self.id.to_s + '/'
  current_class.where(path: children_path).order('position')
end

#descendantsObject



152
153
154
155
# File 'lib/text_based_nested_set/model.rb', line 152

def descendants
  descendants_path = self.path + self.id.to_s + '/%'
  current_class.where("path LIKE ?", descendants_path).order("LENGTH(path) ASC, position ASC")
end

#left_siblingObject



166
167
168
169
170
171
172
173
# File 'lib/text_based_nested_set/model.rb', line 166

def left_sibling
  left_siblings = current_class.where(path: self.path, position: self.position - 1 )
  if left_siblings.empty?
    nil
  else
    left_siblings.first
  end
end

#move_to_child_of(target) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/text_based_nested_set/model.rb', line 106

def move_to_child_of(target)
  in_tenacious_transaction do
    descendants = self.descendants

    # set old siblings' position
    self.siblings.each do |s|
      if s.position > self.position
        s.position -= 1
        s.save!
      end
    end

    # set self attributes
    self.update(path: target.path + target.id.to_s + '/', parent_id: target.id, position: target.children.size)

    # set descendants' path
    descendants.each do |d|
      d.path = d.parent.path + d.parent_id.to_s + '/'
      d.save!
    end
  end
end

#move_to_left_of(target) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/text_based_nested_set/model.rb', line 43

def move_to_left_of(target)
  in_tenacious_transaction do
    descendants = self.descendants

    # set new siblings's position
    target.self_and_siblings.each do |s|
      if s.position >= target.position
        s.position += 1
        s.save!
      end
    end
    target.reload

    # set old siblings's position
    self.siblings.each do |s|
      if s.position > self.position
        s.position -= 1
        s.save!
      end
    end

    # set self attributes 
    self.update(path: target.path, parent_id: target.parent_id, position: target.position - 1)

    # set descendants' path
    descendants.each do |d|
      d.path = d.parent.path + d.parent_id.to_s + '/'
      d.save!
    end
  end
end

#move_to_right_of(target) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/text_based_nested_set/model.rb', line 75

def move_to_right_of(target)
  in_tenacious_transaction do
    descendants = self.descendants

    # set new siblings' position
    target.self_and_siblings.each do |s|
      if s.position > target.position
        s.position += 1
        s.save!
      end
    end

    # set old siblings' position
    self.siblings.each do |s|
      if s.position > self.position
        s.position -= 1
        s.save!
      end
    end

    # set self attributes
    self.update(path: target.path, parent_id: target.parent_id, position: target.position + 1)

    # set descendants' path
    descendants.each do |d|
      d.path = d.parent.path + d.parent_id.to_s + '/'
      d.save!
    end
  end
end

#move_to_rootObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/text_based_nested_set/model.rb', line 20

def move_to_root
  in_tenacious_transaction do
    # reset siblings' position
    self.siblings.each do |s|
      if s.position > self.position
        s.position -= 1
        s.save!
      end
    end

    descendants = self.descendants

    self.update(parent_id: 0, path: '/0/', position: 0)

    # reset descendants' path
    descendants.each do |d|
      d.path = d.parent.path + d.parent_id.to_s + '/'
      d.save!
    end

  end
end

#parentObject



129
130
131
132
133
134
135
# File 'lib/text_based_nested_set/model.rb', line 129

def parent
  if self.root?
    nil
  else
    current_class.find(self.parent_id)
  end
end

#rebuildObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/text_based_nested_set/model.rb', line 187

def rebuild
  in_tenacious_transaction do
    children = current_class.where(parent_id: self.id)
    if self.parent_id == 0
      self.update!(path: '/0/')
    end
    unless children.empty?
      children.each.with_index do |c, index|
        c.update(path: self.path + self.id.to_s + '/', position: index)
        c.rebuild
      end
    end
  end
end

#right_siblingObject



157
158
159
160
161
162
163
164
# File 'lib/text_based_nested_set/model.rb', line 157

def right_sibling
  right_siblings = current_class.where(path: self.path, position: self.position + 1)
  if right_siblings.empty?
    nil
  else
    right_siblings.first
  end
end

#root?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/text_based_nested_set/model.rb', line 183

def root?
  self.parent_id == 0
end

#self_and_ancestorsObject



142
143
144
145
# File 'lib/text_based_nested_set/model.rb', line 142

def self_and_ancestors
  parent_ids = self.path.split('/').select {|v| v != "" && v != "0"} << self.id
  current_class.where(id: parent_ids)
end

#self_and_siblingsObject



179
180
181
# File 'lib/text_based_nested_set/model.rb', line 179

def self_and_siblings
  current_class.where(path: self.path)
end

#siblingsObject



175
176
177
# File 'lib/text_based_nested_set/model.rb', line 175

def siblings
  current_class.where(path: self.path).select {|o| o.id != self.id}
end