forestify 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +3 -7
- data/lib/forestify.rb +16 -5
- metadata +2 -2
data/README.md
CHANGED
@@ -14,12 +14,9 @@ end
|
|
14
14
|
You can then do something like this :
|
15
15
|
|
16
16
|
```ruby
|
17
|
-
vehicle = Tag.
|
18
|
-
vehicle.
|
19
|
-
|
20
|
-
car.save!
|
21
|
-
audi = Tag.new(name: "Audi", parent: car.id)
|
22
|
-
audi.save!
|
17
|
+
vehicle = Tag.create!(name: "Vehicle")
|
18
|
+
car = Tag.create!(name: "Car", parent_id: vehicle.id)
|
19
|
+
audi = Tag.create!(name: "Audi", parent_id: car.id)
|
23
20
|
|
24
21
|
audi.parents
|
25
22
|
# => [vehicle, car]
|
@@ -37,7 +34,6 @@ Although I will add generators later, you still need to manually add migrations
|
|
37
34
|
|
38
35
|
```ruby
|
39
36
|
change_table :tags do |t|
|
40
|
-
t.string :name
|
41
37
|
t.integer :left_position
|
42
38
|
t.integer :right_position
|
43
39
|
t.integer :level
|
data/lib/forestify.rb
CHANGED
@@ -5,7 +5,7 @@ module Forestify
|
|
5
5
|
end
|
6
6
|
before_create :initialize_position
|
7
7
|
before_destroy :update_positions_after_delete
|
8
|
-
attr_accessor :
|
8
|
+
attr_accessor :parent_id
|
9
9
|
end
|
10
10
|
|
11
11
|
module InstanceMethods
|
@@ -14,7 +14,7 @@ module Forestify
|
|
14
14
|
# Should be run only once
|
15
15
|
def initialize_position
|
16
16
|
# @parent = -1 is the option 'No parent'
|
17
|
-
if @
|
17
|
+
if @parent_id.nil? || @parent_id == "-1"
|
18
18
|
# No parent has been specified, we need to add this leaf
|
19
19
|
# to the right side of the last root node.
|
20
20
|
last = self.class.order("right_position DESC").first
|
@@ -23,8 +23,8 @@ module Forestify
|
|
23
23
|
self.level = 0
|
24
24
|
else
|
25
25
|
# Makes sure it's an integer
|
26
|
-
@
|
27
|
-
p = self.class.find(@
|
26
|
+
@parent_id = @parent_id.to_i
|
27
|
+
p = self.class.find(@parent_id)
|
28
28
|
self.left_position = p.right_position
|
29
29
|
self.right_position = self.left_position + 1
|
30
30
|
self.level = p.level + 1
|
@@ -50,16 +50,27 @@ module Forestify
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
|
54
53
|
def parents
|
55
54
|
self.class.where('left_position < ?', self.left_position).where('right_position > ?', self.right_position)
|
56
55
|
end
|
57
56
|
|
57
|
+
def parent
|
58
|
+
self.parents.where('level = ?', self.level - 1).first
|
59
|
+
end
|
60
|
+
|
58
61
|
def children
|
59
62
|
[] if is_leaf?
|
60
63
|
self.class.where('left_position > ?', self.left_position).where('right_position < ?', self.right_position)
|
61
64
|
end
|
62
65
|
|
66
|
+
def siblings
|
67
|
+
if self.parent.nil?
|
68
|
+
self.class.where('level = 0').where('id != ?', self.id)
|
69
|
+
else
|
70
|
+
self.parent.children.where('level = ?', self.level).where('id != ?', self.id)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
63
74
|
def is_node?
|
64
75
|
(self.right_position - self.left_position) > 1
|
65
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forestify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Forestify brings a tree data-structure to your Active Record models
|
15
15
|
email: gabriel.malkas@gmail.com
|