acts_as_tree 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,27 +2,69 @@
2
2
 
3
3
  # ActsAsTree
4
4
 
5
- ## Notes
6
- Specify this `acts_as` extension if you want to model a tree structure by providing a parent association and a children association. This requires that you have a foreign key column, which by default is called `parent_id`.
5
+ A gem that extends ActiveRecord to add simple support for organizing items into
6
+ parentchildren relationships.
7
+
8
+ ## Example
7
9
 
8
10
  class Category < ActiveRecord::Base
9
- acts_as_tree :order => "name"
11
+ include ActiveRecord::Acts:Tree
12
+
13
+ acts_as_tree order: "name"
10
14
  end
11
15
 
12
- Example:
13
- root
14
- \_ child1
15
- \_ subchild1
16
- \_ subchild2
17
-
18
16
  root = Category.create("name" => "root")
19
17
  child1 = root.children.create("name" => "child1")
20
18
  subchild1 = child1.children.create("name" => "subchild1")
21
-
19
+
22
20
  root.parent # => nil
23
21
  child1.parent # => root
24
22
  root.children # => [child1]
25
23
  root.children.first.children.first # => subchild1
26
24
 
25
+ ## Compatibility
26
+
27
+ We no longer support Ruby 1.8 or versions if Rails/ActiveRecord older than 3.0. If you're using a version of ActiveRecord older than 3.0 please use 0.1.1.
28
+
29
+ Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
30
+
31
+ ## Change Log
32
+
33
+ * 0.2.0 - April 9, 2012
34
+ * Rails 3 Support
35
+ * 0.1.1 - February 3, 2010
36
+ * Bug Fixes
37
+ * 0.1.0 - October 9th, 2009
38
+ * First Gem Release
39
+
40
+ ## Note on Patches/Pull Requests
41
+
42
+ 1. Fork the project.
43
+ 2. Make your feature addition or bug fix.
44
+ 3. Add tests for it. This is important so we don't break it in a future version
45
+ unintentionally.
46
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have
47
+ your own version, that is fine but bump version in a commit by itself so we can
48
+ ignore when we pull)
49
+ 5. Send us a pull request. Bonus points for topic branches.
50
+
51
+ ## License (MIT)
52
+
53
+ Copyright (c) 2007 David Heinemeier Hansson
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
56
+ this software and associated documentation files (the 'Software'), to deal in the
57
+ Software without restriction, including without limitation the rights to use,
58
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
59
+ Software, and to permit persons to whom the Software is furnished to do so,
60
+ subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in all
63
+ copies or substantial portions of the Software.
27
64
 
28
- Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
65
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
67
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
68
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
69
+ AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
70
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,17 +1,15 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "acts_as_tree/version"
3
+ require 'acts_as_tree/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "acts_as_tree"
6
+ s.name = 'acts_as_tree'
7
7
  s.version = ActiveRecord::Acts::Tree::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"]
10
- s.homepage = "https://github.com/amerine/acts_as_tree"
11
- s.summary = %q{Provides a simple tree behaviour to active_record mdoels.}
12
- s.description = %q{Specify this acts_as extension if you want to model a tree structure by providing a parent association and a children association.}
13
-
14
- s.rubyforge_project = "acts_as_tree"
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']
10
+ s.homepage = 'https://github.com/amerine/acts_as_tree'
11
+ s.summary = %q{Provides a simple tree behaviour to active_record models.}
12
+ s.description = %q{A gem that adds simple support for organizing ActiveRecord models into parentchildren relationships.}
15
13
 
16
14
  s.files = `git ls-files`.split("\n")
17
15
  s.test_files = `git ls-files -- {test,features}/*`.split("\n")
@@ -19,9 +17,9 @@ Gem::Specification.new do |s|
19
17
  s.require_paths = ["lib"]
20
18
  s.rdoc_options = ["--charset=UTF-8"]
21
19
 
20
+ s.add_dependency "activerecord", ">= 3.0.0"
21
+
22
22
  # Dependencies (installed via 'bundle install')...
23
- s.add_development_dependency("bundler", [">= 1.0.0"])
24
- s.add_development_dependency("activerecord", [">= 1.15.4.7794"])
25
- s.add_development_dependency("rdoc")
26
- s.add_development_dependency("sqlite3")
23
+ s.add_development_dependency "sqlite3"
24
+ s.add_development_dependency "rdoc"
27
25
  end
@@ -1,3 +1,2 @@
1
1
  require 'acts_as_tree/active_record/acts/tree'
2
2
  require 'acts_as_tree/version'
3
- ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Tree }
@@ -11,6 +11,8 @@ module ActiveRecord
11
11
  # +parent_id+.
12
12
  #
13
13
  # class Category < ActiveRecord::Base
14
+ # include ActiveRecord::Acts:Tree
15
+ #
14
16
  # acts_as_tree :order => "name"
15
17
  # end
16
18
  #
@@ -53,24 +55,24 @@ module ActiveRecord
53
55
  # if set to +true+ (default: +false+).
54
56
  def acts_as_tree(options = {})
55
57
  configuration = {
56
- :foreign_key => "parent_id",
57
- :order => nil,
58
- :counter_cache => nil,
59
- :dependent => :destroy
58
+ foreign_key: "parent_id",
59
+ order: nil,
60
+ counter_cache: nil,
61
+ dependent: :destroy
60
62
  }
61
63
 
62
64
  configuration.update(options) if options.is_a?(Hash)
63
65
 
64
- belongs_to :parent, :class_name => name,
65
- :foreign_key => configuration[:foreign_key],
66
- :counter_cache => configuration[:counter_cache],
67
- :inverse_of => :children
66
+ belongs_to :parent, class_name: name,
67
+ foreign_key: configuration[:foreign_key],
68
+ counter_cache: configuration[:counter_cache],
69
+ inverse_of: :children
68
70
 
69
- has_many :children, :class_name => name,
70
- :foreign_key => configuration[:foreign_key],
71
- :order => configuration[:order],
72
- :dependent => configuration[:dependent],
73
- :inverse_of => :parent
71
+ has_many :children, class_name: name,
72
+ foreign_key: configuration[:foreign_key],
73
+ order: configuration[:order],
74
+ dependent: configuration[:dependent],
75
+ inverse_of: :parent
74
76
 
75
77
  class_eval <<-EOV
76
78
  include ActiveRecord::Acts::Tree::InstanceMethods
@@ -78,13 +80,17 @@ module ActiveRecord
78
80
  after_update :update_parents_counter_cache
79
81
 
80
82
  def self.roots
81
- find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL",
82
- :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
83
+ order_option = %Q{#{configuration.fetch :order, "nil"}}
84
+
85
+ find(:all, conditions: "#{configuration[:foreign_key]} IS NULL",
86
+ order: order_option)
83
87
  end
84
88
 
85
89
  def self.root
86
- find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL",
87
- :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
90
+ order_option = %Q{#{configuration.fetch :order, "nil"}}
91
+
92
+ find(:first, conditions: "#{configuration[:foreign_key]} IS NULL",
93
+ order: order_option)
88
94
  end
89
95
  EOV
90
96
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Acts
3
3
  module Tree
4
- VERSION = "0.2.0"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  require 'active_record'
3
- require "#{File.dirname(__FILE__)}/../init"
3
+ require 'acts_as_tree'
4
4
 
5
5
  class Test::Unit::TestCase
6
6
  def assert_queries(num = 1)
@@ -15,18 +15,18 @@ class Test::Unit::TestCase
15
15
  end
16
16
  end
17
17
 
18
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
18
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
19
19
 
20
20
  # AR keeps printing annoying schema statements
21
21
  $stdout = StringIO.new
22
22
 
23
23
  def setup_db
24
24
  ActiveRecord::Base.logger
25
- ActiveRecord::Schema.define(:version => 1) do
25
+ ActiveRecord::Schema.define(version: 1) do
26
26
  create_table :mixins do |t|
27
27
  t.column :type, :string
28
28
  t.column :parent_id, :integer
29
- t.column :children_count, :integer, :default => 0
29
+ t.column :children_count, :integer, default: 0
30
30
  end
31
31
  end
32
32
  end
@@ -38,40 +38,41 @@ def teardown_db
38
38
  end
39
39
 
40
40
  class Mixin < ActiveRecord::Base
41
+ include ActiveRecord::Acts::Tree
41
42
  end
42
43
 
43
44
  class TreeMixin < Mixin
44
- acts_as_tree :foreign_key => "parent_id", :order => "id"
45
+ acts_as_tree foreign_key: "parent_id", order: "id"
45
46
  end
46
47
 
47
48
  class TreeMixinWithoutOrder < Mixin
48
- acts_as_tree :foreign_key => "parent_id"
49
+ acts_as_tree foreign_key: "parent_id"
49
50
  end
50
51
 
51
52
  class TreeMixinNullify < Mixin
52
- acts_as_tree :foreign_key => "parent_id", :order => "id", :dependent => :nullify
53
+ acts_as_tree foreign_key: "parent_id", order: "id", dependent: :nullify
53
54
  end
54
55
 
55
56
  class TreeMixinWithCounterCache < Mixin
56
- acts_as_tree :foreign_key => "parent_id", :order => "id", :counter_cache => :children_count
57
+ acts_as_tree foreign_key: "parent_id", order: "id", counter_cache: :children_count
57
58
  end
58
59
 
59
60
  class RecursivelyCascadedTreeMixin < Mixin
60
- acts_as_tree :foreign_key => "parent_id"
61
- has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
61
+ acts_as_tree foreign_key: "parent_id"
62
+ has_one :first_child, class_name: 'RecursivelyCascadedTreeMixin', foreign_key: :parent_id
62
63
  end
63
64
 
64
65
  class TreeTest < Test::Unit::TestCase
65
66
 
66
67
  def setup
67
68
  setup_db
68
- @root1 = TreeMixin.create!
69
- @root_child1 = TreeMixin.create! :parent_id => @root1.id
70
- @child1_child = TreeMixin.create! :parent_id => @root_child1.id
71
- @child1_child_child = TreeMixin.create! :parent_id => @child1_child.id
72
- @root_child2 = TreeMixin.create! :parent_id => @root1.id
73
- @root2 = TreeMixin.create!
74
- @root3 = TreeMixin.create!
69
+ @root1 = TreeMixin.create!
70
+ @root_child1 = TreeMixin.create! parent_id: @root1.id
71
+ @child1_child = TreeMixin.create! parent_id: @root_child1.id
72
+ @child1_child_child = TreeMixin.create! parent_id: @child1_child.id
73
+ @root_child2 = TreeMixin.create! parent_id: @root1.id
74
+ @root2 = TreeMixin.create!
75
+ @root3 = TreeMixin.create!
75
76
  end
76
77
 
77
78
  def teardown
@@ -161,11 +162,14 @@ class TreeTest < Test::Unit::TestCase
161
162
  end
162
163
 
163
164
  def test_nullify
164
- root4 = TreeMixinNullify.create!
165
- root4_child = TreeMixinNullify.create! :parent_id => root4.id
165
+ root4 = TreeMixinNullify.create!
166
+ root4_child = TreeMixinNullify.create! parent_id: root4.id
167
+
166
168
  assert_equal 2, TreeMixinNullify.count
167
169
  assert_equal root4.id, root4_child.parent_id
170
+
168
171
  root4.destroy
172
+
169
173
  assert_equal 1, TreeMixinNullify.count
170
174
  assert_nil root4_child.reload.parent_id
171
175
  end
@@ -177,17 +181,17 @@ class TreeTestWithEagerLoading < Test::Unit::TestCase
177
181
  def setup
178
182
  teardown_db
179
183
  setup_db
180
- @root1 = TreeMixin.create!
181
- @root_child1 = TreeMixin.create! :parent_id => @root1.id
182
- @child1_child = TreeMixin.create! :parent_id => @root_child1.id
183
- @root_child2 = TreeMixin.create! :parent_id => @root1.id
184
- @root2 = TreeMixin.create!
185
- @root3 = TreeMixin.create!
184
+ @root1 = TreeMixin.create!
185
+ @root_child1 = TreeMixin.create! parent_id: @root1.id
186
+ @child1_child = TreeMixin.create! parent_id: @root_child1.id
187
+ @root_child2 = TreeMixin.create! parent_id: @root1.id
188
+ @root2 = TreeMixin.create!
189
+ @root3 = TreeMixin.create!
186
190
 
187
191
  @rc1 = RecursivelyCascadedTreeMixin.create!
188
- @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
189
- @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
190
- @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
192
+ @rc2 = RecursivelyCascadedTreeMixin.create! parent_id: @rc1.id
193
+ @rc3 = RecursivelyCascadedTreeMixin.create! parent_id: @rc2.id
194
+ @rc4 = RecursivelyCascadedTreeMixin.create! parent_id: @rc3.id
191
195
  end
192
196
 
193
197
  def teardown
@@ -195,8 +199,12 @@ class TreeTestWithEagerLoading < Test::Unit::TestCase
195
199
  end
196
200
 
197
201
  def test_eager_association_loading
198
- roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
202
+ roots = TreeMixin.find :all, include: :children,
203
+ conditions: "mixins.parent_id IS NULL",
204
+ order: "mixins.id"
205
+
199
206
  assert_equal [@root1, @root2, @root3], roots
207
+
200
208
  assert_no_queries do
201
209
  assert_equal 2, roots[0].children.count
202
210
  assert_equal 0, roots[1].children.count
@@ -205,17 +213,26 @@ class TreeTestWithEagerLoading < Test::Unit::TestCase
205
213
  end
206
214
 
207
215
  def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
208
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
216
+ root_node = RecursivelyCascadedTreeMixin.find :first,
217
+ include: {children: {children: :children}},
218
+ order: 'mixins.id'
219
+
209
220
  assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
210
221
  end
211
222
 
212
223
  def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
213
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
224
+ root_node = RecursivelyCascadedTreeMixin.find :first,
225
+ include: {first_child: {first_child: :first_child}},
226
+ order: 'mixins.id'
227
+
214
228
  assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
215
229
  end
216
230
 
217
231
  def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
218
- leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
232
+ leaf_node = RecursivelyCascadedTreeMixin.find :first,
233
+ include: {parent: {parent: :parent}},
234
+ order: 'mixins.id DESC'
235
+
219
236
  assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
220
237
  end
221
238
  end
@@ -233,7 +250,7 @@ class TreeTestWithoutOrder < Test::Unit::TestCase
233
250
  end
234
251
 
235
252
  def test_root
236
- assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
253
+ assert [@root1, @root2].include? TreeMixinWithoutOrder.root
237
254
  end
238
255
 
239
256
  def test_roots
@@ -244,7 +261,7 @@ end
244
261
  class UnsavedTreeTest < Test::Unit::TestCase
245
262
  def setup
246
263
  setup_db
247
- @root = TreeMixin.new
264
+ @root = TreeMixin.new
248
265
  @root_child = @root.children.build
249
266
  end
250
267
 
@@ -263,10 +280,10 @@ class TreeTestWithCounterCache < Test::Unit::TestCase
263
280
  def setup
264
281
  teardown_db
265
282
  setup_db
266
- @root = TreeMixinWithCounterCache.create!
267
- @child1 = TreeMixinWithCounterCache.create! :parent_id => @root.id
268
- @child1_child1 = TreeMixinWithCounterCache.create! :parent_id => @child1.id
269
- @child2 = TreeMixinWithCounterCache.create! :parent_id => @root.id
283
+ @root = TreeMixinWithCounterCache.create!
284
+ @child1 = TreeMixinWithCounterCache.create! parent_id: @root.id
285
+ @child1_child1 = TreeMixinWithCounterCache.create! parent_id: @child1.id
286
+ @child2 = TreeMixinWithCounterCache.create! parent_id: @root.id
270
287
  end
271
288
 
272
289
  def teardown
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,33 +12,22 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-04-09 00:00:00.000000000 Z
15
+ date: 2012-04-14 00:00:00.000000000 Z
16
16
  dependencies:
17
- - !ruby/object:Gem::Dependency
18
- name: bundler
19
- requirement: &70099568607080 !ruby/object:Gem::Requirement
20
- none: false
21
- requirements:
22
- - - ! '>='
23
- - !ruby/object:Gem::Version
24
- version: 1.0.0
25
- type: :development
26
- prerelease: false
27
- version_requirements: *70099568607080
28
17
  - !ruby/object:Gem::Dependency
29
18
  name: activerecord
30
- requirement: &70099568606560 !ruby/object:Gem::Requirement
19
+ requirement: &70099083764900 !ruby/object:Gem::Requirement
31
20
  none: false
32
21
  requirements:
33
22
  - - ! '>='
34
23
  - !ruby/object:Gem::Version
35
- version: 1.15.4.7794
36
- type: :development
24
+ version: 3.0.0
25
+ type: :runtime
37
26
  prerelease: false
38
- version_requirements: *70099568606560
27
+ version_requirements: *70099083764900
39
28
  - !ruby/object:Gem::Dependency
40
- name: rdoc
41
- requirement: &70099568606180 !ruby/object:Gem::Requirement
29
+ name: sqlite3
30
+ requirement: &70099083764300 !ruby/object:Gem::Requirement
42
31
  none: false
43
32
  requirements:
44
33
  - - ! '>='
@@ -46,10 +35,10 @@ dependencies:
46
35
  version: '0'
47
36
  type: :development
48
37
  prerelease: false
49
- version_requirements: *70099568606180
38
+ version_requirements: *70099083764300
50
39
  - !ruby/object:Gem::Dependency
51
- name: sqlite3
52
- requirement: &70099568605720 !ruby/object:Gem::Requirement
40
+ name: rdoc
41
+ requirement: &70099083763760 !ruby/object:Gem::Requirement
53
42
  none: false
54
43
  requirements:
55
44
  - - ! '>='
@@ -57,9 +46,9 @@ dependencies:
57
46
  version: '0'
58
47
  type: :development
59
48
  prerelease: false
60
- version_requirements: *70099568605720
61
- description: Specify this acts_as extension if you want to model a tree structure
62
- by providing a parent association and a children association.
49
+ version_requirements: *70099083763760
50
+ description: A gem that adds simple support for organizing ActiveRecord models into
51
+ parentchildren relationships.
63
52
  email:
64
53
  - erik.dahlstrand@gmail.com
65
54
  - mark@amerine.net
@@ -74,7 +63,6 @@ files:
74
63
  - README.md
75
64
  - Rakefile
76
65
  - acts_as_tree.gemspec
77
- - init.rb
78
66
  - lib/acts_as_tree.rb
79
67
  - lib/acts_as_tree/active_record/acts/tree.rb
80
68
  - lib/acts_as_tree/version.rb
@@ -99,11 +87,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
87
  - !ruby/object:Gem::Version
100
88
  version: '0'
101
89
  requirements: []
102
- rubyforge_project: acts_as_tree
90
+ rubyforge_project:
103
91
  rubygems_version: 1.8.11
104
92
  signing_key:
105
93
  specification_version: 3
106
- summary: Provides a simple tree behaviour to active_record mdoels.
94
+ summary: Provides a simple tree behaviour to active_record models.
107
95
  test_files:
108
96
  - test/acts_as_tree_test.rb
109
97
  has_rdoc:
data/init.rb DELETED
@@ -1,2 +0,0 @@
1
- $:.unshift "#{File.dirname(__FILE__)}/lib"
2
- require 'acts_as_tree'