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



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

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

#check_positionObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/text_based_nested_set/model.rb', line 215

def check_position
  logger = Logger.new(STDOUT)
  if self.children.size != 0
    if self.children.size == self.children.last.position + 1
      self.children.each do |c|
        c.check_position
      end
    else
      logger.info("position not correct, node id: #{self.id}")
      return false
    end
  end
  return true
end

#childrenObject



156
157
158
159
# File 'lib/text_based_nested_set/model.rb', line 156

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

#descendantsObject



161
162
163
164
# File 'lib/text_based_nested_set/model.rb', line 161

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



179
180
181
182
183
184
185
186
# File 'lib/text_based_nested_set/model.rb', line 179

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/text_based_nested_set/model.rb', line 116

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/text_based_nested_set/model.rb', line 53

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/text_based_nested_set/model.rb', line 85

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/text_based_nested_set/model.rb', line 30

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



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

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

#rebuildObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/text_based_nested_set/model.rb', line 200

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



170
171
172
173
174
175
176
177
# File 'lib/text_based_nested_set/model.rb', line 170

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)


196
197
198
# File 'lib/text_based_nested_set/model.rb', line 196

def root?
  self.parent_id == 0
end

#self_and_ancestorsObject



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

def self_and_ancestors
  ancestors << self
end

#self_and_descendantsObject



166
167
168
# File 'lib/text_based_nested_set/model.rb', line 166

def self_and_descendants
  descendants.unshift(self)
end

#self_and_siblingsObject



192
193
194
# File 'lib/text_based_nested_set/model.rb', line 192

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

#siblingsObject



188
189
190
# File 'lib/text_based_nested_set/model.rb', line 188

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