dm-is-nested_set 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ lib/dm-is-nested_set.rb
8
+ lib/dm-is-nested_set/is/nested_set.rb
9
+ lib/dm-is-nested_set/is/version.rb
10
+ spec/integration/nested_set_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
@@ -1,5 +1,4 @@
1
- dm-is-nested_set
2
- ================
1
+ = dm-is-nested_set
3
2
 
4
3
  DataMapper plugin allowing the creation of nested sets from data models.
5
4
  Provides all the same functionality as dm-is-tree, plus tons more! Read on.
data/Rakefile CHANGED
@@ -1,51 +1,44 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
- require 'rake/clean'
4
- require 'rake/gempackagetask'
5
3
  require 'spec/rake/spectask'
6
4
  require 'pathname'
7
5
 
8
- CLEAN.include '{log,pkg}/'
6
+ ROOT = Pathname(__FILE__).dirname.expand_path
7
+ require ROOT + 'lib/dm-is-nested_set/is/version'
9
8
 
10
- spec = Gem::Specification.new do |s|
11
- s.name = 'dm-is-nested_set'
12
- s.version = '0.9.2'
13
- s.platform = Gem::Platform::RUBY
14
- s.has_rdoc = true
15
- s.extra_rdoc_files = %w[ README LICENSE TODO ]
16
- s.summary = 'DataMapper plugin allowing the creation of nested sets from data models'
17
- s.description = s.summary
18
- s.author = 'Sindre Aarsaether'
19
- s.email = 'sindre@identu.no'
20
- s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-is-nested_set'
21
- s.require_path = 'lib'
22
- s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
23
- s.add_dependency('dm-core', "=#{s.version}")
24
- end
9
+ AUTHOR = "Sindre Aarsaether"
10
+ EMAIL = "sindre [a] identu [d] no"
11
+ GEM_NAME = "dm-is-nested_set"
12
+ GEM_VERSION = DataMapper::Is::NestedSet::VERSION
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION]]
14
+ GEM_CLEAN = ["log", "pkg"]
15
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
+
17
+ PROJECT_NAME = "datamapper"
18
+ PROJECT_URL = "http://github.com/sam/dm-more/tree/master/dm-is-nested_set"
19
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin allowing the creation of nested sets from data models"
20
+
21
+ require ROOT.parent + 'tasks/hoe'
25
22
 
26
23
  task :default => [ :spec ]
27
24
 
28
25
  WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
26
  SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
27
 
31
- Rake::GemPackageTask.new(spec) do |pkg|
32
- pkg.gem_spec = spec
33
- end
34
-
35
- desc "Install #{spec.name} #{spec.version} (default ruby)"
28
+ desc "Install #{GEM_NAME} #{GEM_VERSION} (default ruby)"
36
29
  task :install => [ :package ] do
37
- sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
30
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
38
31
  end
39
32
 
40
- desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
33
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
41
34
  task :uninstall => [ :clobber ] do
42
- sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
35
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
43
36
  end
44
37
 
45
38
  namespace :jruby do
46
- desc "Install #{spec.name} #{spec.version} with JRuby"
39
+ desc "Install #{GEM_NAME} #{GEM_VERSION} with JRuby"
47
40
  task :install => [ :package ] do
48
- sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
41
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}, :verbose => false
49
42
  end
50
43
  end
51
44
 
@@ -24,21 +24,6 @@ module DataMapper
24
24
  belongs_to :parent, :class_name => self.name, :child_key => options[:child_key], :order => [:lft.asc]
25
25
  has n, :children, :class_name => self.name, :child_key => options[:child_key], :order => [:lft.asc]
26
26
 
27
- #before :create do
28
- # # scenarios:
29
- # # - user creates a new object and does not specify a parent
30
- # # - user creates a new object with a direct reference to a parent
31
- # # - user spawnes a new object, and then moves it to a position
32
- # if !self.parent
33
- # self.class.root ? self.move_without_saving(:into => self.class.root) : self.move_without_saving(:to => 1)
34
- # # if this is actually root, it will not move a bit (as lft is already 1)
35
- # elsif self.parent && !self.lft
36
- # # user has set a parent before saving (and without moving it anywhere). just move into that, and continue
37
- # # might be som problems here if the referenced parent is not saved.
38
- # self.move_without_saving(:into => self.parent)
39
- # end
40
- #end
41
-
42
27
  before :save do
43
28
  if self.new_record?
44
29
  if !self.parent
@@ -62,17 +47,14 @@ module DataMapper
62
47
  end
63
48
  end
64
49
 
65
- #before :update do
66
- # # scenarios:
67
- # # - user moves the object to a position
68
- # # - user has changed the parent
69
- # # - user has removed any reference to a parent
70
- # # - user sets the parent_id to something, and then use #move before saving
71
- # if (self.parent && !self.lft) || (self.parent != self.ancestor)
72
- # # if the parent is set, we try to move this into that parent, otherwise move into root.
73
- # self.parent ? self.move_without_saving(:into => self.parent) : self.move_without_saving(:into => self.class.root)
74
- # end
75
- #end
50
+ before :destroy do
51
+ self.send(:detach)
52
+ end
53
+
54
+ after_class_method :inherited do |retval,target|
55
+ target.instance_variable_set(:@nested_set_scope, @nested_set_scope.dup)
56
+ target.instance_variable_set(:@nested_set_parent, @nested_set_parent.dup)
57
+ end
76
58
 
77
59
  end
78
60
 
@@ -90,7 +72,7 @@ module DataMapper
90
72
  def root
91
73
  # TODO scoping
92
74
  # what should this return if there is a scope? always false, or node if there is only one?
93
- roots.length > 1 ? false : roots.first
75
+ roots.length > 1 ? false : first(nested_set_parent.zip([]).to_hash)
94
76
  end
95
77
 
96
78
  ##
@@ -376,6 +358,13 @@ module DataMapper
376
358
  def right_sibling
377
359
  self_and_siblings.find{|v| v.lft == rgt+1}
378
360
  end
361
+
362
+ private
363
+ def detach
364
+ offset = self.lft - self.rgt - 1
365
+ self.class.adjust_gap!(nested_set,self.rgt,offset)
366
+ self.lft,self.rgt = nil,nil
367
+ end
379
368
 
380
369
  end
381
370
 
@@ -0,0 +1,7 @@
1
+ module DataMapper
2
+ module Is
3
+ module NestedSet
4
+ VERSION = "0.9.3"
5
+ end
6
+ end
7
+ end
@@ -1,10 +1,10 @@
1
1
  require 'rubygems'
2
2
  require 'pathname'
3
3
 
4
- gem 'dm-core', '=0.9.2'
4
+ gem 'dm-core', '=0.9.3'
5
5
  require 'dm-core'
6
6
 
7
- gem 'dm-adjust', '=0.9.2'
7
+ gem 'dm-adjust', '=0.9.3'
8
8
  require 'dm-adjust'
9
9
 
10
10
  require Pathname(__FILE__).dirname.expand_path / 'dm-is-nested_set' / 'is' / 'nested_set.rb'
@@ -17,15 +17,17 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
17
17
 
18
18
  property :id, Integer, :serial => true
19
19
  property :name, String
20
+ property :class_name, Discriminator
20
21
 
21
22
  belongs_to :user
22
23
 
23
24
  is :nested_set, :scope => [:user_id]
24
25
 
25
-
26
-
27
- # convenience method only for speccing.
28
- def pos; [lft,rgt] end
26
+ def pos; [lft,rgt] end # convenience method only for speccing.
27
+ end
28
+
29
+ class CustomCategory < Category
30
+
29
31
  end
30
32
 
31
33
  def setup
@@ -36,16 +38,16 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
36
38
  @john = User.create!(:name => "john")
37
39
 
38
40
  Category.auto_migrate!
39
- electronics = Category.create!(:id => 1, :name => "Electronics")
40
- televisions = Category.create!(:id => 2, :parent_id => 1, :name => "Televisions")
41
- tube = Category.create!(:id => 3, :parent_id => 2, :name => "Tube")
42
- lcd = Category.create!(:id => 4, :parent_id => 2, :name => "LCD")
43
- plasma = Category.create!(:id => 5, :parent_id => 2, :name => "Plasma")
44
- portable_el = Category.create!(:id => 6, :parent_id => 1, :name => "Portable Electronics")
45
- mp3_players = Category.create!(:id => 7, :parent_id => 6, :name => "MP3 Players")
46
- flash = Category.create!(:id => 8, :parent_id => 7, :name => "Flash")
47
- cd_players = Category.create!(:id => 9, :parent_id => 6, :name => "CD Players")
48
- radios = Category.create!(:id => 10,:parent_id => 6, :name => "2 Way Radios")
41
+ Category.create!(:id => 1, :name => "Electronics")
42
+ Category.create!(:id => 2, :parent_id => 1, :name => "Televisions")
43
+ Category.create!(:id => 3, :parent_id => 2, :name => "Tube")
44
+ Category.create!(:id => 4, :parent_id => 2, :name => "LCD")
45
+ Category.create!(:id => 5, :parent_id => 2, :name => "Plasma")
46
+ Category.create!(:id => 6, :parent_id => 1, :name => "Portable Electronics")
47
+ Category.create!(:id => 7, :parent_id => 6, :name => "MP3 Players")
48
+ Category.create!(:id => 8, :parent_id => 7, :name => "Flash")
49
+ Category.create!(:id => 9, :parent_id => 6, :name => "CD Players")
50
+ Category.create!(:id => 10,:parent_id => 6, :name => "2 Way Radios")
49
51
  end
50
52
  end
51
53
 
@@ -75,12 +77,7 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
75
77
  # | Electronics |
76
78
  # |____________________________________________________________________________________________________|
77
79
 
78
-
79
-
80
80
  describe 'DataMapper::Is::NestedSet' do
81
- before :all do
82
-
83
- end
84
81
 
85
82
  describe 'Class#rebuild_tree_from_set' do
86
83
  it 'should reset all parent_ids correctly' do
@@ -95,13 +92,13 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
95
92
  end
96
93
  end
97
94
  end
98
-
95
+
99
96
  describe 'Class#root and #root' do
100
97
  it 'should return the toplevel node' do
101
98
  Category.root.name.should == "Electronics"
102
99
  end
103
100
  end
104
-
101
+
105
102
  describe 'Class#leaves and #leaves' do
106
103
  it 'should return all nodes without descendants' do
107
104
  repository(:default) do
@@ -113,7 +110,7 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
113
110
  end
114
111
  end
115
112
  end
116
-
113
+
117
114
  describe '#ancestor, #ancestors and #self_and_ancestors' do
118
115
  it 'should return ancestors in an array' do
119
116
  repository(:default) do |repos|
@@ -126,7 +123,7 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
126
123
  end
127
124
  end
128
125
  end
129
-
126
+
130
127
  describe '#children' do
131
128
  it 'should return children of node' do
132
129
  repository(:default) do |repos|
@@ -141,14 +138,17 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
141
138
  end
142
139
  end
143
140
 
141
+ Category.get(1).children
142
+
144
143
  describe '#descendants and #self_and_descendants' do
145
144
  it 'should return all subnodes of node' do
146
145
  repository(:default) do
147
- r = Category.root
146
+ r = Category.get(1)
148
147
  r.self_and_descendants.length.should == 10
149
148
  r.descendants.length.should == 9
150
-
149
+ r.name.should == "Electronics"
151
150
  t = r.children[1]
151
+ t.name.should == "Portable Electronics"
152
152
  t.descendants.length.should == 4
153
153
  t.descendants.map{|a|a.name}.should == ["MP3 Players","Flash","CD Players","2 Way Radios"]
154
154
  end
@@ -170,7 +170,7 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
170
170
  end
171
171
 
172
172
  describe '#move' do
173
-
173
+
174
174
  # Outset:
175
175
  # id | lft| rgt| title
176
176
  #========================================
@@ -184,128 +184,140 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
184
184
  # 8 | 12 | 13 | - Flash
185
185
  # 9 | 15 | 16 | - CD Players
186
186
  # 10 | 17 | 18 | - 2 Way Radios
187
-
188
-
187
+
189
188
  it 'should move items correctly with :higher / :highest / :lower / :lowest' do
190
189
  repository(:default) do |repos|
191
-
190
+
192
191
  Category.get(4).pos.should == [5,6]
193
-
192
+
194
193
  Category.get(4).move(:above => Category.get(3))
195
194
  Category.get(4).pos.should == [3,4]
196
-
195
+
197
196
  Category.get(4).move(:higher).should == false
198
197
  Category.get(4).pos.should == [3,4]
199
198
  Category.get(3).pos.should == [5,6]
200
199
  Category.get(4).right_sibling.should == Category.get(3)
201
-
200
+
202
201
  Category.get(4).move(:lower)
203
202
  Category.get(4).pos.should == [5,6]
204
203
  Category.get(4).left_sibling.should == Category.get(3)
205
204
  Category.get(4).right_sibling.should == Category.get(5)
206
-
205
+
207
206
  Category.get(4).move(:highest)
208
207
  Category.get(4).pos.should == [3,4]
209
208
  Category.get(4).move(:higher).should == false
210
-
209
+
211
210
  Category.get(4).move(:lowest)
212
211
  Category.get(4).pos.should == [7,8]
213
212
  Category.get(4).left_sibling.should == Category.get(5)
214
-
213
+
215
214
  Category.get(4).move(:higher) # should reset the tree to how it was
216
-
215
+
217
216
  end
218
217
  end
219
-
218
+
220
219
  it 'should move items correctly with :indent / :outdent' do
221
220
  repository(:default) do |repos|
222
-
221
+
223
222
  mp3_players = Category.get(7)
224
-
223
+
225
224
  portable_electronics = Category.get(6)
226
225
  televisions = Category.get(2)
227
-
226
+
228
227
  mp3_players.pos.should == [11,14]
229
228
  #mp3_players.descendants.length.should == 1
230
-
229
+
231
230
  # The category is at the top of its parent, should not be able to indent.
232
231
  mp3_players.move(:indent).should == false
233
-
232
+
234
233
  mp3_players.move(:outdent)
235
-
234
+
236
235
  mp3_players.pos.should == [16,19]
237
236
  mp3_players.left_sibling.should == portable_electronics
238
-
237
+
239
238
  mp3_players.move(:higher) # Move up above Portable Electronics
240
-
239
+
241
240
  mp3_players.pos.should == [10,13]
242
241
  mp3_players.left_sibling.should == televisions
243
242
  end
244
243
  end
245
244
  end
246
-
245
+
247
246
  describe 'moving objects with #move_* #and place_node_at' do
248
247
  it 'should set left/right when choosing a parent' do
249
248
  repository(:default) do |repos|
250
249
  Category.auto_migrate!
251
-
250
+
252
251
  c1 = Category.create!(:name => "New Electronics")
253
-
252
+
254
253
  c2 = Category.create!(:name => "OLED TVs")
255
-
254
+
256
255
  c1.pos.should == [1,4]
257
256
  c1.root.should == c1
258
257
  c2.pos.should == [2,3]
259
-
258
+
260
259
  c3 = Category.create(:name => "Portable Electronics")
261
260
  c3.parent=c1
262
261
  c3.save
263
-
262
+
264
263
  c1.pos.should == [1,6]
265
264
  c2.pos.should == [2,3]
266
265
  c3.pos.should == [4,5]
267
-
266
+
268
267
  c3.parent=c2
269
268
  c3.save
270
-
269
+
271
270
  c1.pos.should == [1,6]
272
271
  c2.pos.should == [2,5]
273
272
  c3.pos.should == [3,4]
274
-
273
+
275
274
  c3.parent=c1
276
275
  c3.move(:into => c2)
277
-
276
+
278
277
  c1.pos.should == [1,6]
279
278
  c2.pos.should == [2,5]
280
279
  c3.pos.should == [3,4]
281
-
280
+
282
281
  c4 = Category.create(:name => "Tube", :parent => c2)
283
282
  c5 = Category.create(:name => "Flatpanel", :parent => c2)
284
-
283
+
285
284
  c1.pos.should == [1,10]
286
285
  c2.pos.should == [2,9]
287
286
  c3.pos.should == [3,4]
288
287
  c4.pos.should == [5,6]
289
288
  c5.pos.should == [7,8]
290
-
289
+
291
290
  c5.move(:above => c3)
292
291
  c3.pos.should == [5,6]
293
292
  c4.pos.should == [7,8]
294
293
  c5.pos.should == [3,4]
295
-
294
+
296
295
  end
297
296
  end
298
297
  end
299
-
298
+
300
299
  describe 'scoping' do
301
300
  it 'should detach from list when changing scope' do
302
- setup
303
- plasma = Category.get(5)
304
- plasma.pos.should == [7,8]
305
- plasma.user_id = 1
306
- plasma.save
307
-
308
- plasma.pos.should == [1,2]
301
+ repository(:default) do |repos|
302
+ setup
303
+ plasma = Category.get(5)
304
+ plasma.pos.should == [7,8]
305
+ plasma.user_id = 1
306
+ plasma.save
307
+ plasma.pos.should == [1,2]
308
+ end
309
+ end
310
+ end
311
+
312
+ describe 'integrity' do
313
+ it 'should detach object from list when deleted' do
314
+ repository(:default) do |repos|
315
+ setup
316
+ lcd = Category.get(4)
317
+ lcd.pos.should == [5,6]
318
+ Category.get(3).destroy
319
+ lcd.pos.should == [3,4]
320
+ end
309
321
  end
310
322
  end
311
323
 
data/spec/spec_helper.rb CHANGED
@@ -10,10 +10,11 @@ def load_driver(name, default_uri)
10
10
  lib = "do_#{name}"
11
11
 
12
12
  begin
13
- gem lib, '=0.9.2'
13
+ gem lib, '=0.9.3'
14
14
  require lib
15
15
  DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
16
16
  DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
17
+ DataObjects::Sqlite3.logger = DataObjects::Logger.new(Pathname(__FILE__).dirname+'dm.log',0)
17
18
  true
18
19
  rescue Gem::LoadError => e
19
20
  warn "Could not load #{lib}: #{e}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-is-nested_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sindre Aarsaether
@@ -9,43 +9,59 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-25 00:00:00 -05:00
12
+ date: 2008-07-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dm-core
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
20
21
  - - "="
21
22
  - !ruby/object:Gem::Version
22
- version: 0.9.2
23
+ version: 0.9.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.0
23
34
  version:
24
35
  description: DataMapper plugin allowing the creation of nested sets from data models
25
- email: sindre@identu.no
36
+ email:
37
+ - sindre [a] identu [d] no
26
38
  executables: []
27
39
 
28
40
  extensions: []
29
41
 
30
42
  extra_rdoc_files:
31
- - README
43
+ - README.txt
32
44
  - LICENSE
33
45
  - TODO
34
46
  files:
35
- - lib/dm-is-nested_set/is/nested_set.rb
47
+ - History.txt
48
+ - LICENSE
49
+ - Manifest.txt
50
+ - README.txt
51
+ - Rakefile
52
+ - TODO
36
53
  - lib/dm-is-nested_set.rb
54
+ - lib/dm-is-nested_set/is/nested_set.rb
55
+ - lib/dm-is-nested_set/is/version.rb
37
56
  - spec/integration/nested_set_spec.rb
38
- - spec/spec_helper.rb
39
57
  - spec/spec.opts
40
- - Rakefile
41
- - README
42
- - LICENSE
43
- - TODO
58
+ - spec/spec_helper.rb
44
59
  has_rdoc: true
45
60
  homepage: http://github.com/sam/dm-more/tree/master/dm-is-nested_set
46
61
  post_install_message:
47
- rdoc_options: []
48
-
62
+ rdoc_options:
63
+ - --main
64
+ - README.txt
49
65
  require_paths:
50
66
  - lib
51
67
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -62,8 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
78
  version:
63
79
  requirements: []
64
80
 
65
- rubyforge_project:
66
- rubygems_version: 1.0.1
81
+ rubyforge_project: datamapper
82
+ rubygems_version: 1.2.0
67
83
  signing_key:
68
84
  specification_version: 2
69
85
  summary: DataMapper plugin allowing the creation of nested sets from data models