mongoid_orderable 0.9.0 → 0.9.1

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.
@@ -13,7 +13,7 @@ module Mongoid::Orderable
13
13
  }
14
14
 
15
15
  configuration.update options if options.is_a?(Hash)
16
- field configuration[:column], type: Integer
16
+ field configuration[:column], :type => Integer
17
17
  index configuration[:column]
18
18
 
19
19
  configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
@@ -36,128 +36,124 @@ module Mongoid::Orderable
36
36
  end
37
37
  end
38
38
 
39
- module InstanceMethods
39
+ def move_to! target_position
40
+ @move_to = target_position
41
+ save
42
+ end
43
+ alias_method :insert_at!, :move_to!
40
44
 
41
- def move_to! target_position
42
- @move_to = target_position
43
- save
44
- end
45
- alias_method :insert_at!, :move_to!
45
+ def move_to target_position
46
+ @move_to = target_position
47
+ end
48
+ alias_method :insert_at, :move_to
46
49
 
47
- def move_to target_position
48
- @move_to = target_position
49
- end
50
- alias_method :insert_at, :move_to
50
+ def move_to= target_position
51
+ @move_to = target_position
52
+ end
53
+ alias_method :insert_at=, :move_to=
51
54
 
52
- def move_to= target_position
53
- @move_to = target_position
55
+ [:top, :bottom].each do |symbol|
56
+ define_method "move_to_#{symbol}" do
57
+ move_to symbol
54
58
  end
55
- alias_method :insert_at=, :move_to=
56
59
 
57
- [:top, :bottom].each do |symbol|
58
- define_method "move_to_#{symbol}" do
59
- move_to symbol
60
- end
61
-
62
- define_method "move_to_#{symbol}!" do
63
- move_to! symbol
64
- end
60
+ define_method "move_to_#{symbol}!" do
61
+ move_to! symbol
65
62
  end
63
+ end
66
64
 
67
- [:higher, :lower].each do |symbol|
68
- define_method "move_#{symbol}" do
69
- move_to symbol
70
- end
71
-
72
- define_method "move_#{symbol}!" do
73
- move_to! symbol
74
- end
65
+ [:higher, :lower].each do |symbol|
66
+ define_method "move_#{symbol}" do
67
+ move_to symbol
75
68
  end
76
69
 
77
- def first?
78
- in_list? && orderable_position == 1
70
+ define_method "move_#{symbol}!" do
71
+ move_to! symbol
79
72
  end
73
+ end
80
74
 
81
- def last?
82
- in_list? && orderable_position == bottom_orderable_position
83
- end
75
+ def first?
76
+ in_list? && orderable_position == 1
77
+ end
84
78
 
85
- def in_list?
86
- !orderable_position.nil?
87
- end
79
+ def last?
80
+ in_list? && orderable_position == bottom_orderable_position
81
+ end
88
82
 
89
- def add_to_list
90
- apply_position @move_to
91
- end
83
+ def in_list?
84
+ !orderable_position.nil?
85
+ end
92
86
 
93
- def remove_from_list
94
- orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
95
- end
87
+ def add_to_list
88
+ apply_position @move_to
89
+ end
96
90
 
97
- private
91
+ def remove_from_list
92
+ orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
93
+ end
98
94
 
99
- def orderable_position
100
- send orderable_column
101
- end
95
+ private
102
96
 
103
- def orderable_position= value
104
- send "#{orderable_column}=", value
105
- end
97
+ def orderable_position
98
+ send orderable_column
99
+ end
106
100
 
107
- def orderable_scoped
108
- if embedded?
109
- send(metadata.inverse).send(metadata.name).orderable_scope(self)
110
- else
111
- self.class.orderable_scope(self)
112
- end
113
- end
101
+ def orderable_position= value
102
+ send "#{orderable_column}=", value
103
+ end
114
104
 
115
- def orderable_scope_changed?
116
- !orderable_scoped.where(:_id => _id).exists?
105
+ def orderable_scoped
106
+ if embedded?
107
+ send(metadata.inverse).send(metadata.name).orderable_scope(self)
108
+ else
109
+ self.class.orderable_scope(self)
117
110
  end
111
+ end
118
112
 
119
- def apply_position target_position
120
- if persisted? && !embedded? && orderable_scope_changed?
121
- self.class.find(_id).remove_from_list
122
- self.orderable_position = nil
123
- end
124
-
125
- return if !target_position && in_list?
113
+ def orderable_scope_changed?
114
+ !orderable_scoped.where(:_id => _id).exists?
115
+ end
126
116
 
127
- target_position = target_position_to_position target_position
117
+ def apply_position target_position
118
+ if persisted? && !embedded? && orderable_scope_changed?
119
+ self.class.find(_id).remove_from_list
120
+ self.orderable_position = nil
121
+ end
128
122
 
129
- unless in_list?
130
- orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column => 1)
131
- else
132
- orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column => 1) if target_position < orderable_position
133
- orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
134
- end
123
+ return if !target_position && in_list?
135
124
 
136
- self.orderable_position = target_position
125
+ target_position = target_position_to_position target_position
126
+
127
+ unless in_list?
128
+ orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column => 1)
129
+ else
130
+ orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column => 1) if target_position < orderable_position
131
+ orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
137
132
  end
138
133
 
139
- def target_position_to_position target_position
140
- target_position = :bottom unless target_position
134
+ self.orderable_position = target_position
135
+ end
136
+
137
+ def target_position_to_position target_position
138
+ target_position = :bottom unless target_position
141
139
 
142
- target_position = case target_position.to_sym
143
- when :top then 1
144
- when :bottom then bottom_orderable_position
145
- when :higher then orderable_position.pred
146
- when :lower then orderable_position.next
147
- end unless target_position.is_a? Numeric
140
+ target_position = case target_position.to_sym
141
+ when :top then 1
142
+ when :bottom then bottom_orderable_position
143
+ when :higher then orderable_position.pred
144
+ when :lower then orderable_position.next
145
+ end unless target_position.is_a? Numeric
148
146
 
149
- target_position = 1 if target_position < 1
150
- target_position = bottom_orderable_position if target_position > bottom_orderable_position
151
- target_position
152
- end
147
+ target_position = 1 if target_position < 1
148
+ target_position = bottom_orderable_position if target_position > bottom_orderable_position
149
+ target_position
150
+ end
153
151
 
154
- def bottom_orderable_position
155
- @bottom_orderable_position = begin
156
- max = orderable_scoped.max(orderable_column).to_i
157
- in_list? ? max : max.next
158
- end
152
+ def bottom_orderable_position
153
+ @bottom_orderable_position = begin
154
+ max = orderable_scoped.max(orderable_column).to_i
155
+ in_list? ? max : max.next
159
156
  end
160
-
161
157
  end
162
158
 
163
159
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidOrderable
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
@@ -36,6 +36,12 @@ describe Mongoid::Orderable do
36
36
  embedded_in :embeds_orderable
37
37
  end
38
38
 
39
+ class CustomizedOrderable
40
+ include Mongoid::Document
41
+ include Mongoid::Orderable
42
+
43
+ orderable :column => :pos
44
+ end
39
45
 
40
46
  describe SimpleOrderable do
41
47
  before :each do
@@ -251,4 +257,14 @@ describe Mongoid::Orderable do
251
257
 
252
258
  end
253
259
 
260
+ describe CustomizedOrderable do
261
+ it 'does not have default position field' do
262
+ CustomizedOrderable.fields.should_not have_key('position')
263
+ end
264
+
265
+ it 'should have custom pos field' do
266
+ CustomizedOrderable.fields.should have_key('pos')
267
+ end
268
+ end
269
+
254
270
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_orderable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
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: 2011-11-23 00:00:00.000000000Z
12
+ date: 2012-02-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &21250020 !ruby/object:Gem::Requirement
16
+ requirement: &69828211577180 !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: *21250020
24
+ version_requirements: *69828211577180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &21298420 !ruby/object:Gem::Requirement
27
+ requirement: &69828211576700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *21298420
35
+ version_requirements: *69828211576700
36
36
  description: Gem allows mongoid model behave as orderable list
37
37
  email:
38
38
  - kinwizard@gmail.com
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  version: '0'
76
76
  requirements: []
77
77
  rubyforge_project: mongoid_orderable
78
- rubygems_version: 1.8.6
78
+ rubygems_version: 1.8.10
79
79
  signing_key:
80
80
  specification_version: 3
81
81
  summary: Acts as list mongoid implementation