mongoid-sleeping_king_studios 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -3
- data/README.md +12 -8
- data/lib/mongoid/sleeping_king_studios/orderable/metadata.rb +39 -41
- data/lib/mongoid/sleeping_king_studios/orderable.rb +44 -32
- data/lib/mongoid/sleeping_king_studios/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 287eebf704b95b0ecc4b7d38feecd57d335f743c
|
4
|
+
data.tar.gz: 92be00de6a85145ad88e07bb74b83e33a4b14f72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b389a9cead7baeb1462535a446a89d772b9bb438e48fdd8dc8e08e0a30722f671d1be7e453eddc6ad260cab6302918079b12611425addfcc6d59afe4fda918c5
|
7
|
+
data.tar.gz: 8d093d8555253a54afae3a8924023ad422e1f5c2d44c303400a8c126988164f4e3380d30685139ce25add4d5c6eb13022829869d8061a6c57b9437c80a73a087
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.7.6
|
4
|
+
|
5
|
+
### New Features
|
6
|
+
|
7
|
+
* Moved Orderable finders into included/extended modules, so classes including
|
8
|
+
the concern can override the finders and use super() to call the originals.
|
9
|
+
Also adds optional scope parameter to finders, which filters the results and
|
10
|
+
defaults to base class ::all.
|
11
|
+
|
12
|
+
### Resolved Issues
|
13
|
+
|
14
|
+
* Add unique relation names for each ordering, fixing some undefined behavior
|
15
|
+
when multiple orderings were defined on the same model.
|
16
|
+
|
3
17
|
## 0.7.5
|
4
18
|
|
5
|
-
|
19
|
+
### New Features
|
6
20
|
|
7
21
|
* Add #first and #last helpers to Orderable concern.
|
8
22
|
|
@@ -22,13 +36,13 @@
|
|
22
36
|
|
23
37
|
## 0.7.1
|
24
38
|
|
25
|
-
|
39
|
+
### New Features
|
26
40
|
|
27
41
|
* Add #next and #prev helpers to Orderable concern.
|
28
42
|
|
29
43
|
## 0.7.0
|
30
44
|
|
31
|
-
|
45
|
+
### New Features
|
32
46
|
|
33
47
|
* Add Orderable concern.
|
34
48
|
|
data/README.md
CHANGED
@@ -108,21 +108,25 @@ via the :as option (see below). For example, providing :as =>
|
|
108
108
|
:alphabetical_order will generate helpers \#next_alphabetical,
|
109
109
|
\#prev_alphabetical, and ::reorder_alphabetical!.
|
110
110
|
|
111
|
-
##### \#
|
111
|
+
##### \#next_ordering_name
|
112
112
|
|
113
|
-
Finds the
|
113
|
+
Finds the next document, based on the stored ordering values. Takes an optional
|
114
|
+
scope parameter to filter the results.
|
114
115
|
|
115
|
-
##### \#
|
116
|
+
##### \#prev_ordering_name
|
116
117
|
|
117
|
-
Finds the
|
118
|
+
Finds the previous document, based on the stored ordering values. Takes an
|
119
|
+
optional scope parameter to filter the results.
|
118
120
|
|
119
|
-
#####
|
121
|
+
##### \::first_ordering_name
|
120
122
|
|
121
|
-
Finds the
|
123
|
+
(Class Method) Finds the first document, based on the stored ordering values.
|
124
|
+
Takes an optional scope parameter to filter the results.
|
122
125
|
|
123
|
-
#####
|
126
|
+
##### \::last_ordering_name
|
124
127
|
|
125
|
-
Finds the
|
128
|
+
(Class Method) Finds the last document, based on the stored ordering values.
|
129
|
+
Takes an optional scope parameter to filter the results.
|
126
130
|
|
127
131
|
##### ::reorder_ordering_name!
|
128
132
|
|
@@ -6,30 +6,51 @@ module Mongoid::SleepingKingStudios
|
|
6
6
|
module Orderable
|
7
7
|
# Stores information about an Orderable concern.
|
8
8
|
class Metadata < Mongoid::SleepingKingStudios::Concern::Metadata
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
class << self
|
10
|
+
def default_field_name sort_params
|
11
|
+
(sort_params.map { |key, value|
|
12
|
+
"#{key}_#{value == 1 ? 'asc' : 'desc'}"
|
13
|
+
}.join('_') + '_order').intern
|
14
|
+
end # class method default_field_name
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
def normalize_sort_params sort_params
|
17
|
+
case sort_params
|
18
|
+
when Array
|
19
|
+
sort_params.reduce({}) do |hsh, param|
|
20
|
+
hsh.merge parse_sort_param(param)
|
21
|
+
end # each
|
22
|
+
when Hash
|
23
|
+
sort_params.each.with_object({}) do |(key, value), hsh|
|
24
|
+
hsh[key] = parse_sort_direction(value)
|
25
|
+
end # each
|
26
|
+
when Symbol, Origin::Key
|
27
|
+
parse_sort_param(sort_params)
|
28
|
+
end # case
|
29
|
+
end # class method normalize_sort_params
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def parse_sort_direction direction
|
34
|
+
(direction == -1 || direction.to_s.downcase == 'desc') ? -1 : 1
|
35
|
+
end # class method parse_sort_direction
|
36
|
+
|
37
|
+
def parse_sort_param param
|
38
|
+
case param
|
39
|
+
when Array
|
40
|
+
{ param[0] => parse_sort_direction(param[1]) }
|
41
|
+
when Origin::Key
|
42
|
+
{ param.name => param.operator }
|
43
|
+
when Symbol
|
44
|
+
{ param => 1 }
|
45
|
+
end # case
|
46
|
+
end # class method parse_sort_param
|
47
|
+
end # class << self
|
27
48
|
|
28
49
|
# The name of the field used to store the order.
|
29
50
|
#
|
30
51
|
# @return [Symbol] The field name.
|
31
52
|
def field_name
|
32
|
-
fetch(:as, default_field_name).intern
|
53
|
+
fetch(:as, Metadata.default_field_name(self[:sort_params])).intern
|
33
54
|
end # method field_name
|
34
55
|
|
35
56
|
# @return [Boolean] True if a custom field name is defined; otherwise
|
@@ -83,29 +104,6 @@ module Mongoid::SleepingKingStudios
|
|
83
104
|
def sort_criteria criteria
|
84
105
|
filter_criteria(criteria).order_by(self[:sort_params])
|
85
106
|
end # method sort_criteria
|
86
|
-
|
87
|
-
private
|
88
|
-
|
89
|
-
def default_field_name
|
90
|
-
self[:sort_params].map { |key, value|
|
91
|
-
"#{key}_#{value == 1 ? 'asc' : 'desc'}"
|
92
|
-
}.join('_') + '_order'
|
93
|
-
end # method default_field_name
|
94
|
-
|
95
|
-
def parse_sort_param param
|
96
|
-
case param
|
97
|
-
when Array
|
98
|
-
{ param[0] => parse_sort_direction(param[1]) }
|
99
|
-
when Origin::Key
|
100
|
-
{ param.name => param.operator }
|
101
|
-
when Symbol
|
102
|
-
{ param => 1 }
|
103
|
-
end # case
|
104
|
-
end # method sort_param=
|
105
|
-
|
106
|
-
def parse_sort_direction direction
|
107
|
-
(direction == -1 || direction.to_s.downcase == 'desc') ? -1 : 1
|
108
|
-
end # method parse_sort_direction
|
109
107
|
end # class
|
110
108
|
end # module
|
111
109
|
end # module
|
@@ -33,9 +33,10 @@ module Mongoid::SleepingKingStudios
|
|
33
33
|
# collection, generating the cached order index.
|
34
34
|
# @param [Hash] options The options for the relation.
|
35
35
|
def self.apply base, sort_params, options
|
36
|
-
name = :orderable
|
37
36
|
validate_options name, options
|
37
|
+
sort_params = Metadata.normalize_sort_params(sort_params)
|
38
38
|
options.update :sort_params => sort_params
|
39
|
+
name = options.fetch(:as, Metadata.default_field_name(sort_params))
|
39
40
|
meta = characterize name, options, Metadata
|
40
41
|
|
41
42
|
relate base, name, meta
|
@@ -109,24 +110,33 @@ module Mongoid::SleepingKingStudios
|
|
109
110
|
base_name = metadata.field_name.to_s.gsub(/_order\z/,'')
|
110
111
|
filtered = metadata.filter_criteria(base)
|
111
112
|
|
112
|
-
|
113
|
-
|
113
|
+
# Define instance-level helpers.
|
114
|
+
instance_methods = Module.new
|
115
|
+
|
116
|
+
instance_methods.send :define_method, :"next_#{base_name}" do |scope = base|
|
117
|
+
metadata.filter_criteria(scope).asc(metadata.field_name).
|
118
|
+
where(metadata.field_name.gt => send(metadata.field_name)).limit(1).first
|
114
119
|
end # method
|
115
120
|
|
116
|
-
|
117
|
-
|
121
|
+
instance_methods.send :define_method, :"prev_#{base_name}" do |scope = base|
|
122
|
+
metadata.filter_criteria(scope).desc(metadata.field_name).
|
123
|
+
where(metadata.field_name.lt => send(metadata.field_name)).limit(1).first
|
118
124
|
end # method
|
119
|
-
|
120
|
-
base.send :
|
121
|
-
|
125
|
+
|
126
|
+
base.send :include, instance_methods
|
127
|
+
|
128
|
+
# Define class-level helpers.
|
129
|
+
class_methods = Module.new
|
130
|
+
|
131
|
+
class_methods.send :define_method, :"first_#{base_name}" do |scope = base|
|
132
|
+
metadata.filter_criteria(scope).asc(metadata.field_name).limit(1).first
|
122
133
|
end # method
|
123
134
|
|
124
|
-
|
125
|
-
|
135
|
+
class_methods.send :define_method, :"last_#{base_name}" do |scope = base|
|
136
|
+
metadata.filter_criteria(scope).desc(metadata.field_name).limit(1).first
|
126
137
|
end # method
|
127
138
|
|
128
|
-
|
129
|
-
meta.send :define_method, :"reorder_#{base_name}!" do
|
139
|
+
class_methods.send :define_method, :"reorder_#{base_name}!" do
|
130
140
|
base.update_all(metadata.field_name => nil)
|
131
141
|
|
132
142
|
criteria = metadata.sort_criteria(base)
|
@@ -136,6 +146,8 @@ module Mongoid::SleepingKingStudios
|
|
136
146
|
record.set(metadata.field_name => index)
|
137
147
|
end # each
|
138
148
|
end # method
|
149
|
+
|
150
|
+
base.extend class_methods
|
139
151
|
end # module method define_helpers
|
140
152
|
|
141
153
|
# Returns a list of options that are valid for this concern.
|
@@ -175,6 +187,26 @@ module Mongoid::SleepingKingStudios
|
|
175
187
|
concern.apply self, sort_params, options
|
176
188
|
end # class method slugify
|
177
189
|
|
190
|
+
# @!method first_ordering_name
|
191
|
+
# Finds the first document, based on the stored ordering values.
|
192
|
+
#
|
193
|
+
# The generated name of this method will depend on the sort params or the
|
194
|
+
# :as option provided. For example, :as => :alphabetical_order will
|
195
|
+
# result in an instance method #first_alphabetical.
|
196
|
+
#
|
197
|
+
# @return [Mongoid::Document, nil] The first document in the order, or
|
198
|
+
# nil if there are no documents in the collection.
|
199
|
+
|
200
|
+
# @!method last_ordering_name
|
201
|
+
# Finds the last document, based on the stored ordering values.
|
202
|
+
#
|
203
|
+
# The generated name of this method will depend on the sort params or the
|
204
|
+
# :as option provided. For example, :as => :alphabetical_order will
|
205
|
+
# result in an instance method #last_alphabetical.
|
206
|
+
#
|
207
|
+
# @return [Mongoid::Document, nil] The last document in the order, or nil
|
208
|
+
# if there are no documents in the collection.
|
209
|
+
|
178
210
|
# @!method reorder_ordering_name!
|
179
211
|
# Iterates through the entire collection and sets the cached order of
|
180
212
|
# each item to its current order index. Filtered items have their order
|
@@ -187,26 +219,6 @@ module Mongoid::SleepingKingStudios
|
|
187
219
|
# result in a class method ::reorder_alphabetical!.
|
188
220
|
end # module
|
189
221
|
|
190
|
-
# @!method first_ordering_name
|
191
|
-
# Finds the first document, based on the stored ordering values.
|
192
|
-
#
|
193
|
-
# The generated name of this method will depend on the sort params or the
|
194
|
-
# :as option provided. For example, :as => :alphabetical_order will
|
195
|
-
# result in an instance method #first_alphabetical.
|
196
|
-
#
|
197
|
-
# @return [Mongoid::Document, nil] The first document in the order, or
|
198
|
-
# nil if there are no documents in the collection.
|
199
|
-
|
200
|
-
# @!method last_ordering_name
|
201
|
-
# Finds the last document, based on the stored ordering values.
|
202
|
-
#
|
203
|
-
# The generated name of this method will depend on the sort params or the
|
204
|
-
# :as option provided. For example, :as => :alphabetical_order will
|
205
|
-
# result in an instance method #last_alphabetical.
|
206
|
-
#
|
207
|
-
# @return [Mongoid::Document, nil] The last document in the order, or nil
|
208
|
-
# if there are no documents in the collection.
|
209
|
-
|
210
222
|
# @!method next_ordering_name
|
211
223
|
# Finds the next document, based on the stored ordering values.
|
212
224
|
#
|