has_scope 0.6.0 → 0.7.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: 48513d85f54847d588aec0efc61f818a2ffebefe
4
- data.tar.gz: 8c85796fc391a814bbb821f006934ded14bd820f
3
+ metadata.gz: 4a38c086c93545529f868febab3d09cf921f5722
4
+ data.tar.gz: c41ae283cf6b0f45698fee5d631030cdf0565acf
5
5
  SHA512:
6
- metadata.gz: 9648a802b913abb6873ec394d14c3fe22404e788f1a984901439a3c5883351dc9b3223bdb67c4f443351db16e56b9502b3923c8e0c0770448893e5f4eb1c7350
7
- data.tar.gz: 49c94cbb4ef1d0376078deb8ac6c81cbc61b1092e0440b36f53dc362240799bf503a8a91338c8b9a5a52425fa4cced774f9965e7c65d03c280eb37546ddc592e
6
+ metadata.gz: 0dd2ec790d7b8f8e322cd40bb5086f445b3bc0ae39dba21d9465741270adb99ebd89c0fd91a62a0f9c338e41cd2d0df2370c2e2d9d5587801d55acff15a42971
7
+ data.tar.gz: 1d377574c40939629bbd82116a14070777ed9df568792599a94749a048addd1218d7c0cd0be4543a402479884afcd2f408cfbee35156ffb72f5769285d24939c
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Build Status](https://api.travis-ci.org/plataformatec/has_scope.png?branch=master)](http://travis-ci.org/plataformatec/has_scope)
5
5
  [![Code Climate](https://codeclimate.com/github/plataformatec/has_scope.png)](https://codeclimate.com/github/plataformatec/has_scope)
6
6
 
7
- Has scope allows you to easily create controller filters based on your resources named scopes.
7
+ Has scope allows you to map incoming controller parameters to named scopes in your resources.
8
8
  Imagine the following model called graduations:
9
9
 
10
10
  ```ruby
@@ -131,6 +131,20 @@ has_scope :not_voted_by_me, :type => :boolean do |controller, scope|
131
131
  end
132
132
  ```
133
133
 
134
+ ## Keyword arguments
135
+
136
+ Scopes with keyword arguments need to be called in a block:
137
+
138
+ ```ruby
139
+ # in the model
140
+ scope :for_course, lambda { |course_id:| where(course_id: course_id) }
141
+
142
+ # in the controller
143
+ has_scope :for_course do |controller, scope, value|
144
+ scope.for_course(course_id: value)
145
+ end
146
+ ```
147
+
134
148
  ## Apply scope on every request
135
149
 
136
150
  To apply scope on every request set default value and `allow_blank: true`:
@@ -3,7 +3,7 @@ module HasScope
3
3
 
4
4
  ALLOWED_TYPES = {
5
5
  :array => [[ Array ]],
6
- :hash => [[ Hash ]],
6
+ :hash => [[Hash, ActionController::Parameters]],
7
7
  :boolean => [[ Object ], -> v { TRUE_VALUES.include?(v) }],
8
8
  :default => [[ String, Numeric ]],
9
9
  }
@@ -147,6 +147,8 @@ module HasScope
147
147
  value.select { |v| v.present? }
148
148
  when Hash
149
149
  value.select { |k, v| normalize_blanks(v).present? }.with_indifferent_access
150
+ when ActionController::Parameters
151
+ normalize_blanks(value.to_unsafe_h)
150
152
  else
151
153
  value
152
154
  end
@@ -201,5 +203,5 @@ end
201
203
 
202
204
  ActiveSupport.on_load :action_controller do
203
205
  include HasScope
204
- helper_method :current_scopes
206
+ helper_method :current_scopes if respond_to?(:helper_method)
205
207
  end
@@ -1,3 +1,3 @@
1
1
  module HasScope
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -38,6 +38,7 @@ class TreesController < ApplicationController
38
38
  def show
39
39
  @tree = apply_scopes(Tree).find(params[:id])
40
40
  end
41
+
41
42
  alias :edit :show
42
43
 
43
44
  protected
@@ -49,8 +50,15 @@ class TreesController < ApplicationController
49
50
  false
50
51
  end
51
52
 
52
- def default_render
53
- render :text => action_name
53
+ if ActionPack::VERSION::MAJOR == 5
54
+ def default_render
55
+ render body: action_name
56
+ end
57
+ else
58
+ # TODO: Remove this when we only support Rails 5.
59
+ def default_render
60
+ render text: action_name
61
+ end
54
62
  end
55
63
  end
56
64
 
@@ -70,7 +78,7 @@ class HasScopeTest < ActionController::TestCase
70
78
  Tree.expects(:only_tall).with().returns(Tree).in_sequence
71
79
  Tree.expects(:all).returns([mock_tree]).in_sequence
72
80
  get :index, :only_tall => 'true'
73
- assert_equal([mock_tree], assigns(:trees))
81
+ assert_equal([mock_tree], assigns(:@trees))
74
82
  assert_equal({ :only_tall => true }, current_scopes)
75
83
  end
76
84
 
@@ -78,7 +86,7 @@ class HasScopeTest < ActionController::TestCase
78
86
  Tree.expects(:only_tall).never
79
87
  Tree.expects(:all).returns([mock_tree])
80
88
  get :index, :only_tall => 'false'
81
- assert_equal([mock_tree], assigns(:trees))
89
+ assert_equal([mock_tree], assigns(:@trees))
82
90
  assert_equal({ }, current_scopes)
83
91
  end
84
92
 
@@ -86,7 +94,7 @@ class HasScopeTest < ActionController::TestCase
86
94
  Tree.expects(:conifer).with(true).returns(Tree).in_sequence
87
95
  Tree.expects(:all).returns([mock_tree]).in_sequence
88
96
  get :index, :conifer => 'true'
89
- assert_equal([mock_tree], assigns(:trees))
97
+ assert_equal([mock_tree], assigns(:@trees))
90
98
  assert_equal({ :conifer => true }, current_scopes)
91
99
  end
92
100
 
@@ -94,7 +102,7 @@ class HasScopeTest < ActionController::TestCase
94
102
  Tree.expects(:conifer).with(false).returns(Tree).in_sequence
95
103
  Tree.expects(:all).returns([mock_tree]).in_sequence
96
104
  get :index, :conifer => 'not_true'
97
- assert_equal([mock_tree], assigns(:trees))
105
+ assert_equal([mock_tree], assigns(:@trees))
98
106
  assert_equal({ :conifer => false }, current_scopes)
99
107
  end
100
108
 
@@ -102,7 +110,7 @@ class HasScopeTest < ActionController::TestCase
102
110
  Tree.expects(:conifer).never
103
111
  Tree.expects(:all).returns([mock_tree])
104
112
  get :index
105
- assert_equal([mock_tree], assigns(:trees))
113
+ assert_equal([mock_tree], assigns(:@trees))
106
114
  assert_equal({ }, current_scopes)
107
115
  end
108
116
 
@@ -110,7 +118,7 @@ class HasScopeTest < ActionController::TestCase
110
118
  Tree.expects(:only_tall).never
111
119
  Tree.expects(:find).with('42').returns(mock_tree)
112
120
  get :show, :only_tall => 'true', :id => '42'
113
- assert_equal(mock_tree, assigns(:tree))
121
+ assert_equal(mock_tree, assigns(:@tree))
114
122
  assert_equal({ }, current_scopes)
115
123
  end
116
124
 
@@ -119,7 +127,7 @@ class HasScopeTest < ActionController::TestCase
119
127
  Tree.expects(:only_tall).never
120
128
  Tree.expects(:all).returns([mock_tree])
121
129
  get :index, :only_tall => 'true'
122
- assert_equal([mock_tree], assigns(:trees))
130
+ assert_equal([mock_tree], assigns(:@trees))
123
131
  assert_equal({ }, current_scopes)
124
132
  end
125
133
 
@@ -128,7 +136,7 @@ class HasScopeTest < ActionController::TestCase
128
136
  Tree.expects(:color).never
129
137
  Tree.expects(:all).returns([mock_tree])
130
138
  get :index, :color => 'blue'
131
- assert_equal([mock_tree], assigns(:trees))
139
+ assert_equal([mock_tree], assigns(:@trees))
132
140
  assert_equal({ }, current_scopes)
133
141
  end
134
142
 
@@ -136,7 +144,7 @@ class HasScopeTest < ActionController::TestCase
136
144
  Tree.expects(:shadown_range).never
137
145
  Tree.expects(:all).returns([mock_tree])
138
146
  get :index, :shadown_range => 20
139
- assert_equal([mock_tree], assigns(:trees))
147
+ assert_equal([mock_tree], assigns(:@trees))
140
148
  assert_equal({ }, current_scopes)
141
149
  end
142
150
 
@@ -144,7 +152,7 @@ class HasScopeTest < ActionController::TestCase
144
152
  Tree.expects(:color).with('blue').returns(Tree).in_sequence
145
153
  Tree.expects(:all).returns([mock_tree]).in_sequence
146
154
  get :index, :color => 'blue'
147
- assert_equal([mock_tree], assigns(:trees))
155
+ assert_equal([mock_tree], assigns(:@trees))
148
156
  assert_equal({ :color => 'blue' }, current_scopes)
149
157
  end
150
158
 
@@ -152,7 +160,7 @@ class HasScopeTest < ActionController::TestCase
152
160
  Tree.expects(:color).never
153
161
  Tree.expects(:all).returns([mock_tree]).in_sequence
154
162
  get :index, :color => ''
155
- assert_equal([mock_tree], assigns(:trees))
163
+ assert_equal([mock_tree], assigns(:@trees))
156
164
  assert_equal({ }, current_scopes)
157
165
  end
158
166
 
@@ -160,7 +168,7 @@ class HasScopeTest < ActionController::TestCase
160
168
  Tree.expects(:root_type).with('').returns(Tree)
161
169
  Tree.expects(:all).returns([mock_tree]).in_sequence
162
170
  get :index, :root => ''
163
- assert_equal([mock_tree], assigns(:trees))
171
+ assert_equal([mock_tree], assigns(:@trees))
164
172
  assert_equal({ :root => '' }, current_scopes)
165
173
  end
166
174
 
@@ -169,7 +177,7 @@ class HasScopeTest < ActionController::TestCase
169
177
  Tree.expects(:color).with('blue').returns(Tree)
170
178
  Tree.expects(:all).returns([mock_tree])
171
179
  get :index, :color => 'blue', :only_tall => 'true'
172
- assert_equal([mock_tree], assigns(:trees))
180
+ assert_equal([mock_tree], assigns(:@trees))
173
181
  assert_equal({ :color => 'blue', :only_tall => true }, current_scopes)
174
182
  end
175
183
 
@@ -178,8 +186,8 @@ class HasScopeTest < ActionController::TestCase
178
186
  Tree.expects(:paginate).with(hash).returns(Tree)
179
187
  Tree.expects(:all).returns([mock_tree])
180
188
  get :index, :paginate => hash
181
- assert_equal([mock_tree], assigns(:trees))
182
- assert_equal({ :paginate => hash }, current_scopes)
189
+ assert_equal([mock_tree], assigns(:@trees))
190
+ assert_equal({ paginate: hash }, current_scopes)
183
191
  end
184
192
 
185
193
  def test_scope_of_type_hash_with_using
@@ -187,7 +195,7 @@ class HasScopeTest < ActionController::TestCase
187
195
  Tree.expects(:args_paginate).with("1", "10").returns(Tree)
188
196
  Tree.expects(:all).returns([mock_tree])
189
197
  get :index, :args_paginate => hash
190
- assert_equal([mock_tree], assigns(:trees))
198
+ assert_equal([mock_tree], assigns(:@trees))
191
199
  assert_equal({ :args_paginate => hash }, current_scopes)
192
200
  end
193
201
 
@@ -196,7 +204,7 @@ class HasScopeTest < ActionController::TestCase
196
204
  Tree.expects(:paginate).never
197
205
  Tree.expects(:all).returns([mock_tree])
198
206
  get :index, :paginate => hash
199
- assert_equal([mock_tree], assigns(:trees))
207
+ assert_equal([mock_tree], assigns(:@trees))
200
208
  assert_equal({ }, current_scopes)
201
209
  end
202
210
 
@@ -205,7 +213,7 @@ class HasScopeTest < ActionController::TestCase
205
213
  Tree.expects(:paginate).never
206
214
  Tree.expects(:all).returns([mock_tree])
207
215
  get :index, :paginate => hash
208
- assert_equal([mock_tree], assigns(:trees))
216
+ assert_equal([mock_tree], assigns(:@trees))
209
217
  assert_equal({ }, current_scopes)
210
218
  end
211
219
 
@@ -214,7 +222,7 @@ class HasScopeTest < ActionController::TestCase
214
222
  Tree.expects(:paginate).never
215
223
  Tree.expects(:all).returns([mock_tree])
216
224
  get :index, :paginate => hash
217
- assert_equal([mock_tree], assigns(:trees))
225
+ assert_equal([mock_tree], assigns(:@trees))
218
226
  assert_equal({ }, current_scopes)
219
227
  end
220
228
 
@@ -223,7 +231,7 @@ class HasScopeTest < ActionController::TestCase
223
231
  Tree.expects(:categories).with(array).returns(Tree)
224
232
  Tree.expects(:all).returns([mock_tree])
225
233
  get :index, :categories => array
226
- assert_equal([mock_tree], assigns(:trees))
234
+ assert_equal([mock_tree], assigns(:@trees))
227
235
  assert_equal({ :categories => array }, current_scopes)
228
236
  end
229
237
 
@@ -231,14 +239,14 @@ class HasScopeTest < ActionController::TestCase
231
239
  Tree.expects(:categories).never
232
240
  Tree.expects(:all).returns([mock_tree])
233
241
  get :index, :categories => [""]
234
- assert_equal([mock_tree], assigns(:trees))
242
+ assert_equal([mock_tree], assigns(:@trees))
235
243
  assert_equal({ }, current_scopes)
236
244
  end
237
245
 
238
246
  def test_scope_of_invalid_type_silently_fails
239
247
  Tree.expects(:all).returns([mock_tree])
240
248
  get :index, :paginate => "1"
241
- assert_equal([mock_tree], assigns(:trees))
249
+ assert_equal([mock_tree], assigns(:@trees))
242
250
  assert_equal({ }, current_scopes)
243
251
  end
244
252
 
@@ -246,7 +254,7 @@ class HasScopeTest < ActionController::TestCase
246
254
  Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence
247
255
  Tree.expects(:find).with('42').returns(mock_tree).in_sequence
248
256
  get :edit, :id => '42'
249
- assert_equal(mock_tree, assigns(:tree))
257
+ assert_equal(mock_tree, assigns(:@tree))
250
258
  assert_equal({ :shadown_range => 10 }, current_scopes)
251
259
  end
252
260
 
@@ -254,7 +262,7 @@ class HasScopeTest < ActionController::TestCase
254
262
  Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence
255
263
  Tree.expects(:find).with('42').returns(mock_tree).in_sequence
256
264
  get :edit, :id => '42', :shadown_range => '20'
257
- assert_equal(mock_tree, assigns(:tree))
265
+ assert_equal(mock_tree, assigns(:@tree))
258
266
  assert_equal({ :shadown_range => '20' }, current_scopes)
259
267
  end
260
268
 
@@ -262,7 +270,7 @@ class HasScopeTest < ActionController::TestCase
262
270
  Tree.expects(:root_type).with('outside').returns(Tree).in_sequence
263
271
  Tree.expects(:find).with('42').returns(mock_tree).in_sequence
264
272
  get :show, :id => '42', :root => 'outside'
265
- assert_equal(mock_tree, assigns(:tree))
273
+ assert_equal(mock_tree, assigns(:@tree))
266
274
  assert_equal({ :root => 'outside' }, current_scopes)
267
275
  end
268
276
 
@@ -271,7 +279,7 @@ class HasScopeTest < ActionController::TestCase
271
279
  Tree.expects(:planted_before).with("today").returns(Tree)
272
280
  Tree.expects(:all).returns([mock_tree])
273
281
  get :index
274
- assert_equal([mock_tree], assigns(:trees))
282
+ assert_equal([mock_tree], assigns(:@trees))
275
283
  assert_equal({ :planted_before => "today" }, current_scopes)
276
284
  end
277
285
 
@@ -280,7 +288,7 @@ class HasScopeTest < ActionController::TestCase
280
288
  Tree.expects(:calculate_height).with(100).returns(Tree).in_sequence
281
289
  Tree.expects(:new).returns(mock_tree).in_sequence
282
290
  get :new
283
- assert_equal(mock_tree, assigns(:tree))
291
+ assert_equal(mock_tree, assigns(:@tree))
284
292
  assert_equal({ :calculate_height => 100 }, current_scopes)
285
293
  end
286
294
 
@@ -289,7 +297,7 @@ class HasScopeTest < ActionController::TestCase
289
297
  Tree.expects(:planted_after).with(parsed).returns(Tree)
290
298
  Tree.expects(:all).returns([mock_tree])
291
299
  get :index, :planted_after => "2014-11-11"
292
- assert_equal([mock_tree], assigns(:trees))
300
+ assert_equal([mock_tree], assigns(:@trees))
293
301
  assert_equal({ :planted_after => parsed }, current_scopes)
294
302
  end
295
303
 
@@ -297,7 +305,7 @@ class HasScopeTest < ActionController::TestCase
297
305
  Tree.expects(:only_really_short!).with(@controller.object_id).returns(Tree)
298
306
  Tree.expects(:all).returns([mock_tree])
299
307
  get :index, :only_short => 'true'
300
- assert_equal([mock_tree], assigns(:trees))
308
+ assert_equal([mock_tree], assigns(:@trees))
301
309
  assert_equal({ :only_short => true }, current_scopes)
302
310
  end
303
311
 
@@ -305,7 +313,7 @@ class HasScopeTest < ActionController::TestCase
305
313
  Tree.expects(:by_given_category).with(@controller.object_id, 'for_id').returns(Tree)
306
314
  Tree.expects(:all).returns([mock_tree])
307
315
  get :index, :by_category => 'for'
308
- assert_equal([mock_tree], assigns(:trees))
316
+ assert_equal([mock_tree], assigns(:@trees))
309
317
  assert_equal({ :by_category => 'for' }, current_scopes)
310
318
  end
311
319
 
@@ -314,9 +322,9 @@ class HasScopeTest < ActionController::TestCase
314
322
  Tree.expects(:title).with('the-title').returns(Tree)
315
323
  Tree.expects(:content).with('the-content').returns(Tree)
316
324
  Tree.expects(:all).returns([mock_tree])
317
- get :index, :q => hash
318
- assert_equal([mock_tree], assigns(:trees))
319
- assert_equal({ :q => hash }, current_scopes)
325
+ get :index, q: hash
326
+ assert_equal([mock_tree], assigns(:@trees))
327
+ assert_equal({ q: hash }, current_scopes)
320
328
  end
321
329
 
322
330
  def test_overwritten_scope
@@ -326,6 +334,13 @@ class HasScopeTest < ActionController::TestCase
326
334
 
327
335
  protected
328
336
 
337
+ if ActionPack::VERSION::MAJOR == 5
338
+ # TODO: Remove this when we only support Rails 5.
339
+ def get(action, params = {})
340
+ super action, params: params
341
+ end
342
+ end
343
+
329
344
  def mock_tree(stubs={})
330
345
  @mock_tree ||= mock(stubs)
331
346
  end
@@ -333,6 +348,10 @@ class HasScopeTest < ActionController::TestCase
333
348
  def current_scopes
334
349
  @controller.send :current_scopes
335
350
  end
351
+
352
+ def assigns(ivar)
353
+ @controller.instance_variable_get(ivar)
354
+ end
336
355
  end
337
356
 
338
357
  class TreeHugger
@@ -8,6 +8,7 @@ require 'mocha/mini_test'
8
8
  ENV['RAILS_ENV'] = 'test'
9
9
 
10
10
  require 'active_support'
11
+ require 'active_support/core_ext/string/strip'
11
12
  require 'action_controller'
12
13
  require 'action_dispatch/middleware/flash'
13
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-08 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
19
+ version: '4.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5'
22
+ version: '5.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '3.2'
29
+ version: '4.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5'
32
+ version: '5.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '3.2'
39
+ version: '4.1'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '5'
42
+ version: '5.1'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '3.2'
49
+ version: '4.1'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '5'
52
+ version: '5.1'
53
53
  description: Maps controller filters to your resource scopes
54
54
  email: opensource@plataformatec.com.br
55
55
  executables: []
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: '0'
79
+ version: 2.1.7
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="
@@ -91,4 +91,3 @@ summary: Maps controller filters to your resource scopes.
91
91
  test_files:
92
92
  - test/has_scope_test.rb
93
93
  - test/test_helper.rb
94
- has_rdoc: