acts_as_tree 1.6.0 → 1.6.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.
- checksums.yaml +4 -4
- data/README.md +23 -2
- data/acts_as_tree.gemspec +2 -2
- data/lib/acts_as_tree.rb +22 -12
- data/lib/acts_as_tree/version.rb +1 -1
- data/test/acts_as_tree_test.rb +58 -23
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 935893a440670b7d94b79bf3705368e8bbb376bb
|
4
|
+
data.tar.gz: 559f8dadb94b8a72f0cbf338958c632019d87d0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd51a4bfee115a00ee7306906955ab022da8544cdb7de247f66417998572dd0dfc3219a03aa3bad3b7c02658cd75827d76ad1bf04af07cade21e787b3bedf08a
|
7
|
+
data.tar.gz: d298cf66a35aaa8da2826d270ec3e5b046c6cced630547e40a1640114c849ebf1646d10d2f18c9d5bcd6172e1796afb04179ae7b28d6b84ea68ef58669b37aa4
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
[travis]: (http://travis-ci.org/amerine/acts_as_tree)
|
3
3
|
|
4
4
|
|
5
|
-
ActsAsTree extends ActiveRecord to add simple support for organizing items into parent–children relationships.
|
5
|
+
ActsAsTree extends ActiveRecord to add simple support for organizing items into parent–children relationships. By default, ActsAsTree expects a foreign key column called `parent_id`.
|
6
6
|
|
7
7
|
## Example
|
8
8
|
|
@@ -48,6 +48,10 @@ We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0.
|
|
48
48
|
Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
|
49
49
|
|
50
50
|
## Change Log
|
51
|
+
* 1.6.1 - April 21, 2014
|
52
|
+
* Readme Immprovements, see #26 -- schlick
|
53
|
+
* Improvements and Fixes for counter cache (fix counter\_cache: true). See #24, #25 -- dv
|
54
|
+
* Cleanup and fix tests, see #24.
|
51
55
|
* 1.6.0 - April 21, 2014
|
52
56
|
* Added new `leaves` method. See #23 -- MichalPokorny
|
53
57
|
* 1.5.1 - March 28, 2014
|
@@ -62,7 +66,7 @@ Moving forward we will do our best to support the latest versions of ActiveRecor
|
|
62
66
|
* Rails 4.0 Support! -- mischa78
|
63
67
|
* Readme Fixes -- mischa78 & seanhussey
|
64
68
|
* 1.2.0 - October 29th, 2012
|
65
|
-
* Adding new `self_with_ancestors` accessor --
|
69
|
+
* Adding new `self_with_ancestors` accessor -- felixbuenemann
|
66
70
|
* `roots` is now a scope.
|
67
71
|
* 1.1.0 - April 24th, 2012
|
68
72
|
* Deprecate the ActiveRecord::Acts::Tree module in favor of ActsAsTree
|
@@ -91,6 +95,23 @@ Moving forward we will do our best to support the latest versions of ActiveRecor
|
|
91
95
|
access to the repository and are welcome as full contributors to ActsAsTree. All
|
92
96
|
we ask is that all changes go through CI and a Pull Request before merging.
|
93
97
|
|
98
|
+
## Releasing new versions
|
99
|
+
|
100
|
+
1. We follow Semver. So if you're shipping interface breaking changes then bump
|
101
|
+
the major version. We don't care if we ship version 1101.1.1, as long as
|
102
|
+
people know that 1101.1.1 has breaking differences from 1100.0. If you're
|
103
|
+
adding new features, but not changing existing functionality bump the minor
|
104
|
+
version, if you're shipping a bugfix, just bump the patch.
|
105
|
+
2. Following the above rules, change the version found in lib/acts_as_tree/version.rb.
|
106
|
+
3. Make sure the Change log in the README includes a brief summary of the versions
|
107
|
+
changes, with credit to the contributors.
|
108
|
+
4. Commit these changes in one "release-prep" commit (on the master branch).
|
109
|
+
5. Push that commit up to the repo.
|
110
|
+
6. Run `rake release`
|
111
|
+
This will create and push a tag to Github, then generate a gem and push it to
|
112
|
+
Rubygems.
|
113
|
+
7. Profit.
|
114
|
+
|
94
115
|
## License (MIT)
|
95
116
|
|
96
117
|
Copyright (c) 2007 David Heinemeier Hansson
|
data/acts_as_tree.gemspec
CHANGED
@@ -5,8 +5,8 @@ require 'acts_as_tree/version'
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'acts_as_tree'
|
7
7
|
s.version = ActsAsTree::VERSION
|
8
|
-
s.authors = ['Erik Dahlstrand', 'Rails Core', 'Mark Turner', 'Swanand Pagnis']
|
9
|
-
s.email = ['erik.dahlstrand@gmail.com', 'mark@amerine.net', 'swanand.pagnis@gmail.com']
|
8
|
+
s.authors = ['Erik Dahlstrand', 'Rails Core', 'Mark Turner', 'Swanand Pagnis', 'Felix Bünemann']
|
9
|
+
s.email = ['erik.dahlstrand@gmail.com', 'mark@amerine.net', 'swanand.pagnis@gmail.com', 'felix.buenemann@gmail.com']
|
10
10
|
s.homepage = 'https://github.com/amerine/acts_as_tree'
|
11
11
|
s.summary = %q{Provides a simple tree behaviour to active_record models.}
|
12
12
|
s.description = %q{A gem that adds simple support for organizing ActiveRecord models into parent–children relationships.}
|
data/lib/acts_as_tree.rb
CHANGED
@@ -59,7 +59,8 @@ module ActsAsTree
|
|
59
59
|
# * <tt>order</tt> - makes it possible to sort the children according to
|
60
60
|
# this SQL snippet.
|
61
61
|
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column
|
62
|
-
# if set to +true+ (default: +false+).
|
62
|
+
# if set to +true+ (default: +false+). Specify
|
63
|
+
# a custom column by passing a symbol or string.
|
63
64
|
def acts_as_tree(options = {})
|
64
65
|
configuration = {
|
65
66
|
foreign_key: "parent_id",
|
@@ -70,6 +71,10 @@ module ActsAsTree
|
|
70
71
|
|
71
72
|
configuration.update(options) if options.is_a?(Hash)
|
72
73
|
|
74
|
+
if configuration[:counter_cache] == true
|
75
|
+
configuration[:counter_cache] = :children_count
|
76
|
+
end
|
77
|
+
|
73
78
|
belongs_to :parent, class_name: name,
|
74
79
|
foreign_key: configuration[:foreign_key],
|
75
80
|
counter_cache: configuration[:counter_cache],
|
@@ -92,8 +97,6 @@ module ActsAsTree
|
|
92
97
|
class_eval <<-EOV
|
93
98
|
include ActsAsTree::InstanceMethods
|
94
99
|
|
95
|
-
after_update :update_parents_counter_cache
|
96
|
-
|
97
100
|
def self.default_tree_order
|
98
101
|
order_option = %Q{#{configuration.fetch :order, "nil"}}
|
99
102
|
order(order_option)
|
@@ -109,11 +112,16 @@ module ActsAsTree
|
|
109
112
|
EOV
|
110
113
|
|
111
114
|
if configuration[:counter_cache]
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
115
|
+
after_update :update_parents_counter_cache
|
116
|
+
|
117
|
+
def children_counter_cache_column
|
118
|
+
reflect_on_association(:parent).counter_cache_column
|
119
|
+
end
|
120
|
+
|
121
|
+
def leaves
|
122
|
+
where(children_counter_cache_column => 0).default_tree_order
|
123
|
+
end
|
124
|
+
|
117
125
|
else
|
118
126
|
# Fallback to less efficent ways to find leaves.
|
119
127
|
class_eval <<-EOV
|
@@ -232,15 +240,17 @@ module ActsAsTree
|
|
232
240
|
# subchild1.leaf? # => true
|
233
241
|
# child1.leaf? # => false
|
234
242
|
def leaf?
|
235
|
-
children.
|
243
|
+
children.size.zero?
|
236
244
|
end
|
237
245
|
|
238
246
|
private
|
239
247
|
|
240
248
|
def update_parents_counter_cache
|
241
|
-
|
242
|
-
|
243
|
-
|
249
|
+
counter_cache_column = self.class.children_counter_cache_column
|
250
|
+
|
251
|
+
if parent_id_changed?
|
252
|
+
self.class.decrement_counter(counter_cache_column, parent_id_was)
|
253
|
+
self.class.increment_counter(counter_cache_column, parent_id)
|
244
254
|
end
|
245
255
|
end
|
246
256
|
end
|
data/lib/acts_as_tree/version.rb
CHANGED
data/test/acts_as_tree_test.rb
CHANGED
@@ -4,31 +4,57 @@ require 'active_record'
|
|
4
4
|
require 'acts_as_tree'
|
5
5
|
|
6
6
|
class MiniTest::Unit::TestCase
|
7
|
-
def assert_queries(num = 1)
|
8
|
-
|
9
|
-
|
7
|
+
def assert_queries(num = 1, &block)
|
8
|
+
query_count, result = count_queries(&block)
|
9
|
+
result
|
10
10
|
ensure
|
11
|
-
assert_equal num,
|
11
|
+
assert_equal num, query_count, "#{query_count} instead of #{num} queries were executed."
|
12
12
|
end
|
13
13
|
|
14
14
|
def assert_no_queries(&block)
|
15
15
|
assert_queries(0, &block)
|
16
16
|
end
|
17
|
+
|
18
|
+
def count_queries &block
|
19
|
+
count = 0
|
20
|
+
|
21
|
+
counter_f = ->(name, started, finished, unique_id, payload) {
|
22
|
+
unless %w[ CACHE SCHEMA ].include? payload[:name]
|
23
|
+
count += 1
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
result = ActiveSupport::Notifications.subscribed(counter_f, "sql.active_record", &block)
|
28
|
+
|
29
|
+
[count, result]
|
30
|
+
end
|
31
|
+
|
32
|
+
def capture_stdout(&block)
|
33
|
+
real_stdout = $stdout
|
34
|
+
|
35
|
+
$stdout = StringIO.new
|
36
|
+
yield
|
37
|
+
$stdout.string
|
38
|
+
ensure
|
39
|
+
$stdout = real_stdout
|
40
|
+
end
|
17
41
|
end
|
18
42
|
|
19
43
|
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
20
44
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
45
|
+
def setup_db(counter_cache = false)
|
46
|
+
# AR keeps printing annoying schema statements
|
47
|
+
capture_stdout do
|
48
|
+
ActiveRecord::Base.logger
|
49
|
+
ActiveRecord::Schema.define(version: 1) do
|
50
|
+
create_table :mixins do |t|
|
51
|
+
t.column :type, :string
|
52
|
+
t.column :parent_id, :integer
|
53
|
+
t.column(:children_count, :integer, default: 0) if counter_cache
|
54
|
+
end
|
31
55
|
end
|
56
|
+
|
57
|
+
Mixin.reset_column_information
|
32
58
|
end
|
33
59
|
end
|
34
60
|
|
@@ -236,9 +262,7 @@ class TreeTest < MiniTest::Unit::TestCase
|
|
236
262
|
|_ 6
|
237
263
|
|_ 7
|
238
264
|
END
|
239
|
-
|
240
|
-
TreeMixin.tree_view(:id)
|
241
|
-
assert_equal tree_view_outputs, $stdout.string
|
265
|
+
assert_equal tree_view_outputs, capture_stdout { TreeMixin.tree_view(:id) }
|
242
266
|
end
|
243
267
|
end
|
244
268
|
|
@@ -322,9 +346,9 @@ class TreeTestWithEagerLoading < MiniTest::Unit::TestCase
|
|
322
346
|
assert_equal [@root1, @root2, @root3], roots
|
323
347
|
|
324
348
|
assert_no_queries do
|
325
|
-
assert_equal 2, roots[0].children.
|
326
|
-
assert_equal 0, roots[1].children.
|
327
|
-
assert_equal 0, roots[2].children.
|
349
|
+
assert_equal 2, roots[0].children.size
|
350
|
+
assert_equal 0, roots[1].children.size
|
351
|
+
assert_equal 0, roots[2].children.size
|
328
352
|
end
|
329
353
|
end
|
330
354
|
|
@@ -395,11 +419,14 @@ end
|
|
395
419
|
class TreeTestWithCounterCache < MiniTest::Unit::TestCase
|
396
420
|
def setup
|
397
421
|
teardown_db
|
398
|
-
setup_db
|
422
|
+
setup_db(true)
|
423
|
+
|
399
424
|
@root = TreeMixinWithCounterCache.create!
|
400
425
|
@child1 = TreeMixinWithCounterCache.create! parent_id: @root.id
|
401
426
|
@child1_child1 = TreeMixinWithCounterCache.create! parent_id: @child1.id
|
402
427
|
@child2 = TreeMixinWithCounterCache.create! parent_id: @root.id
|
428
|
+
|
429
|
+
[@root, @child1, @child1_child1, @child2].map(&:reload)
|
403
430
|
end
|
404
431
|
|
405
432
|
def teardown
|
@@ -407,8 +434,8 @@ class TreeTestWithCounterCache < MiniTest::Unit::TestCase
|
|
407
434
|
end
|
408
435
|
|
409
436
|
def test_counter_cache
|
410
|
-
assert_equal 2, @root.
|
411
|
-
assert_equal 1, @child1.
|
437
|
+
assert_equal 2, @root.children_count
|
438
|
+
assert_equal 1, @child1.children_count
|
412
439
|
end
|
413
440
|
|
414
441
|
def test_update_parents_counter_cache
|
@@ -419,5 +446,13 @@ class TreeTestWithCounterCache < MiniTest::Unit::TestCase
|
|
419
446
|
|
420
447
|
def test_leaves
|
421
448
|
assert_equal [@child1_child1, @child2], TreeMixinWithCounterCache.leaves
|
449
|
+
|
450
|
+
assert !@root.leaf?
|
451
|
+
assert @child2.leaf?
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_counter_cache_being_used
|
455
|
+
assert_no_queries { @root.leaf? }
|
456
|
+
assert_no_queries { @child2.leaf? }
|
422
457
|
end
|
423
458
|
end
|
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Dahlstrand
|
8
8
|
- Rails Core
|
9
9
|
- Mark Turner
|
10
10
|
- Swanand Pagnis
|
11
|
+
- Felix Bünemann
|
11
12
|
autorequire:
|
12
13
|
bindir: bin
|
13
14
|
cert_chain: []
|
14
|
-
date: 2014-
|
15
|
+
date: 2014-05-29 00:00:00.000000000 Z
|
15
16
|
dependencies:
|
16
17
|
- !ruby/object:Gem::Dependency
|
17
18
|
name: activerecord
|
@@ -75,6 +76,7 @@ email:
|
|
75
76
|
- erik.dahlstrand@gmail.com
|
76
77
|
- mark@amerine.net
|
77
78
|
- swanand.pagnis@gmail.com
|
79
|
+
- felix.buenemann@gmail.com
|
78
80
|
executables: []
|
79
81
|
extensions: []
|
80
82
|
extra_rdoc_files: []
|