mongo_mapper_acts_as_list 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'bson_ext'
4
+ gem 'mongo_mapper'
5
+ gem 'rails', '3.1.1'
6
+ gem 'ruby-debug19'
7
+ gem 'shoulda'
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require File.expand_path('../lib/mongo_mapper/plugins/version', __FILE__)
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs = %w(test)
9
+ t.pattern = 'test/*_test.rb'
10
+ end
11
+
12
+ task :default => :test
13
+
14
+
15
+
16
+ desc 'Builds the gem'
17
+ task :build do
18
+ sh "gem build mongo_mapper_acts_as_list.gemspec"
19
+ end
20
+
21
+ desc 'Builds and installs the gem'
22
+ task :install => :build do
23
+ sh "gem install mongo_mapper_acts_as_list-#{MongoMapper::Plugins::ActsAsList::Version}"
24
+ end
25
+
26
+ desc 'Tags version, pushes to remote, and pushes gem'
27
+ task :release => :build do
28
+ sh "git tag v#{MongoMapper::Plugins::ActsAsList::Version}"
29
+ sh "git push origin master"
30
+ sh "git push origin v#{MongoMapper::Plugins::ActsAsList::Version}"
31
+ sh "gem push mongo_mapper_acts_as_list-#{MongoMapper::Plugins::ActsAsList::Version}.gem"
32
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'mongo_mapper_acts_as_list'
@@ -63,6 +63,8 @@ module MongoMapper
63
63
  end
64
64
 
65
65
  #{scope_condition_method}
66
+
67
+ key '#{configuration[:column]}', Integer
66
68
 
67
69
  before_destroy :decrement_positions_on_lower_items
68
70
  before_create :add_to_list_bottom
@@ -8,7 +8,7 @@ module MongoMapper
8
8
 
9
9
 
10
10
 
11
- Version = '0.3.1'
11
+ Version = '0.3.2'
12
12
 
13
13
 
14
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper_acts_as_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo_mapper
16
- requirement: &70119513438580 !ruby/object:Gem::Requirement
16
+ requirement: &70330524729220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70119513438580
24
+ version_requirements: *70330524729220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70119513438100 !ruby/object:Gem::Requirement
27
+ requirement: &70330524728780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,12 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70119513438100
36
- description:
37
- email:
38
- - tomas.celizna@gmail.com
35
+ version_requirements: *70330524728780
36
+ description: This is a port of classic Rails' acts_as_list to Mongo Mapper. This Mongo
37
+ Mapper plugin provides the capabilities for sorting and reordering a number of objects
38
+ in a list. If you do not specify custom position column in the options, a key named
39
+ position will be added and used by default.
40
+ email: tomas.celizna@gmail.com
39
41
  executables: []
40
42
  extensions: []
41
43
  extra_rdoc_files: []
@@ -43,9 +45,10 @@ files:
43
45
  - lib/mongo_mapper/plugins/acts_as_list.rb
44
46
  - lib/mongo_mapper/plugins/version.rb
45
47
  - lib/mongo_mapper_acts_as_list.rb
46
- - test/mongo_mapper_acts_as_list_test.rb
47
- - test/test_helper.rb
48
+ - Gemfile
49
+ - Rakefile
48
50
  - README.rdoc
51
+ - init.rb
49
52
  homepage: http://github.com/tomasc/mongo_mapper_acts_as_list
50
53
  licenses: []
51
54
  post_install_message:
@@ -60,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
63
  version: '0'
61
64
  segments:
62
65
  - 0
63
- hash: 3420355153011742463
66
+ hash: -342600062179569977
64
67
  required_rubygems_version: !ruby/object:Gem::Requirement
65
68
  none: false
66
69
  requirements:
@@ -72,5 +75,5 @@ rubyforge_project:
72
75
  rubygems_version: 1.8.7
73
76
  signing_key:
74
77
  specification_version: 3
75
- summary: Port of classic Rails ActsAsList for MongoMapper
78
+ summary: Port of classic Rails ActsAsList for MongoMapper.
76
79
  test_files: []
@@ -1,362 +0,0 @@
1
- require 'test_helper'
2
-
3
-
4
-
5
- # ---------------------------------------------------------------------
6
- # CLASS SETUP
7
-
8
-
9
-
10
- class ListMixin
11
-
12
- include MongoMapper::Document
13
- include MongoMapper::Plugins::ActsAsList
14
-
15
- key :pos, Integer
16
- key :parent_id, Integer
17
- key :original_id, Integer
18
-
19
- acts_as_list :column => :pos, :scope => :parent_id
20
-
21
- end
22
-
23
- class ListMixinSub1 < ListMixin
24
- end
25
-
26
- class ListMixinSub2 < ListMixin
27
- end
28
-
29
- class ListMixinWithArrayScope
30
-
31
- include MongoMapper::Document
32
- include MongoMapper::Plugins::ActsAsList
33
-
34
- key :pos, Integer
35
- key :parent_id, Integer
36
-
37
- acts_as_list :column => :pos, :scope => [:parent_id, :original_id]
38
-
39
- end
40
-
41
-
42
-
43
- # ---------------------------------------------------------------------
44
-
45
-
46
-
47
- class ScopeTest < ActiveSupport::TestCase
48
-
49
- def setup
50
- @lm1 = ListMixin.create! :pos => 1, :parent_id => 5, :original_id => 1
51
- @lm2 = ListMixinWithArrayScope.create! :pos => 1, :parent_id => 5, :original_id => 1
52
- end
53
-
54
- def test_symbol_scope
55
- assert_equal @lm1.scope_condition, { :parent_id => 5 }
56
- assert_equal @lm2.scope_condition, { :parent_id => 5, :original_id => 1 }
57
- end
58
-
59
- end
60
-
61
-
62
-
63
- # ---------------------------------------------------------------------
64
-
65
-
66
-
67
- class ActiveSupport::TestCase
68
-
69
- private
70
-
71
- def mixins_with_parent_id(parent_id)
72
- ListMixin.where(:parent_id => parent_id).sort(:pos).all.map(&:original_id)
73
- end
74
-
75
- def mixin_with_original_id(original_id)
76
- ListMixin.where(:original_id => original_id).first
77
- end
78
- end
79
-
80
-
81
-
82
- # ---------------------------------------------------------------------
83
-
84
-
85
-
86
- class ListTest < ActiveSupport::TestCase
87
-
88
- def setup
89
- (1..4).each{ |counter| ListMixin.create! :pos => counter, :parent_id => 5, :original_id => counter }
90
- end
91
-
92
- def test_reordering
93
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
94
-
95
- mixin_with_original_id(2).move_lower
96
- assert_equal [1, 3, 2, 4], mixins_with_parent_id(5)
97
-
98
- mixin_with_original_id(2).move_higher
99
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
100
-
101
- mixin_with_original_id(1).move_to_bottom
102
- assert_equal [2, 3, 4, 1], mixins_with_parent_id(5)
103
-
104
- mixin_with_original_id(1).move_to_top
105
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
106
-
107
- mixin_with_original_id(2).move_to_bottom
108
- assert_equal [1, 3, 4, 2], mixins_with_parent_id(5)
109
-
110
- mixin_with_original_id(4).move_to_top
111
- assert_equal [4, 1, 3, 2], mixins_with_parent_id(5)
112
- end
113
-
114
- def test_move_to_bottom_with_next_to_last_item
115
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
116
- mixin_with_original_id(3).move_to_bottom
117
- assert_equal [1, 2, 4, 3], mixins_with_parent_id(5)
118
- end
119
-
120
- def test_next_prev
121
- assert_equal mixin_with_original_id(2), mixin_with_original_id(1).lower_item
122
- assert_nil mixin_with_original_id(1).higher_item
123
- assert_equal mixin_with_original_id(3), mixin_with_original_id(4).higher_item
124
- assert_nil mixin_with_original_id(4).lower_item
125
- end
126
-
127
- def test_injection
128
- item = ListMixin.new(:parent_id => 1)
129
- assert_equal item.scope_condition, {:parent_id => 1}
130
- assert_equal "pos", item.position_column
131
- end
132
-
133
- def test_insert
134
- new = ListMixin.create(:parent_id => 20)
135
- assert_equal 1, new.pos
136
- assert new.first?
137
- assert new.last?
138
-
139
- new = ListMixin.create(:parent_id => 20)
140
- assert_equal 2, new.pos
141
- assert !new.first?
142
- assert new.last?
143
-
144
- new = ListMixin.create(:parent_id => 20)
145
- assert_equal 3, new.pos
146
- assert !new.first?
147
- assert new.last?
148
-
149
- new = ListMixin.create(:parent_id => 0)
150
- assert_equal 1, new.pos
151
- assert new.first?
152
- assert new.last?
153
- end
154
-
155
- def test_insert_at
156
- new = ListMixin.create(:parent_id => 20)
157
- assert_equal 1, new.pos
158
-
159
- new = ListMixin.create(:parent_id => 20)
160
- assert_equal 2, new.pos
161
-
162
- new = ListMixin.create(:parent_id => 20)
163
- assert_equal 3, new.pos
164
-
165
- new4 = ListMixin.create(:parent_id => 20)
166
- assert_equal 4, new4.pos
167
-
168
- new4.insert_at(3)
169
- assert_equal 3, new4.pos
170
-
171
- new.reload
172
- assert_equal 4, new.pos
173
-
174
- new.insert_at(2)
175
- assert_equal 2, new.pos
176
-
177
- new4.reload
178
- assert_equal 4, new4.pos
179
-
180
- new5 = ListMixin.create(:parent_id => 20)
181
- assert_equal 5, new5.pos
182
-
183
- new5.insert_at(1)
184
- assert_equal 1, new5.pos
185
-
186
- new4.reload
187
- assert_equal 5, new4.pos
188
- end
189
-
190
- def test_delete_middle
191
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
192
-
193
- mixin_with_original_id(2).destroy
194
-
195
- assert_equal [1, 3, 4], mixins_with_parent_id(5)
196
-
197
- assert_equal 1, mixin_with_original_id(1).pos
198
- assert_equal 2, mixin_with_original_id(3).pos
199
- assert_equal 3, mixin_with_original_id(4).pos
200
-
201
- mixin_with_original_id(1).destroy
202
-
203
- assert_equal [3, 4], mixins_with_parent_id(5)
204
-
205
- assert_equal 1, mixin_with_original_id(3).pos
206
- assert_equal 2, mixin_with_original_id(4).pos
207
- end
208
-
209
- def test_nil_scope
210
- new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
211
- new2.move_higher
212
- assert_equal [new2, new1, new3], ListMixin.where(:parent_id => nil).sort(:pos).all
213
- end
214
-
215
- def test_remove_from_list_should_then_fail_in_list?
216
- assert_equal true, mixin_with_original_id(1).in_list?
217
- mixin_with_original_id(1).remove_from_list
218
- assert_equal false, mixin_with_original_id(1).in_list?
219
- end
220
-
221
- def test_remove_from_list_should_set_position_to_nil
222
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
223
-
224
- mixin_with_original_id(2).remove_from_list
225
-
226
- assert_equal [2, 1, 3, 4], mixins_with_parent_id(5)
227
-
228
- assert_equal 1, mixin_with_original_id(1).pos
229
- assert_equal nil, mixin_with_original_id(2).pos
230
- assert_equal 2, mixin_with_original_id(3).pos
231
- assert_equal 3, mixin_with_original_id(4).pos
232
- end
233
-
234
- def test_remove_before_destroy_does_not_shift_lower_items_twice
235
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5)
236
-
237
- mixin_with_original_id(2).remove_from_list
238
- mixin_with_original_id(2).destroy
239
-
240
- assert_equal [1, 3, 4], mixins_with_parent_id(5)
241
-
242
- assert_equal 1, mixin_with_original_id(1).pos
243
- assert_equal 2, mixin_with_original_id(3).pos
244
- assert_equal 3, mixin_with_original_id(4).pos
245
- end
246
-
247
- end
248
-
249
-
250
-
251
- # ---------------------------------------------------------------------
252
-
253
-
254
-
255
- class ListSubTest < ActiveSupport::TestCase
256
-
257
- def setup
258
- (1..4).each{ |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000, :original_id => i }
259
- end
260
-
261
- def test_reordering
262
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5000)
263
-
264
- mixin_with_original_id(2).move_lower
265
- assert_equal [1, 3, 2, 4], mixins_with_parent_id(5000)
266
-
267
- mixin_with_original_id(2).move_higher
268
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5000)
269
-
270
- mixin_with_original_id(1).move_to_bottom
271
- assert_equal [2, 3, 4, 1], mixins_with_parent_id(5000)
272
-
273
- mixin_with_original_id(1).move_to_top
274
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5000)
275
-
276
- mixin_with_original_id(2).move_to_bottom
277
- assert_equal [1, 3, 4, 2], mixins_with_parent_id(5000)
278
-
279
- mixin_with_original_id(4).move_to_top
280
- assert_equal [4, 1, 3, 2], mixins_with_parent_id(5000)
281
- end
282
-
283
- def test_move_to_bottom_with_next_to_last_item
284
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5000)
285
- mixin_with_original_id(3).move_to_bottom
286
- assert_equal [1, 2, 4, 3], mixins_with_parent_id(5000)
287
- end
288
-
289
- def test_next_prev
290
- assert_equal mixin_with_original_id(2), mixin_with_original_id(1).lower_item
291
- assert_nil mixin_with_original_id(1).higher_item
292
- assert_equal mixin_with_original_id(3), mixin_with_original_id(4).higher_item
293
- assert_nil mixin_with_original_id(4).lower_item
294
- end
295
-
296
- def test_injection
297
- item = ListMixin.new(:parent_id => 1)
298
- assert_equal item.scope_condition, { :parent_id => 1 }
299
- assert_equal "pos", item.position_column
300
- end
301
-
302
- def test_insert_at
303
- new = ListMixin.create(:parent_id => 20)
304
- assert_equal 1, new.pos
305
-
306
- new = ListMixinSub1.create(:parent_id => 20)
307
- assert_equal 2, new.pos
308
-
309
- new = ListMixinSub2.create(:parent_id => 20)
310
- assert_equal 3, new.pos
311
-
312
- new4 = ListMixin.create(:parent_id => 20)
313
- assert_equal 4, new4.pos
314
-
315
- new4.insert_at(3)
316
- assert_equal 3, new4.pos
317
-
318
- new.reload
319
- assert_equal 4, new.pos
320
-
321
- new.insert_at(2)
322
- assert_equal 2, new.pos
323
-
324
- new4.reload
325
- assert_equal 4, new4.pos
326
-
327
- new5 = ListMixinSub1.create(:parent_id => 20)
328
- assert_equal 5, new5.pos
329
-
330
- new5.insert_at(1)
331
- assert_equal 1, new5.pos
332
-
333
- new4.reload
334
- assert_equal 5, new4.pos
335
- end
336
-
337
- def test_delete_middle
338
- assert_equal [1, 2, 3, 4], mixins_with_parent_id(5000)
339
-
340
- mixin_with_original_id(2).destroy
341
-
342
- assert_equal [1, 3, 4], mixins_with_parent_id(5000)
343
-
344
- assert_equal 1, mixin_with_original_id(1).pos
345
- assert_equal 2, mixin_with_original_id(3).pos
346
- assert_equal 3, mixin_with_original_id(4).pos
347
-
348
- mixin_with_original_id(1).destroy
349
-
350
- assert_equal [3, 4], mixins_with_parent_id(5000)
351
-
352
- assert_equal 1, mixin_with_original_id(3).pos
353
- assert_equal 2, mixin_with_original_id(4).pos
354
- end
355
-
356
- end
357
-
358
-
359
-
360
- # ---------------------------------------------------------------------
361
-
362
-
data/test/test_helper.rb DELETED
@@ -1,32 +0,0 @@
1
- require 'rubygems'
2
- $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
3
-
4
- require 'mongo_mapper'
5
- require 'mongo_mapper/plugins/acts_as_list'
6
- require 'test/unit'
7
- require 'ruby-debug'
8
-
9
-
10
-
11
- class ActiveSupport::TestCase
12
-
13
- # Drop all collections after each test case.
14
- def teardown
15
- MongoMapper.database.collections.each { |coll| coll.remove }
16
- end
17
-
18
- # Make sure that each test case has a teardown
19
- # method to clear the db after each test.
20
- def inherited(base)
21
- base.define_method teardown do
22
- super
23
- end
24
- end
25
-
26
- end
27
-
28
-
29
-
30
- MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
31
- MongoMapper.database = "mongo_mapper_acts_as_list_test"
32
- MongoMapper.database.collections.each { |c| c.drop_indexes }