has_hierarchy 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 296f6af57a4b4997d4f63b1ad555c9723da8701b
4
- data.tar.gz: 83c399372635843bbeac74f6aa632d2eaeea0b05
3
+ metadata.gz: 86d673f21a344bfee6de33caa82e9d3828766b4c
4
+ data.tar.gz: bfd3039a2107c54291497898345bad43024413aa
5
5
  SHA512:
6
- metadata.gz: 14a6ad138900fa59c5e174e26497ede7184c8e33137f8fc724045b1fb4d2c4befc89f82ed1071f82b5507bac917f517994e592f5cefd83928579d5f6a47e4549
7
- data.tar.gz: 3df4da8267691965a285f537ab10e994596de77e3fda143223a9f00f6bb180eba0fc73368a8ef1dcac56d45092b464892dea7b5997daedcf575a8c1aff1d30f4
6
+ metadata.gz: 324bc0f6c38e8f8ca92c1f350ec204a8925c4820b8b6dbe402f65b28e5d36176e9c9b45f9663a424c51d46be9c2755f80a54d5c8fab6e01d0e494f9a0711beae
7
+ data.tar.gz: 7e1fc5c1b53e078bec6e1bc752775fd0cfd0aa6f1a0dc118f2cc9f25540c28402b9c942939f972806b465d8ad9d4d8e9ec282ecf36972d6ba3441e8b03881b1b
data/README.md CHANGED
@@ -90,8 +90,10 @@ Item.ordered.tree
90
90
  # }
91
91
  # }
92
92
 
93
- Item.find_by_path('bar/qux/quux')
93
+ quux = Item.find_by_path('bar/qux/quux')
94
94
  # => quux
95
+ quux.full_path
96
+ # => 'bar/qux/quux'
95
97
  ```
96
98
 
97
99
  Operations on nodes:
@@ -118,8 +120,8 @@ bar.descendants # => [ qux, quux, baz ]
118
120
 
119
121
  Ordering (see [has_order](https://github.com/kolesnikovde/has_order)):
120
122
  ```ruby
121
- bar.previous_siblings # => [ foo, quux ]
122
- foo.next_siblings # => [ quux, bar ]
123
+ bar.previous_siblings # => [ foo ]
124
+ foo.next_siblings # => [ bar ]
123
125
 
124
126
  foo.move_after(quux)
125
127
  Item.ordered.tree
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
 
10
10
  spec.authors = ['Kolesnikov Danil']
11
11
  spec.email = ['kolesnikovde@gmail.com']
12
- spec.description = 'Provides tree behavior to active_record models.'
13
- spec.summary = 'Provides tree behavior to active_record models.'
12
+ spec.description = 'Tree behavior for ActiveRecord models and Mongoid documents.'
13
+ spec.summary = 'Tree behavior for ActiveRecord models and Mongoid documents.'
14
14
  spec.homepage = 'https://github.com/kolesnikovde/has_hierarchy'
15
15
  spec.license = 'MIT'
16
16
 
@@ -7,6 +7,10 @@ module HasHierarchy
7
7
  after_destroy :decrement_children_counter, if: :parent_id?
8
8
  end
9
9
 
10
+ def leaf?
11
+ self[children_count_column] == 0
12
+ end
13
+
10
14
  protected
11
15
 
12
16
  def update_children_counter
@@ -3,8 +3,7 @@ module HasHierarchy
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- before_create :populate_path
7
- before_update :rebuild_subtree, if: :need_to_rebuild_subtree?
6
+ before_save :rebuild_subtree, if: :need_to_rebuild_subtree?
8
7
  end
9
8
 
10
9
  module ClassMethods
@@ -71,15 +70,16 @@ module HasHierarchy
71
70
  end
72
71
 
73
72
  def populate_path(path = nil)
74
- self.path = root? ? '' : (path || parent.path_for_children)
75
73
  end
76
74
 
77
75
  def rebuild_subtree(path = nil)
78
- populate_path(path)
76
+ self.path = root? ? '' : (path || parent.path_for_children)
79
77
 
80
- children.each do |child|
81
- child.rebuild_subtree(path_for_children)
82
- child.save!
78
+ unless new_record?
79
+ children.each do |child|
80
+ child.rebuild_subtree(path_for_children)
81
+ child.save!
82
+ end
83
83
  end
84
84
  end
85
85
 
@@ -1,3 +1,3 @@
1
1
  module HasHierarchy
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
data/lib/has_hierarchy.rb CHANGED
@@ -94,11 +94,7 @@ module HasHierarchy
94
94
 
95
95
  module InstanceMethods
96
96
  def leaf?
97
- if counter_cache = has_hierarchy_options[:counter_cache]
98
- self[counter_cache] == 0
99
- else
100
- children.empty?
101
- end
97
+ children.count == 0
102
98
  end
103
99
 
104
100
  def root?
data/spec/tree.rb CHANGED
@@ -225,8 +225,8 @@ shared_examples 'materialized path' do
225
225
  let(:new_ancestors) { [ foo ] }
226
226
 
227
227
  before do
228
- baz.parent = new_parent
229
- baz.save!
228
+ qux.parent = new_parent
229
+ qux.save!
230
230
  reload_items
231
231
  end
232
232
 
@@ -241,16 +241,12 @@ shared_examples 'materialized path' do
241
241
  end
242
242
 
243
243
  it 'changes ancestors' do
244
- expect(baz.ancestors).to match_array(new_ancestors)
244
+ expect(qux.ancestors).to match_array(new_ancestors)
245
245
  end
246
246
 
247
247
  it 'applies to all descendants' do
248
- baz.children.each do |child|
248
+ qux.children.each do |child|
249
249
  expect(child).to be_descendant_of(new_parent)
250
-
251
- child.children.each do |subchild|
252
- expect(subchild).to be_descendant_of(new_parent)
253
- end
254
250
  end
255
251
  end
256
252
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_hierarchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kolesnikov Danil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-29 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,7 +136,7 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0.2'
139
- description: Provides tree behavior to active_record models.
139
+ description: Tree behavior for ActiveRecord models and Mongoid documents.
140
140
  email:
141
141
  - kolesnikovde@gmail.com
142
142
  executables: []
@@ -192,7 +192,7 @@ rubyforge_project:
192
192
  rubygems_version: 2.2.2
193
193
  signing_key:
194
194
  specification_version: 4
195
- summary: Provides tree behavior to active_record models.
195
+ summary: Tree behavior for ActiveRecord models and Mongoid documents.
196
196
  test_files:
197
197
  - spec/has_hierarchy_spec.rb
198
198
  - spec/spec_helper.rb