awesome_nested_set 3.0.0.rc.5 → 3.0.0.rc.6
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.
- checksums.yaml +4 -4
- data/README.md +45 -1
- data/lib/awesome_nested_set/columns.rb +4 -0
- data/lib/awesome_nested_set/model.rb +1 -1
- data/lib/awesome_nested_set/model/movable.rb +12 -2
- data/lib/awesome_nested_set/model/prunable.rb +3 -0
- data/lib/awesome_nested_set/model/transactable.rb +2 -2
- data/lib/awesome_nested_set/model/validatable.rb +1 -1
- data/lib/awesome_nested_set/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebe27a6d3e69be31c797a0d3c663f3677d68b302
|
4
|
+
data.tar.gz: e9f0144d3430f05b4b5af855f8f58a9cd3cd174d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd9ddf03673994b9ff5cdb2a7c855f507aab5c69c1bf8eb21a367d1601634709932482d690e17baf34e5713b1f08cfabf47e50d4a9b1b76e400d9d4ed85d1971
|
7
|
+
data.tar.gz: be3ee9c75fbc3179ed6c75776edb9b65fde98ad2a7b415023d922748f5bd4b288785485e23109bf69bac45b21484f75ea9127999a942fe04ef59fc61e9e02a01
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
Awesome Nested Set is an implementation of the nested set pattern for ActiveRecord models.
|
8
8
|
It is a replacement for acts_as_nested_set and BetterNestedSet, but more awesome.
|
9
9
|
|
10
|
-
Version 2 supports Rails 3. Gem versions prior to 2.0 support Rails 2.
|
10
|
+
Version 2 supports Rails 3 and Rails 4. Gem versions prior to 2.0 support Rails 2.
|
11
11
|
|
12
12
|
## What makes this so awesome?
|
13
13
|
|
@@ -151,6 +151,50 @@ class Category < ActiveRecord::Base
|
|
151
151
|
end
|
152
152
|
```
|
153
153
|
|
154
|
+
|
155
|
+
## Add to your existing project
|
156
|
+
|
157
|
+
To make use of `awesome_nested_set`, your model needs to have 3 fields:
|
158
|
+
`lft`, `rgt`, and `parent_id`. The names of these fields are configurable.
|
159
|
+
You can also have an optional field, `depth`.
|
160
|
+
|
161
|
+
Create a migration to add fields:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
class AddNestedToCategories < ActiveRecord::Migration
|
165
|
+
|
166
|
+
def self.up
|
167
|
+
add_column :categories, :parent_id, :integer # Comment this line if your project already has this column
|
168
|
+
# Category.where(parent_id: 0).update_all(parent_id: nil) # Uncomment this line if your project already has :parent_id
|
169
|
+
add_column :categories, :lft , :integer
|
170
|
+
add_column :categories, :rgt , :integer
|
171
|
+
add_column :categories, :depth , :integer # this is optional.
|
172
|
+
|
173
|
+
# This is necessary to update :lft and :rgt columns
|
174
|
+
Category.rebuild!
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.down
|
178
|
+
remove_column :categories, :parent_id
|
179
|
+
remove_column :categories, :lft
|
180
|
+
remove_column :categories, :rgt
|
181
|
+
remove_column :categories, :depth # this is optional.
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
Enable the nested set functionality by declaring `acts_as_nested_set` on your model
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
class Category < ActiveRecord::Base
|
191
|
+
acts_as_nested_set
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
195
|
+
Your project is now ready to run with the `awesome_nested_set` gem!
|
196
|
+
|
197
|
+
|
154
198
|
## Conversion from other trees
|
155
199
|
|
156
200
|
Coming from acts_as_tree or another system where you only have a parent_id? No problem. Simply add the lft & rgt fields as above, and then run:
|
@@ -63,6 +63,10 @@ module CollectiveIdea #:nodoc:
|
|
63
63
|
"#{quoted_table_name}.#{quoted_primary_column_name}"
|
64
64
|
end
|
65
65
|
|
66
|
+
def quoted_order_column_full_name
|
67
|
+
"#{quoted_table_name}.#{quoted_order_column_name}"
|
68
|
+
end
|
69
|
+
|
66
70
|
def quoted_left_column_full_name
|
67
71
|
"#{quoted_table_name}.#{quoted_left_column_name}"
|
68
72
|
end
|
@@ -79,7 +79,7 @@ module CollectiveIdea #:nodoc:
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def nested_set_scope(options = {})
|
82
|
-
options = {:order =>
|
82
|
+
options = {:order => quoted_order_column_full_name}.merge(options)
|
83
83
|
|
84
84
|
where(options[:conditions]).order(options.delete(:order))
|
85
85
|
end
|
@@ -29,7 +29,7 @@ module CollectiveIdea #:nodoc:
|
|
29
29
|
move_to node, :left
|
30
30
|
end
|
31
31
|
|
32
|
-
# Move the node to the
|
32
|
+
# Move the node to the right of another node
|
33
33
|
def move_to_right_of(node)
|
34
34
|
move_to node, :right
|
35
35
|
end
|
@@ -46,7 +46,17 @@ module CollectiveIdea #:nodoc:
|
|
46
46
|
elsif node.children.count == index
|
47
47
|
move_to_right_of(node.children.last)
|
48
48
|
else
|
49
|
-
|
49
|
+
my_position = node.children.index(self)
|
50
|
+
if my_position && my_position < index
|
51
|
+
# e.g. if self is at position 0 and we want to move self to position 1 then self
|
52
|
+
# needs to move to the *right* of the node at position 1. That's because the node
|
53
|
+
# that is currently at position 1 will be at position 0 after the move completes.
|
54
|
+
move_to_right_of(node.children[index])
|
55
|
+
elsif my_position && my_position == index
|
56
|
+
# do nothing. already there.
|
57
|
+
else
|
58
|
+
move_to_left_of(node.children[index])
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
|
@@ -19,6 +19,9 @@ module CollectiveIdea #:nodoc:
|
|
19
19
|
# update lefts and rights for remaining nodes
|
20
20
|
update_siblings_for_remaining_nodes
|
21
21
|
|
22
|
+
# Reload is needed because children may have updated their parent (self) during deletion.
|
23
|
+
reload
|
24
|
+
|
22
25
|
# Don't allow multiple calls to destroy to corrupt the set
|
23
26
|
self.skip_before_destroy = true
|
24
27
|
end
|
@@ -10,8 +10,8 @@ module CollectiveIdea #:nodoc:
|
|
10
10
|
begin
|
11
11
|
transaction(&block)
|
12
12
|
rescue ActiveRecord::StatementInvalid => error
|
13
|
-
raise unless connection.open_transactions.zero?
|
14
|
-
raise unless error.message =~ /
|
13
|
+
raise unless self.class.connection.open_transactions.zero?
|
14
|
+
raise unless error.message =~ /[Dd]eadlock|Lock wait timeout exceeded/
|
15
15
|
raise unless retry_count < 10
|
16
16
|
retry_count += 1
|
17
17
|
logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"
|
@@ -17,7 +17,7 @@ module CollectiveIdea
|
|
17
17
|
def no_duplicates_for_columns?
|
18
18
|
[quoted_left_column_full_name, quoted_right_column_full_name].all? do |column|
|
19
19
|
# No duplicates
|
20
|
-
select("#{scope_string}#{column}, COUNT(#{column})").
|
20
|
+
select("#{scope_string}#{column}, COUNT(#{column}) as _count").
|
21
21
|
group("#{scope_string}#{column}", quoted_primary_key_column_full_name).
|
22
22
|
having("COUNT(#{column}) > 1").
|
23
23
|
order(quoted_primary_key_column_full_name).
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_nested_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.rc.
|
4
|
+
version: 3.0.0.rc.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Keepers
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|