sortability 0.0.3 → 0.1.0

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: f0c25a30046ab1c63c849bf3f5c5955b30e237b5
4
- data.tar.gz: b21fea91cfbc95d4a3d62d4a56b75106452d3b61
3
+ metadata.gz: 8de084b141765cc5613a436a6573b6a370d535c3
4
+ data.tar.gz: 20c7a9f1951fa31c5840cb12c8046d2c734536ce
5
5
  SHA512:
6
- metadata.gz: 689a20e1ea4bee8f63f5fd9854f75331393d375408cec56de3d02761d9fe65c7c3b7b2cff1830c6684c9efdf006470ebfee0d2ece1e295b1815754598adbe03d
7
- data.tar.gz: 7b1123cc8b8a6b52eb931b200201a712a358c16b757b9fe576cfe0b84e2bdf4e8f8623c5376a45c0436acb932ac148d207d1198581a1e75d4ada2a8631fbe794
6
+ metadata.gz: 5d1313f9b10346d6f2dd1d5506ab6b9a24cba540ff0fdef6b388ed32631e4aaa7cb2c9b5a0949971a6dd7e19e26020d2023a18a78036d7f0e8f4d066398d3ced
7
+ data.tar.gz: a9fb6ac2194be7a270f51d5bdf6a48a781389f3365d4c14b239d3ab028cbaed4952e8ae449766349a8674037bb0be6c548e0ed001e96055b470bd41c1308b1c0
data/README.md CHANGED
@@ -87,10 +87,16 @@ sortable_belongs_to :container, inverse_of: :records,
87
87
  scope: :container_id # , on: :sort_position
88
88
  ```
89
89
 
90
+ You can also specify a custom association scope:
91
+
92
+ ```rb
93
+ sortable_belongs_to :container, -> { with_deleted },
94
+ inverse_of: :records, scope: :container_id # , on: :sort_position
95
+ ```
96
+
90
97
  It is highly recommended that you specify the `inverse_of` and `scope` options.
91
98
 
92
- If `records` are sorted globally, without a `container`,
93
- use the `sortable_class` method instead:
99
+ If `records` are sorted globally, without a `container`, use the `sortable_class` method instead:
94
100
 
95
101
  ```rb
96
102
  sortable_class # on: :sort_position, scope: :sort_group_number
@@ -104,6 +110,16 @@ Simply replace the `has_many :records` relation in your `Container` model with:
104
110
  sortable_has_many :records, inverse_of: :container # , on: :sort_position
105
111
  ```
106
112
 
113
+ You can also specify a custom association scope,
114
+ but in that case you have to order the records yourself:
115
+
116
+ ```rb
117
+ sortable_has_many :records, ->{ with_deleted.order(:sort_position) },
118
+ inverse_of: :container # , on: :sort_position
119
+ ```
120
+
121
+ Once again, it is highly recommended that you specify the `inverse_of` option.
122
+
107
123
  ## Usage
108
124
 
109
125
  Once you have run the migrations and modified your models according to the installation instructions, you are ready to start sorting the `records`.
@@ -26,9 +26,10 @@ module Sortability
26
26
 
27
27
  # Returns all the sort peers for this record, including self
28
28
  define_method peers_mname do |force_scope_load = false|
29
- cont = container.nil? ? nil : send(container)
30
- return send(container).send(inverse_of) \
31
- unless force_scope_load || cont.nil? || inverse_of.nil?
29
+ unless force_scope_load || container.nil? || inverse_of.nil?
30
+ cont = send(container)
31
+ return cont.send(inverse_of) unless cont.nil?
32
+ end
32
33
 
33
34
  relation = self.class.unscoped
34
35
  scope_array.each do |s|
@@ -122,20 +123,24 @@ module Sortability
122
123
  end
123
124
 
124
125
  # Defines a sortable has_many relation on the container
125
- def sortable_has_many(records, options = {})
126
- on = options[:on] || :sort_position
127
-
128
- class_exec do
129
- has_many records, lambda { order(on) }, options.except(:on)
126
+ def sortable_has_many(records, scope_or_options = nil, options_with_scope = {}, &extension)
127
+ scope, options = extract_association_params(scope_or_options, options_with_scope)
128
+ if scope.nil?
129
+ on = options[:on] || :sort_position
130
+ scope = -> { order(on) }
130
131
  end
132
+
133
+ class_exec { has_many records, scope, options.except(:on), &extension }
131
134
  end
132
135
 
133
136
  # Defines a sortable belongs_to relation on the child records
134
- def sortable_belongs_to(container, options = {})
137
+ def sortable_belongs_to(container, scope_or_options = nil,
138
+ options_with_scope = {}, &extension)
139
+ scope, options = extract_association_params(scope_or_options, options_with_scope)
135
140
  on = options[:on] || :sort_position
136
141
 
137
142
  class_exec do
138
- belongs_to container, options.except(:on, :scope)
143
+ belongs_to container, scope, options.except(:on, :scope), &extension
139
144
 
140
145
  reflection = reflect_on_association(container)
141
146
  options[:scope] ||= reflection.polymorphic? ? \
@@ -170,6 +175,16 @@ module Sortability
170
175
 
171
176
  sortable_methods(options)
172
177
  end
178
+
179
+ protected
180
+
181
+ def extract_association_params(scope_or_options, options_with_scope)
182
+ if scope_or_options.is_a?(Hash)
183
+ [nil, scope_or_options]
184
+ else
185
+ [scope_or_options, options_with_scope]
186
+ end
187
+ end
173
188
  end
174
189
  end
175
190
  end
@@ -1,3 +1,3 @@
1
1
  module Sortability
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dante Soares
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-05 00:00:00.000000000 Z
12
+ date: 2016-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  requirements: []
146
146
  rubyforge_project:
147
- rubygems_version: 2.4.5
147
+ rubygems_version: 2.4.5.1
148
148
  signing_key:
149
149
  specification_version: 4
150
150
  summary: Rails gem that provides easy to use ordered records