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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29178b4032ba47697556efe5804600c73450fb69
4
- data.tar.gz: 1df094986f9fba1e988117c46df89ae85b44134c
3
+ metadata.gz: 287eebf704b95b0ecc4b7d38feecd57d335f743c
4
+ data.tar.gz: 92be00de6a85145ad88e07bb74b83e33a4b14f72
5
5
  SHA512:
6
- metadata.gz: c492123648a5e294f516a5b06494ae133166ae1c8c565453dc427ab066479e88ab0c1ae1dfe30a234a294149cc64526eab701c61112c7314e8db83f84accad7a
7
- data.tar.gz: 163787e59bc295cac492c8da46e21563c2f98cc441729c91f045149caa71d7cf814865740d92ff6e5fe970a4129b3c7e2aefcb5b6ca7d2aafc7fe8bbd289244d
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
- ## New Features
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
- ## New Features
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
- ## New Features
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
- ##### \#first_ordering_name
111
+ ##### \#next_ordering_name
112
112
 
113
- Finds the first document, based on the stored ordering values.
113
+ Finds the next document, based on the stored ordering values. Takes an optional
114
+ scope parameter to filter the results.
114
115
 
115
- ##### \#last_ordering_name
116
+ ##### \#prev_ordering_name
116
117
 
117
- Finds the last document, based on the stored ordering values.
118
+ Finds the previous document, based on the stored ordering values. Takes an
119
+ optional scope parameter to filter the results.
118
120
 
119
- ##### \#next_ordering_name
121
+ ##### \::first_ordering_name
120
122
 
121
- Finds the next document, based on the stored ordering values.
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
- ##### \#prev_ordering_name
126
+ ##### \::last_ordering_name
124
127
 
125
- Finds the previous document, based on the stored ordering values.
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
- # @param [Symbol, String] name The name of the concern or relation.
10
- # @param [Hash] properties The properties of the concern or relation.
11
- def initialize name, properties = {}
12
- super
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
- self[:sort_params] = case properties[:sort_params]
15
- when Array
16
- properties[:sort_params].reduce({}) do |hsh, param|
17
- hsh.merge parse_sort_param(param)
18
- end # each
19
- when Hash
20
- properties[:sort_params].each.with_object({}) do |(key, value), hsh|
21
- hsh[key] = parse_sort_direction(value)
22
- end # each
23
- when Symbol, Origin::Key
24
- parse_sort_param(properties[:sort_params])
25
- end # case
26
- end # method initialize
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
- base.send :define_method, :"first_#{base_name}" do
113
- filtered.order_by(metadata.field_name.asc).limit(1).first
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
- base.send :define_method, :"last_#{base_name}" do
117
- filtered.order_by(metadata.field_name.desc).limit(1).first
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 :define_method, :"next_#{base_name}" do
121
- filtered.order_by(metadata.field_name.asc).where(metadata.field_name.gt => send(metadata.field_name)).limit(1).first
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
- base.send :define_method, :"prev_#{base_name}" do
125
- filtered.order_by(metadata.field_name.desc).where(metadata.field_name.lt => send(metadata.field_name)).limit(1).first
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
- meta = class << base; self; end
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
  #
@@ -3,6 +3,6 @@
3
3
  module Mongoid
4
4
  module SleepingKingStudios
5
5
  # The current version of the gem.
6
- VERSION = '0.7.5'
6
+ VERSION = '0.7.6'
7
7
  end # module
8
8
  end # module
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-sleeping_king_studios
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob "Merlin" Smith