acts_as_list_mongoid 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.DS_Store +0 -0
- data/Gemfile +10 -1
- data/Rakefile +44 -10
- data/VERSION +1 -1
- data/acts_as_list_mongoid.gemspec +48 -39
- data/lib/.DS_Store +0 -0
- data/lib/acts_as_list_mongoid.rb +7 -1
- data/lib/mongoid/acts_as_list.rb +132 -143
- data/model/embedded_item.rb +4 -4
- data/model/referenced_category.rb +5 -2
- data/spec/acts_as_list/embedded_item_spec.rb +68 -72
- data/spec/acts_as_list/referenced_category_spec.rb +20 -22
- data/spec/spec_helper.rb +4 -2
- metadata +109 -70
- data/.gitignore +0 -4
data/.DS_Store
ADDED
Binary file
|
data/Gemfile
CHANGED
@@ -1,4 +1,13 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
|
-
gem 'mongoid', '>= 2.0.
|
3
|
+
gem 'mongoid', '>= 2.0.1'
|
4
4
|
gem "mongoid_embedded_helper", '>= 0.2.5'
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem "shoulda", ">= 0"
|
8
|
+
gem "cutter", ">= 0"
|
9
|
+
gem "rspec", ">= 2.5"
|
10
|
+
gem "bundler", "~> 1.0.10"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Rakefile
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
2
12
|
require 'rake'
|
3
13
|
|
4
|
-
|
5
|
-
|
6
|
-
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
7
17
|
gem.name = "acts_as_list_mongoid"
|
8
18
|
gem.summary = %Q{acts_as_list for Mongoid}
|
9
19
|
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.
|
@@ -11,12 +21,36 @@ begin
|
|
11
21
|
gem.email = "kmandrup@gmail.com"
|
12
22
|
gem.homepage = "http://github.com/rails/acts_as_list"
|
13
23
|
gem.authors = ["Kristian Mandrup"]
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
21
41
|
end
|
22
42
|
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "acts_as_list_mongoid #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.4
|
@@ -1,68 +1,77 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.2.
|
7
|
+
s.name = "acts_as_list_mongoid"
|
8
|
+
s.version = "0.2.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
|
15
|
-
s.email = %q{kmandrup@gmail.com}
|
12
|
+
s.date = "2011-09-30"
|
13
|
+
s.description = "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.\n The instances that take part in the list should have a +position+ field of type Integer."
|
14
|
+
s.email = "kmandrup@gmail.com"
|
16
15
|
s.extra_rdoc_files = [
|
17
16
|
"README.markdown"
|
18
17
|
]
|
19
18
|
s.files = [
|
20
19
|
".DS_Store",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
"spec/.rspec",
|
37
|
-
"spec/acts_as_list/embedded_item_spec.rb",
|
38
|
-
"spec/acts_as_list/referenced_category_spec.rb",
|
39
|
-
"spec/spec_helper.rb"
|
40
|
-
]
|
41
|
-
s.homepage = %q{http://github.com/rails/acts_as_list}
|
42
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
-
s.require_paths = ["lib"]
|
44
|
-
s.rubygems_version = %q{1.3.7}
|
45
|
-
s.summary = %q{acts_as_list for Mongoid}
|
46
|
-
s.test_files = [
|
20
|
+
".rspec",
|
21
|
+
"Gemfile",
|
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
|
+
"model/referenced_category.rb",
|
34
|
+
"spec/.rspec",
|
47
35
|
"spec/acts_as_list/embedded_item_spec.rb",
|
48
|
-
|
49
|
-
|
36
|
+
"spec/acts_as_list/referenced_category_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
50
38
|
]
|
39
|
+
s.homepage = "http://github.com/rails/acts_as_list"
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = "1.8.10"
|
42
|
+
s.summary = "acts_as_list for Mongoid"
|
51
43
|
|
52
44
|
if s.respond_to? :specification_version then
|
53
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
45
|
s.specification_version = 3
|
55
46
|
|
56
47
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
-
s.add_runtime_dependency(%q<mongoid>, [">= 2.0.
|
48
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 2.0.1"])
|
58
49
|
s.add_runtime_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
|
50
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<cutter>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<rspec>, [">= 2.5"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
56
|
else
|
60
|
-
s.add_dependency(%q<mongoid>, [">= 2.0.
|
57
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.1"])
|
61
58
|
s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
|
59
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
60
|
+
s.add_dependency(%q<cutter>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 2.5"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
62
65
|
end
|
63
66
|
else
|
64
|
-
s.add_dependency(%q<mongoid>, [">= 2.0.
|
67
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.1"])
|
65
68
|
s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
|
69
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
+
s.add_dependency(%q<cutter>, [">= 0"])
|
71
|
+
s.add_dependency(%q<rspec>, [">= 2.5"])
|
72
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
74
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
75
|
end
|
67
76
|
end
|
68
77
|
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/acts_as_list_mongoid.rb
CHANGED
data/lib/mongoid/acts_as_list.rb
CHANGED
@@ -1,59 +1,56 @@
|
|
1
|
-
require "mongoid"
|
1
|
+
require "mongoid"
|
2
2
|
require 'mongoid_embedded_helper'
|
3
3
|
require 'mongoid_adjust'
|
4
4
|
|
5
5
|
module ActsAsList
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
module Mongoid
|
7
|
+
class << self
|
8
|
+
attr_accessor :default_position_column
|
9
9
|
end
|
10
|
-
|
11
|
-
def self.included(klass)
|
12
|
-
klass.extend InitializerMethods
|
13
|
-
key = self.default_position_column || :position
|
14
|
-
klass.field key, :type => Integer
|
15
|
-
klass.acts_as_list :column => key.to_s
|
16
|
-
end
|
17
10
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
11
|
+
def self.included(klass)
|
12
|
+
klass.extend InitializerMethods
|
13
|
+
key = self.default_position_column || :position
|
14
|
+
klass.field key, :type => Integer
|
15
|
+
klass.acts_as_list :column => key.to_s
|
16
|
+
end
|
23
17
|
|
24
|
-
|
25
|
-
|
18
|
+
module InitializerMethods
|
19
|
+
def acts_as_list(options = {})
|
20
|
+
configuration = { :column => 'position' }
|
21
|
+
configuration.update(options) if options.is_a?(Hash)
|
22
|
+
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
26
23
|
|
27
24
|
define_method :position_column do
|
28
25
|
configuration[:column].to_s
|
29
26
|
end
|
30
27
|
|
31
28
|
if !configuration[:scope]
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
define_method :scope_condition do
|
30
|
+
{position_key.ne => nil}
|
31
|
+
end
|
35
32
|
elsif configuration[:scope].is_a?(Symbol)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
33
|
+
define_method :scope_condition do
|
34
|
+
{configuration[:scope] => self[configuration[:scope]]}
|
35
|
+
end
|
36
|
+
else
|
37
|
+
raise ArgumentError, "acts_as_list must either take a valid scope option or be in an embedded document and use the parent document as scope"
|
38
|
+
end
|
39
|
+
|
40
|
+
include ::Mongoid::EmbeddedHelper
|
41
|
+
include InstanceMethods
|
42
|
+
include Fields
|
43
|
+
include Triggers
|
44
|
+
extend Fields
|
45
|
+
extend ClassMethods
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
52
49
|
module ClassMethods
|
53
50
|
def in_scope
|
54
51
|
where(scope_condition)
|
55
|
-
end
|
56
|
-
|
52
|
+
end
|
53
|
+
|
57
54
|
def move_commands symbol
|
58
55
|
case symbol
|
59
56
|
when :symbol
|
@@ -65,15 +62,15 @@ module ActsAsList
|
|
65
62
|
end
|
66
63
|
end
|
67
64
|
end
|
68
|
-
|
69
|
-
module InstanceMethods
|
65
|
+
|
66
|
+
module InstanceMethods
|
70
67
|
def move command
|
71
68
|
if command.kind_of? Symbol
|
72
69
|
case command
|
73
70
|
when :highest, :top
|
74
71
|
move_to_top
|
75
72
|
when :lowest, :bottom
|
76
|
-
move_to_bottom
|
73
|
+
move_to_bottom
|
77
74
|
when :up, :higher
|
78
75
|
move_higher
|
79
76
|
when :down, :lower
|
@@ -81,11 +78,11 @@ module ActsAsList
|
|
81
78
|
else
|
82
79
|
raise ArgumentError, "unknown move command '#{command}', try one of #{self.class.move_commands_available}"
|
83
80
|
end
|
84
|
-
elsif command.kind_of? Hash
|
81
|
+
elsif command.kind_of? Hash
|
85
82
|
other = command.values.first
|
86
83
|
cmd = command.keys.first
|
87
84
|
case cmd
|
88
|
-
when :to
|
85
|
+
when :to
|
89
86
|
move_to(other)
|
90
87
|
when :above
|
91
88
|
move_above(other)
|
@@ -96,10 +93,10 @@ module ActsAsList
|
|
96
93
|
end
|
97
94
|
else
|
98
95
|
raise ArgumentError, "move command takes either a Symbol or Hash as an argument, not a #{command.class}"
|
99
|
-
end
|
96
|
+
end
|
100
97
|
end
|
101
|
-
|
102
|
-
|
98
|
+
|
99
|
+
|
103
100
|
def order_by_position conditions, extras = []
|
104
101
|
sub_collection = in_collection.where(conditions)
|
105
102
|
sub_collection = if embedded?
|
@@ -108,11 +105,11 @@ module ActsAsList
|
|
108
105
|
sub_collection.order_by(position_key.to_sym.asc)
|
109
106
|
end
|
110
107
|
|
111
|
-
if !extras.empty?
|
108
|
+
if !extras.empty?
|
112
109
|
sub_collection = if embedded?
|
113
|
-
sub_collection.sort do |x,y|
|
110
|
+
sub_collection.sort do |x,y|
|
114
111
|
if x.my_position == y.my_position
|
115
|
-
x.created_at <=> y.created_at
|
112
|
+
x.created_at <=> y.created_at
|
116
113
|
else
|
117
114
|
x.my_position <=> y.my_position
|
118
115
|
end
|
@@ -121,21 +118,21 @@ module ActsAsList
|
|
121
118
|
sub_collection.order_by(extras)
|
122
119
|
end
|
123
120
|
end
|
124
|
-
|
121
|
+
|
125
122
|
sub_collection
|
126
123
|
end
|
127
124
|
|
128
125
|
# conditions, { position_column => 1 }
|
129
|
-
def do_decrement( conditions, options)
|
126
|
+
def do_decrement( conditions, options)
|
130
127
|
in_collection.where(conditions).adjust! position_key => -1
|
131
128
|
end
|
132
129
|
|
133
|
-
def do_increment( conditions, options)
|
130
|
+
def do_increment( conditions, options)
|
134
131
|
in_collection.where(conditions).adjust! position_key => 1
|
135
132
|
end
|
136
|
-
|
133
|
+
|
137
134
|
def less_than_me
|
138
|
-
{ position_key.lt => my_position.to_i}
|
135
|
+
{ position_key.lt => my_position.to_i}
|
139
136
|
end
|
140
137
|
|
141
138
|
def greater_than_me
|
@@ -146,7 +143,7 @@ module ActsAsList
|
|
146
143
|
insert_in_list_at(position)
|
147
144
|
end
|
148
145
|
|
149
|
-
def move_to(position = 1)
|
146
|
+
def move_to(position = 1)
|
150
147
|
insert_in_list_at(position)
|
151
148
|
end
|
152
149
|
|
@@ -155,23 +152,23 @@ module ActsAsList
|
|
155
152
|
move_to(new_pos)
|
156
153
|
end
|
157
154
|
|
158
|
-
def move_above(object)
|
155
|
+
def move_above(object)
|
159
156
|
new_pos = ( self == object ) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position - 1 : object.my_position)
|
160
157
|
move_to(new_pos)
|
161
158
|
end
|
162
159
|
|
163
|
-
|
160
|
+
# Insert the item at the given position (defaults to the top position of 1).
|
164
161
|
def insert_in_list_at(position = 1)
|
165
162
|
insert_at_position(position)
|
166
163
|
end
|
167
164
|
|
168
165
|
# Swap positions with the next lower item, if one exists.
|
169
|
-
def move_lower
|
170
|
-
low_item = lower_item
|
166
|
+
def move_lower
|
167
|
+
low_item = lower_item
|
171
168
|
return unless low_item
|
172
169
|
|
173
|
-
|
174
|
-
increment_position
|
170
|
+
low_item.decrement_position
|
171
|
+
increment_position
|
175
172
|
end
|
176
173
|
|
177
174
|
# Swap positions with the next higher item, if one exists.
|
@@ -205,25 +202,25 @@ module ActsAsList
|
|
205
202
|
def remove_from_list
|
206
203
|
if in_list?
|
207
204
|
decrement_positions_on_lower_items
|
208
|
-
|
205
|
+
set_my_position nil
|
209
206
|
end
|
210
207
|
end
|
211
208
|
|
212
209
|
# Increase the position of this item without adjusting the rest of the list.
|
213
210
|
def increment_position
|
214
211
|
return unless in_list?
|
215
|
-
|
216
|
-
|
217
|
-
|
212
|
+
# in_collection.where(:pos => my_position).
|
213
|
+
adjust!(position_key => 1)
|
214
|
+
save!
|
218
215
|
end
|
219
216
|
|
220
217
|
# Decrease the position of this item without adjusting the rest of the list.
|
221
218
|
def decrement_position
|
222
219
|
return unless in_list?
|
223
|
-
|
224
|
-
# in_collection.where(:pos => my_position).
|
220
|
+
|
221
|
+
# in_collection.where(:pos => my_position).
|
225
222
|
adjust!(position_key => -1)
|
226
|
-
save!
|
223
|
+
save!
|
227
224
|
end
|
228
225
|
|
229
226
|
# Return +true+ if this object is the first in the list.
|
@@ -234,25 +231,23 @@ module ActsAsList
|
|
234
231
|
|
235
232
|
# Return +true+ if this object is the last in the list.
|
236
233
|
def last?
|
237
|
-
return false unless in_list?
|
234
|
+
return false unless in_list?
|
238
235
|
bottom_pos = bottom_position_in_list
|
239
236
|
my_position == bottom_pos
|
240
237
|
end
|
241
238
|
|
242
239
|
# Return the next higher item in the list.
|
243
240
|
def higher_item
|
244
|
-
return nil unless in_list?
|
245
|
-
|
246
|
-
|
241
|
+
return nil unless in_list?
|
242
|
+
conditions = scope_condition.merge!( less_than_me )
|
247
243
|
order_by_position(conditions).last
|
248
244
|
end
|
249
245
|
|
250
246
|
# Return the next lower item in the list.
|
251
247
|
def lower_item
|
252
248
|
return nil unless in_list?
|
253
|
-
|
254
|
-
|
255
|
-
|
249
|
+
|
250
|
+
conditions = scope_condition.merge!( greater_than_me )
|
256
251
|
order_by_position(conditions).first
|
257
252
|
end
|
258
253
|
|
@@ -261,33 +256,32 @@ module ActsAsList
|
|
261
256
|
!my_position.nil?
|
262
257
|
end
|
263
258
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
259
|
+
# sorts all items in the list
|
260
|
+
# if two items have same position, the one created more recently goes first
|
261
|
+
def sort
|
262
|
+
conditions = scope_condition
|
269
263
|
list_items = order_by_position(conditions, :created_at.desc).to_a
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
private
|
277
|
-
|
264
|
+
|
265
|
+
list_items.each_with_index do |list_item, index|
|
266
|
+
list_item.set_my_position index + 1
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
private
|
271
|
+
|
278
272
|
def add_to_list_top
|
279
273
|
increment_positions_on_all_items
|
280
274
|
end
|
281
275
|
|
282
|
-
def add_to_list_bottom
|
276
|
+
def add_to_list_bottom
|
283
277
|
bottom_pos = bottom_position_in_list.to_i
|
284
278
|
set_my_position(bottom_pos + 1)
|
285
279
|
end
|
286
280
|
|
287
281
|
# Overwrite this method to define the scope of the list changes
|
288
282
|
def scope_condition
|
289
|
-
|
290
|
-
|
283
|
+
{}
|
284
|
+
end
|
291
285
|
|
292
286
|
# Returns the bottom position number in the list.
|
293
287
|
# bottom_position_in_list # => 2
|
@@ -298,38 +292,38 @@ module ActsAsList
|
|
298
292
|
|
299
293
|
# Returns the bottom item
|
300
294
|
def bottom_item(except = nil)
|
301
|
-
|
295
|
+
conditions = scope_condition
|
302
296
|
if except
|
303
297
|
conditions.merge!( { position_key.ne => except.my_position } )
|
304
298
|
end
|
305
299
|
|
306
|
-
|
300
|
+
order_by_position(conditions).last
|
307
301
|
end
|
308
302
|
|
309
303
|
# Forces item to assume the bottom position in the list.
|
310
304
|
def assume_bottom_position
|
311
|
-
|
312
|
-
|
305
|
+
pos = bottom_position_in_list(self).to_i + 1
|
306
|
+
set_my_position(pos)
|
313
307
|
end
|
314
308
|
|
315
309
|
# Forces item to assume the top position in the list.
|
316
310
|
def assume_top_position
|
317
|
-
|
311
|
+
set_my_position(1)
|
318
312
|
end
|
319
313
|
|
320
314
|
# This has the effect of moving all the higher items up one.
|
321
315
|
def decrement_positions_on_higher_items(position)
|
322
|
-
|
323
|
-
|
316
|
+
conditions = scope_condition
|
317
|
+
conditions.merge!( { position_key.lt => position } )
|
324
318
|
|
325
|
-
|
319
|
+
decrease_all! in_collection.where(conditions)
|
326
320
|
end
|
327
321
|
|
328
322
|
# This has the effect of moving all the lower items up one.
|
329
323
|
def decrement_positions_on_lower_items(max_pos = nil)
|
330
324
|
return unless in_list?
|
331
|
-
|
332
|
-
|
325
|
+
conditions = scope_condition
|
326
|
+
conditions.merge!( greater_than_me )
|
333
327
|
conditions.merge!({ position_key.lt => max_pos} ) if max_pos
|
334
328
|
|
335
329
|
decrease_all! in_collection.where(conditions)
|
@@ -338,8 +332,8 @@ module ActsAsList
|
|
338
332
|
# This has the effect of moving all the higher items down one.
|
339
333
|
def increment_positions_on_higher_items(min_pos = nil)
|
340
334
|
return unless in_list?
|
341
|
-
|
342
|
-
|
335
|
+
conditions = scope_condition
|
336
|
+
conditions.merge!( less_than_me )
|
343
337
|
conditions.merge!({ position_key.gt => min_pos} ) if min_pos
|
344
338
|
|
345
339
|
increase_all! in_collection.where(conditions)
|
@@ -349,90 +343,85 @@ module ActsAsList
|
|
349
343
|
collection.adjust!(position_key => number).each{|doc| doc.save}
|
350
344
|
end
|
351
345
|
|
352
|
-
def increase_all! collection
|
346
|
+
def increase_all! collection
|
353
347
|
adjust_all! collection, 1
|
354
348
|
end
|
355
349
|
|
356
|
-
def decrease_all! collection
|
350
|
+
def decrease_all! collection
|
357
351
|
adjust_all! collection, -1
|
358
352
|
end
|
359
|
-
|
353
|
+
|
360
354
|
# This has the effect of moving all the lower items down one.
|
361
355
|
def increment_positions_on_lower_items(position)
|
362
|
-
|
363
|
-
|
356
|
+
conditions = scope_condition
|
357
|
+
conditions.merge!( { position_key.gte => position } )
|
364
358
|
|
365
|
-
|
359
|
+
increase_all! in_collection.where(conditions)
|
366
360
|
end
|
367
361
|
|
368
362
|
# Increments position (<tt>position_column</tt>) of all items in the list.
|
369
363
|
def increment_positions_on_all_items
|
370
|
-
|
371
|
-
|
372
|
-
increase_all! in_collection.where(conditions)
|
364
|
+
conditions = scope_condition
|
365
|
+
increase_all! in_collection.where(conditions)
|
373
366
|
end
|
374
367
|
|
375
368
|
def insert_at_position(position)
|
376
369
|
position = [position, 1].max
|
377
370
|
remove_from_list
|
378
371
|
increment_positions_on_lower_items(position)
|
379
|
-
|
372
|
+
set_my_position(position)
|
380
373
|
end
|
381
374
|
end
|
382
375
|
|
383
376
|
module Triggers
|
384
|
-
def after_parentize
|
377
|
+
def after_parentize
|
385
378
|
# should register on root element to be called when root is saved first time!?
|
386
|
-
end
|
379
|
+
end
|
387
380
|
|
388
381
|
def init_list_item!
|
389
382
|
self['created_at'] = Time.now
|
390
|
-
self['updated_at'] = Time.now
|
383
|
+
self['updated_at'] = Time.now
|
391
384
|
add_to_list_bottom unless in_list?
|
392
|
-
end
|
385
|
+
end
|
393
386
|
|
394
387
|
end
|
395
|
-
|
388
|
+
|
396
389
|
module Fields
|
397
|
-
def my_position
|
390
|
+
def my_position
|
398
391
|
self[position_column]
|
399
392
|
end
|
400
393
|
|
401
394
|
def set_my_position new_position
|
402
|
-
if new_position != my_position
|
403
|
-
self
|
395
|
+
if new_position != my_position
|
396
|
+
self.update_attributes position_column => new_position
|
404
397
|
save!
|
405
398
|
end
|
406
399
|
end
|
407
400
|
|
408
|
-
|
409
|
-
|
410
|
-
|
401
|
+
def [](field_name)
|
402
|
+
self.send field_name
|
403
|
+
end
|
411
404
|
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
self.send("#{key}=", value)
|
417
|
-
end
|
418
|
-
save!
|
419
|
-
end
|
405
|
+
def []=(key, value)
|
406
|
+
@attributes[key.to_s] = value
|
407
|
+
save!
|
408
|
+
end
|
420
409
|
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
410
|
+
def ==(other)
|
411
|
+
return true if other.equal?(self)
|
412
|
+
return true if other.instance_of?(self.class) and other.respond_to?('_id') and other._id == self._id
|
413
|
+
false
|
414
|
+
end
|
426
415
|
|
427
416
|
def position_key
|
428
|
-
position_column.to_sym
|
417
|
+
position_column.to_sym
|
429
418
|
end
|
430
|
-
end
|
431
|
-
end
|
419
|
+
end
|
420
|
+
end
|
432
421
|
end
|
433
422
|
|
434
|
-
class Array
|
423
|
+
class Array
|
435
424
|
def init_list!
|
436
425
|
each {|i| i.init_list_item! }
|
437
426
|
end
|
438
|
-
end
|
427
|
+
end
|