acts_as_list_mongoid 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ .bundle
4
+ vendor
@@ -47,12 +47,33 @@ See the /specs folder specs that demontrate the API. Usage examples are located
47
47
  todo_item = Item.new(:name => name)
48
48
  todo_list.items << todo_item
49
49
  end
50
- todo_list.items.created! # IMPORTANT!!!
50
+ todo_list.items.init_list! # IMPORTANT!!!
51
51
 
52
- todo_list.items.first.move_to_bottom
53
- todo_list.items.last.move_higher
52
+ todo_list.items.first.move(:bottom)
53
+ todo_list.items.last.move(:higher)
54
54
  </pre>
55
-
55
+
56
+ ### List initialization
57
+
58
+ In order for the list items to be initialized properly, it is necessary to call the method <code>init_list!</code> on the
59
+ collection in order for the position of each list item to be set to an initial position.
60
+
61
+ +Example:+
62
+ <code>todo_list.items.init_list!</code>
63
+
64
+ ## New move API borrowed from Data Mapper *in-list* plugin
65
+
66
+ <pre>
67
+ item.move(:highest) # moves to top of list.
68
+ item.move(:lowest) # moves to bottom of list.
69
+ item.move(:top) # moves to top of list.
70
+ item.move(:bottom) # moves to bottom of list.
71
+ item.move(:up) # moves one up (:higher and :up is the same) within the scope.
72
+ item.move(:down) # moves one up (:lower and :down is the same) within the scope.
73
+ item.move(:to => position) # moves item to a specific position.
74
+ item.move(:above => other) # moves item above the other item.*
75
+ item.move(:below => other)
76
+ <pre>
56
77
 
57
78
  ## Running the specs
58
79
 
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "acts_as_list_mongoid"
8
- gem.summary = %Q{Gem version of acts_as_list for Mongoid}
8
+ gem.summary = %Q{acts_as_list for Mongoid}
9
9
  gem.description = %Q{Make your Mongoid model acts as a list. This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list.
10
10
  The instances that take part in the list should have a +position+ field of type Integer.}
11
11
  gem.email = "kmandrup@gmail.com"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_list_mongoid}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2010-07-06}
13
+ s.description = %q{Make your Mongoid model acts as a list. This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list.
14
+ The instances that take part in the list should have a +position+ field of type Integer.}
15
+ s.email = %q{kmandrup@gmail.com}
16
+ s.extra_rdoc_files = [
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".DS_Store",
21
+ ".gitignore",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "acts_as_list_mongoid.gemspec",
26
+ "example/example.rb",
27
+ "init.rb",
28
+ "lib/.DS_Store",
29
+ "lib/acts_as_list_mongoid.rb",
30
+ "lib/init.rb",
31
+ "lib/mongoid/acts_as_list.rb",
32
+ "model/embedded_item.rb",
33
+ "spec/.rspec",
34
+ "spec/acts_as_list/embedded_item_spec.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/rails/acts_as_list}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{acts_as_list for Mongoid}
43
+ s.test_files = [
44
+ "spec/acts_as_list/embedded_item_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
54
+ s.add_runtime_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
55
+ else
56
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
57
+ s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
61
+ s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
62
+ end
63
+ end
64
+
@@ -47,9 +47,53 @@ module ActsAsList
47
47
  def in_scope
48
48
  where(scope_condition)
49
49
  end
50
+
51
+ def move_commands symbol
52
+ case symbol
53
+ when :symbol
54
+ [:highest, :top, :lowest, :bottom, :up, :higher, :down, :lower]
55
+ when :hash
56
+ [:to, :above, :below]
57
+ else
58
+ raise ArgumentError, "no move_commands defined for: #{symbol}"
59
+ end
60
+ end
50
61
  end
51
62
 
52
- module InstanceMethods
63
+ module InstanceMethods
64
+ def move command
65
+ if command.kind_of? Symbol
66
+ case command
67
+ when :highest, :top
68
+ move_to_top
69
+ when :lowest, :bottom
70
+ move_to_bottom
71
+ when :up, :higher
72
+ move_higher
73
+ when :down, :lower
74
+ move_lower
75
+ else
76
+ raise ArgumentError, "unknown move command '#{command}', try one of #{self.class.move_commands_available}"
77
+ end
78
+ elsif command.kind_of? Hash
79
+ other = command.values.first
80
+ cmd = command.keys.first
81
+ case cmd
82
+ when :to
83
+ move_to(other)
84
+ when :above
85
+ move_above(other)
86
+ when :below
87
+ move_below(other)
88
+ else
89
+ raise ArgumentError, "Hash command #{cmd.inspect} not valid, must be one of"
90
+ end
91
+ else
92
+ raise ArgumentError, "move command takes either a Symbol or Hash as an argument, not a #{command.class}"
93
+ end
94
+ end
95
+
96
+
53
97
  def order_by_position conditions, extras = []
54
98
  sub_collection = in_collection.where(conditions)
55
99
  sub_collection = if embedded?
@@ -96,6 +140,20 @@ module ActsAsList
96
140
  insert_in_list_at(position)
97
141
  end
98
142
 
143
+ def move_to(position = 1)
144
+ insert_in_list_at(position)
145
+ end
146
+
147
+ def move_below(object)
148
+ new_pos = ( (self == object) or (object.my_position < self.my_position) ) ? self.my_position : object.my_position
149
+ move_to(new_pos)
150
+ end
151
+
152
+ def move_above(object)
153
+ new_pos = ( self == object or (object.my_position < self.my_position) ) ? self.my_position : object.my_position - 1
154
+ move_to(new_pos)
155
+ end
156
+
99
157
  # Insert the item at the given position (defaults to the top position of 1).
100
158
  def insert_in_list_at(position = 1)
101
159
  insert_at_position(position)
@@ -257,23 +315,26 @@ module ActsAsList
257
315
  def decrement_positions_on_higher_items(position)
258
316
  conditions = scope_condition
259
317
  conditions.merge!( { position_key.lt => position } )
260
- in_collection.where(conditions).adjust! position_key => 1
318
+
319
+ decrease_all! in_collection.where(conditions)
261
320
  end
262
321
 
263
322
  # This has the effect of moving all the lower items up one.
264
- def decrement_positions_on_lower_items
323
+ def decrement_positions_on_lower_items(max_pos = nil)
265
324
  return unless in_list?
266
325
  conditions = scope_condition
267
326
  conditions.merge!( greater_than_me )
327
+ conditions.merge!({ position_key.lt => max_pos} ) if max_pos
268
328
 
269
329
  decrease_all! in_collection.where(conditions)
270
330
  end
271
331
 
272
332
  # This has the effect of moving all the higher items down one.
273
- def increment_positions_on_higher_items
333
+ def increment_positions_on_higher_items(min_pos = nil)
274
334
  return unless in_list?
275
335
  conditions = scope_condition
276
336
  conditions.merge!( less_than_me )
337
+ conditions.merge!({ position_key.gt => min_pos} ) if min_pos
277
338
 
278
339
  increase_all! in_collection.where(conditions)
279
340
  end
@@ -306,6 +367,7 @@ module ActsAsList
306
367
  end
307
368
 
308
369
  def insert_at_position(position)
370
+ position = [position, 1].max
309
371
  remove_from_list
310
372
  increment_positions_on_lower_items(position)
311
373
  set_my_position(position)
@@ -317,7 +379,7 @@ module ActsAsList
317
379
  # should register on root element to be called when root is saved first time!?
318
380
  end
319
381
 
320
- def created!
382
+ def init_list_item!
321
383
  self['created_at'] = Time.now
322
384
  self['updated_at'] = Time.now
323
385
  add_to_list_bottom unless in_list?
@@ -364,7 +426,7 @@ module ActsAsList
364
426
  end
365
427
 
366
428
  class Array
367
- def created!
368
- each {|i| i.created! }
429
+ def init_list!
430
+ each {|i| i.init_list_item! }
369
431
  end
370
432
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  require 'acts_as_list_mongoid'
4
4
  require 'embedded_item'
@@ -13,7 +13,7 @@ describe 'ActsAsList for Mongoid' do
13
13
  end
14
14
  @list.save!
15
15
 
16
- @list.items.created!
16
+ @list.items.init_list!
17
17
  end
18
18
 
19
19
  after :each do
@@ -46,21 +46,56 @@ describe 'ActsAsList for Mongoid' do
46
46
  @list.items.where(:number => 2).first.move_lower
47
47
  get_positions(@list).should == [1, 3, 2, 4]
48
48
  end
49
+
50
+ it "move :down should move item 2 to position 3" do
51
+ @list.items.where(:number => 2).first.move(:down)
52
+ get_positions(@list).should == [1, 3, 2, 4]
53
+ end
54
+
55
+ it "move :lower should move item 2 to position 3" do
56
+ @list.items.where(:number => 2).first.move(:lower)
57
+ get_positions(@list).should == [1, 3, 2, 4]
58
+ end
49
59
 
50
60
  it "should move item 2 to position 1" do
51
61
  @list.items.where(:number => 2).first.move_higher
52
62
  get_positions(@list).should == [2, 1, 3, 4]
53
63
  end
64
+
65
+ it "move :up should move item 2 to position 1" do
66
+ @list.items.where(:number => 2).first.move(:up)
67
+ get_positions(@list).should == [2, 1, 3, 4]
68
+ end
69
+
70
+ it "move :higher should move item 2 to position 1" do
71
+ @list.items.where(:number => 2).first.move(:higher)
72
+ get_positions(@list).should == [2, 1, 3, 4]
73
+ end
54
74
 
55
75
  it "should move item 1 to bottom" do
56
76
  @list.items.where(:number => 1).first.move_to_bottom
57
77
  get_positions(@list).should == [2, 3, 4, 1]
58
78
  end
79
+
80
+ it "move :lowest should move item 1 to bottom" do
81
+ @list.items.where(:number => 1).first.move(:lowest)
82
+ get_positions(@list).should == [2, 3, 4, 1]
83
+ end
59
84
 
60
85
  it "should move item 1 to top" do
61
86
  @list.items.where(:number => 1).first.move_to_top
62
- get_positions(@list).should == [1, 2, 3, 4]
87
+ get_positions(@list).should == [1, 2, 3, 4]
88
+ end
89
+
90
+ it "move :highest should move item 1 to top" do
91
+ @list.items.where(:number => 1).first.move(:highest)
92
+ get_positions(@list).should == [1, 2, 3, 4]
93
+ end
94
+
95
+ it "move :unknown should cause argument error" do
96
+ lambda {@list.items.where(:number => 1).first.move(:unknown)}.should raise_error
63
97
  end
98
+
64
99
 
65
100
  it "should move item 2 to bottom" do
66
101
  @list.items.where(:number => 2).first.move_to_bottom
@@ -73,12 +108,38 @@ describe 'ActsAsList for Mongoid' do
73
108
  end
74
109
 
75
110
  it "should move item 3 to bottom" do
76
- get_positions(@list).should == [1, 2, 3, 4]
77
-
78
111
  @list.items.where(:number => 3).first.move_to_bottom
79
112
  get_positions(@list).should == [1, 2, 4, 3]
80
113
 
81
114
  end
115
+
116
+ it "items[2].move_to(4) should move item 2 to position 4" do
117
+ @list.items.where(:number => 2).first.move_to(4)
118
+ get_positions(@list).should == [1, 3, 4, 2]
119
+ end
120
+
121
+ it "items[2].insert_at(3) should move item 2 to position 3" do
122
+ @list.items.where(:number => 2).first.insert_at(3)
123
+ get_positions(@list).should == [1, 3, 2, 4]
124
+ end
125
+
126
+ it "items[2].move(:to => 3) should move item 2 to position 3" do
127
+ @list.items.where(:number => 2).first.move(:to => 3)
128
+ get_positions(@list).should == [1, 3, 2, 4]
129
+ end
130
+
131
+ it "items[1].move_below(item[2]) should move item 1 to position 2" do
132
+ item2 = @list.items.where(:number => 2).first
133
+ @list.items.where(:number => 1).first.move_below(item2)
134
+ get_positions(@list).should == [2, 1, 3, 4]
135
+ end
136
+
137
+ it "items[1].move_above(item[3]) should move item 1 to position 2" do
138
+ item3 = @list.items.where(:number => 3).first
139
+ @list.items.where(:number => 1).first.move_above(item3)
140
+ get_positions(@list).should == [2, 1, 3, 4]
141
+ end
142
+
82
143
  end
83
144
 
84
145
  describe 'relative position queries' do
@@ -99,6 +160,6 @@ describe 'ActsAsList for Mongoid' do
99
160
  it "should not find item lower than item 4" do
100
161
  @list.items.where(:pos => 4).first.lower_item.should == nil
101
162
  end
102
- end
163
+ end
103
164
  end
104
165
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -60,9 +60,11 @@ extra_rdoc_files:
60
60
  - README.markdown
61
61
  files:
62
62
  - .DS_Store
63
+ - .gitignore
63
64
  - README.markdown
64
65
  - Rakefile
65
66
  - VERSION
67
+ - acts_as_list_mongoid.gemspec
66
68
  - example/example.rb
67
69
  - init.rb
68
70
  - lib/.DS_Store
@@ -71,7 +73,7 @@ files:
71
73
  - lib/mongoid/acts_as_list.rb
72
74
  - model/embedded_item.rb
73
75
  - spec/.rspec
74
- - spec/acts_as_list/embedded/custom_embedded_spec.rb
76
+ - spec/acts_as_list/embedded_item_spec.rb
75
77
  - spec/spec.opts
76
78
  - spec/spec_helper.rb
77
79
  has_rdoc: true
@@ -105,7 +107,7 @@ rubyforge_project:
105
107
  rubygems_version: 1.3.7
106
108
  signing_key:
107
109
  specification_version: 3
108
- summary: Gem version of acts_as_list for Mongoid
110
+ summary: acts_as_list for Mongoid
109
111
  test_files:
110
- - spec/acts_as_list/embedded/custom_embedded_spec.rb
112
+ - spec/acts_as_list/embedded_item_spec.rb
111
113
  - spec/spec_helper.rb