mongoid_acts_as_list 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,7 +13,7 @@ Mongoid::ActsAsList provides the ability of ordering and sorting a number of obj
13
13
  Place the following in your Gemfile:
14
14
 
15
15
  ``` ruby
16
- gem 'mongoid_acts_as_list', '~> 0.0.3'
16
+ gem 'mongoid_acts_as_list', '~> 0.0.4'
17
17
  ```
18
18
 
19
19
  Then run `bundle install`
@@ -108,6 +108,16 @@ item.last?
108
108
 
109
109
  item.next_item #=> returns the item immediately following `item` in the list
110
110
  item.previous_item #=> returns the item immediately preceding `item` in the list
111
+
112
+ ## Original ActsAsList #move_ methods
113
+
114
+ item.move_higher #=> same as item.move(:backward)
115
+ item.move_lower #=> same as item.move(:forward)
116
+ item.move_to_top #=> same as item.move(to: :start)
117
+ item.move_to_bottom #=> same as item.move(to: :end)
118
+ item.increment_position #=> increments the position number without affecting other items
119
+ item.decrement_position #=> decrements the position number without affecting other items
120
+ item.insert_at 3 #=> same as item.move(to: 3)
111
121
  ```
112
122
 
113
123
 
@@ -8,6 +8,47 @@ module Mongoid::ActsAsList
8
8
  class ScopeMissingError < RuntimeError; end
9
9
 
10
10
  module ClassMethods
11
+
12
+ # Public: class macro to enable the ActsAsList module
13
+ #
14
+ # options - a Hash of options
15
+ # :field - the name of the field to hold the position number as a Symbol or a String (optional)
16
+ # :scope - the name of the association to scope the list for (required for non-embedded models)
17
+ #
18
+ # Examples
19
+ #
20
+ # ## on a belong_to relation
21
+ #
22
+ # class List
23
+ # include Mongoid::Document
24
+ #
25
+ # has_many :items
26
+ # end
27
+ #
28
+ # class Item
29
+ # include Mongoid::Document
30
+ # include Mongoid::ActsAsList
31
+ #
32
+ # belongs_to :list
33
+ # acts_as_list scope: :list, field: :position
34
+ # end
35
+ #
36
+ #
37
+ # ## on a embedded_in relation
38
+ #
39
+ # class List
40
+ # include Mongoid::Document
41
+ #
42
+ # embeds_many :items
43
+ # end
44
+ #
45
+ # class Item
46
+ # include Mongoid::Document
47
+ # include Mongoid::ActsAsList
48
+ #
49
+ # embedded_in :list
50
+ # acts_as_list field: :num
51
+ # end
11
52
  def acts_as_list options = {}
12
53
  field = options.fetch(:field, Mongoid::ActsAsList.configuration.default_position_field).try(:to_sym)
13
54
  scope = options.fetch(:scope, nil).try(:to_sym)
@@ -138,8 +179,8 @@ module Mongoid::ActsAsList
138
179
  def move_backwards by_how_much = 1
139
180
  move_to(self[position_field] - by_how_much) unless first?
140
181
  end
141
- alias_method :move_higher , :move_backwards
142
- alias_method :move_forward, :move_forwards
182
+ alias_method :move_higher , :move_backwards
183
+ alias_method :move_backward, :move_backwards
143
184
 
144
185
  # Public: Moves the item before another one in the list
145
186
  #
@@ -216,7 +257,7 @@ module Mongoid::ActsAsList
216
257
  return unless in_list?
217
258
  items_in_list.where(position_field => self[position_field]+1).first
218
259
  end
219
- alias_method :higher_item, :next_item
260
+ alias_method :lower_item, :next_item
220
261
 
221
262
  # Public: Gets the preceding item in the list
222
263
  #
@@ -226,11 +267,10 @@ module Mongoid::ActsAsList
226
267
  return unless in_list?
227
268
  items_in_list.where(position_field => self[position_field]-1).first
228
269
  end
229
- alias_method :lower_item, :previous_item
230
-
231
- private
270
+ alias_method :higher_item, :previous_item
232
271
 
233
- # Internal: Insert at a given position in the list
272
+ # Public: Insert at a given position in the list
273
+ # for API compatibility with AR acts_as_list
234
274
  #
235
275
  # new_position - an Integer indicating the position to insert the item at
236
276
  #
@@ -240,6 +280,32 @@ module Mongoid::ActsAsList
240
280
  update_attribute(position_field, new_position)
241
281
  end
242
282
 
283
+ # Public: increments the position number without affecting other items
284
+ # for API compatibility with AR acts_as_list
285
+ def increment_position
286
+ inc(position_field, 1)
287
+ end
288
+
289
+ # Public: decrements the position number without affecting other items
290
+ # for API compatibility with AR acts_as_list
291
+ def decrement_position
292
+ inc(position_field, -1)
293
+ end
294
+
295
+ # Public: returns the default position symbol as defined in the configuration
296
+ # for API compatibility with AR acts_as_list
297
+ def default_position
298
+ Mongoid::ActsAsList.configuration.default_position_field
299
+ end
300
+
301
+ # Public: returns true if the model uses the default position field name as defined in the configuration
302
+ # for API compatibility with AR acts_as_list
303
+ def default_position?
304
+ position_field == default_position
305
+ end
306
+
307
+ private
308
+
243
309
  # Internal: Make space in the list at a given position number
244
310
  # used when moving a item to a new position in the list.
245
311
  #
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module ActsAsList
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -103,7 +103,37 @@ shared_examples_for "a list" do
103
103
  end
104
104
  end
105
105
 
106
- %w[higher_item next_item].each do |method_name|
106
+ describe "#increment_position" do
107
+ let(:item) { category.items.order_by_position.first }
108
+
109
+ it "increments the position number" do
110
+ lambda do
111
+ item.increment_position
112
+ end.should change(item, position_field).by(1)
113
+ end
114
+
115
+ it "does not reorder other items" do
116
+ item.increment_position
117
+ category.items.order_by_position.map(&position_field).should == [1,1,2]
118
+ end
119
+ end
120
+
121
+ describe "#increment_position" do
122
+ let(:item) { category.items.order_by_position.last }
123
+
124
+ it "descrements the position number" do
125
+ lambda do
126
+ item.decrement_position
127
+ end.should change(item, position_field).by(-1)
128
+ end
129
+
130
+ it "does not reorder other items" do
131
+ item.decrement_position
132
+ category.items.order_by_position.map(&position_field).should == [0,1,1]
133
+ end
134
+ end
135
+
136
+ %w[lower_item next_item].each do |method_name|
107
137
  describe "##{method_name}" do
108
138
  it "returns the next item in the list if there is one" do
109
139
  item = category.items.where(position_field => 1).first
@@ -124,7 +154,7 @@ shared_examples_for "a list" do
124
154
  end
125
155
  end
126
156
 
127
- %w[lower_item previous_item].each do |method_name|
157
+ %w[higher_item previous_item].each do |method_name|
128
158
  describe "##{method_name}" do
129
159
  it "returns the previous item in the list if there is one" do
130
160
  item = category.items.where(position_field => 1).first
@@ -511,15 +541,33 @@ shared_examples_for "a list" do
511
541
  Mongoid::ActsAsList.configure {|c| c.start_list_at = @original_start}
512
542
  end
513
543
 
514
- # it "is configurable" do
515
- # empty_category = category.class.create!
516
- # empty_category.items.should be_empty
517
- # start = 1
518
- # Mongoid::ActsAsList.configure {|c| c.start_list_at = start}
519
- # item = empty_category.items.create!
520
- # item[position_field].should == start
521
- # item = empty_category.items.create!
522
- # item[position_field].should == start+1
523
- # end
544
+ it "is configurable" do
545
+ category.items.destroy_all
546
+ start = 1
547
+ Mongoid::ActsAsList.configure {|c| c.start_list_at = start}
548
+ item = category.items.create!
549
+ item[position_field].should == start
550
+ item = category.items.create!
551
+ item[position_field].should == start+1
552
+ end
553
+ end
554
+
555
+ describe "#default_position" do
556
+ it "returns the default position field name" do
557
+ item = category.items.create!
558
+ item.default_position.should == position_field
559
+ end
560
+ end
561
+ describe "#default_position?" do
562
+ let(:item) { category.items.create! }
563
+
564
+ it "returns true if the model uses the default position field" do
565
+ item.default_position?.should == true
566
+ end
567
+
568
+ it "returns false if not" do
569
+ item.stub(:position_field).and_return :blahblah
570
+ item.default_position?.should == false
571
+ end
524
572
  end
525
573
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_acts_as_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-25 00:00:00.000000000 Z
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &74681830 !ruby/object:Gem::Requirement
16
+ requirement: &77666190 !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: :development
23
23
  prerelease: false
24
- version_requirements: *74681830
24
+ version_requirements: *77666190
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &74681400 !ruby/object:Gem::Requirement
27
+ requirement: &77665860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *74681400
35
+ version_requirements: *77665860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov
38
- requirement: &74681120 !ruby/object:Gem::Requirement
38
+ requirement: &77665580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *74681120
46
+ version_requirements: *77665580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bson_ext
49
- requirement: &74680850 !ruby/object:Gem::Requirement
49
+ requirement: &77665230 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '1.5'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *74680850
57
+ version_requirements: *77665230
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: database_cleaner
60
- requirement: &74680180 !ruby/object:Gem::Requirement
60
+ requirement: &77664690 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *74680180
68
+ version_requirements: *77664690
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry
71
- requirement: &74679440 !ruby/object:Gem::Requirement
71
+ requirement: &77663960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *74679440
79
+ version_requirements: *77663960
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mongoid
82
- requirement: &74678270 !ruby/object:Gem::Requirement
82
+ requirement: &77662720 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 2.0.1
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *74678270
90
+ version_requirements: *77662720
91
91
  description: ''
92
92
  email:
93
93
  - olivier.melcher@gmail.com