mongoid-sleeping_king_studios 0.7.0 → 0.7.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +18 -5
- data/lib/mongoid/sleeping_king_studios/orderable.rb +34 -2
- 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: fdbcd42bc1fb3604aaf7be52d70f408408af12b4
|
4
|
+
data.tar.gz: 05958760903d551da4b9789b2357ce27fb3df707
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7440b1410fcef46d1c2e35c9e813253d492faa2b4a92b5b880e2ca620edc6acd2b62e7146863c3b1d3f01c6683bcbd085dca6a125226a5f353093f15e891eb1e
|
7
|
+
data.tar.gz: c51b962818822ec95937220a58f5a6189e10d239abf8237017e4bcf42e809a18c9089823601f3379c99adac5c9e61eca3fb389cca3a752123d5fba870c6d21e0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -102,14 +102,27 @@ The ::cache_ordering method accepts a subset of the params for an Origin
|
|
102
102
|
|
103
103
|
#### Helpers
|
104
104
|
|
105
|
-
Creating an ordering cache also creates the following helpers
|
105
|
+
Creating an ordering cache also creates the following helpers. The name of the
|
106
|
+
generated helpers will depend on the sort params provided, or the name given
|
107
|
+
via the :as option (see below). For example, providing :as =>
|
108
|
+
:alphabetical_order will generate helpers \#next_alphabetical,
|
109
|
+
\#prev_alphabetical, and ::reorder_alphabetical!.
|
110
|
+
|
111
|
+
##### \#next_ordering_name
|
112
|
+
|
113
|
+
Finds the next document, based on the stored ordering values.
|
114
|
+
|
115
|
+
##### \#prev_ordering_name
|
116
|
+
|
117
|
+
Finds the previous document, based on the stored ordering values.
|
106
118
|
|
107
119
|
##### ::reorder_ordering_name!
|
108
120
|
|
109
|
-
Loops through the collection and sets each item's field to the
|
110
|
-
ordered index. Normally, this is handled on item save, but this
|
111
|
-
a bulk update of the collection when adding the concern to an
|
112
|
-
or if data corruption or other issues have broken the existing
|
121
|
+
(Class Method) Loops through the collection and sets each item's field to the
|
122
|
+
appropriate ordered index. Normally, this is handled on item save, but this
|
123
|
+
helper allows a bulk update of the collection when adding the concern to an
|
124
|
+
existing model, or if data corruption or other issues have broken the existing
|
125
|
+
values.
|
113
126
|
|
114
127
|
#### Options
|
115
128
|
|
@@ -106,7 +106,19 @@ module Mongoid::SleepingKingStudios
|
|
106
106
|
# @param [Class] base The base class into which the concern is mixed in.
|
107
107
|
# @param [Metadata] metadata The metadata for the relation.
|
108
108
|
def self.define_helpers base, metadata
|
109
|
-
|
109
|
+
base_name = metadata.field_name.to_s.gsub(/_order\z/,'')
|
110
|
+
|
111
|
+
name = :"next_#{base_name}"
|
112
|
+
base.send :define_method, name do
|
113
|
+
base.order_by(metadata.field_name.asc).where(metadata.field_name.gt => send(metadata.field_name)).limit(1).first
|
114
|
+
end # method
|
115
|
+
|
116
|
+
name = :"prev_#{base_name}"
|
117
|
+
base.send :define_method, name do
|
118
|
+
base.order_by(metadata.field_name.desc).where(metadata.field_name.lt => send(metadata.field_name)).limit(1).first
|
119
|
+
end # method
|
120
|
+
|
121
|
+
name = :"reorder_#{base_name}!"
|
110
122
|
meta = class << base; self; end
|
111
123
|
meta.send :define_method, name do
|
112
124
|
base.update_all(metadata.field_name => nil)
|
@@ -166,7 +178,27 @@ module Mongoid::SleepingKingStudios
|
|
166
178
|
#
|
167
179
|
# The generated name of this method will depend on the sort params or
|
168
180
|
# the :as option provided. For example, :as => :alphabetical_order will
|
169
|
-
# result in a class method ::reorder_alphabetical
|
181
|
+
# result in a class method ::reorder_alphabetical!.
|
170
182
|
end # module
|
183
|
+
|
184
|
+
# @!method next_ordering_name
|
185
|
+
# Finds the next document, based on the stored ordering values.
|
186
|
+
#
|
187
|
+
# The generated name of this method will depend on the sort params or the
|
188
|
+
# :as option provided. For example, :as => :alphabetical_order will
|
189
|
+
# result in an instance method #next_alphabetical.
|
190
|
+
#
|
191
|
+
# @return [Mongoid::Document, nil] The next document in the order, or nil
|
192
|
+
# if there are no more documents in the collection.
|
193
|
+
|
194
|
+
# @!method prev_ordering_name
|
195
|
+
# Finds the previous document, based on the stored ordering values.
|
196
|
+
#
|
197
|
+
# The generated name of this method will depend on the sort params or the
|
198
|
+
# :as option provided. For example, :as => :alphabetical_order will
|
199
|
+
# result in an instance method #prev_alphabetical.
|
200
|
+
#
|
201
|
+
# @return [Mongoid::Document, nil] The previous document in the order, or
|
202
|
+
# nil if there are no prior documents in the collection.
|
171
203
|
end # module
|
172
204
|
end # module
|