decent_exposure 2.0.1 → 2.1.0
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.
- data/README.md +50 -1
- data/lib/decent_exposure/expose.rb +3 -11
- data/lib/decent_exposure/strategizer.rb +3 -15
- data/lib/decent_exposure/strong_parameters_strategy.rb +27 -0
- data/lib/decent_exposure/version.rb +1 -1
- metadata +15 -14
data/README.md
CHANGED
@@ -267,6 +267,22 @@ expose(:article, finder: :find_by_slug)
|
|
267
267
|
expose(:article, finder_parameter: :slug)
|
268
268
|
```
|
269
269
|
|
270
|
+
### Setting a distinct object for a single action
|
271
|
+
|
272
|
+
There are times when one action in a controller is different from the
|
273
|
+
rest of the actions. Rather than putting conditional logic in your
|
274
|
+
exposure block, a better approach is the use the controller's setter
|
275
|
+
methods:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
expose(:articles) { current_user.articles }
|
279
|
+
expose(:article)
|
280
|
+
|
281
|
+
def index
|
282
|
+
self.articles = Article.all
|
283
|
+
end
|
284
|
+
```
|
285
|
+
|
270
286
|
### Getting your hands dirty
|
271
287
|
|
272
288
|
While we try to make things as easy for you as possible, sometimes you just
|
@@ -353,6 +369,39 @@ And opt into it like so:
|
|
353
369
|
expose(:article, config: :sluggable)
|
354
370
|
```
|
355
371
|
|
372
|
+
## Usage with Rails 4 (or strong\_parameters plugin)
|
373
|
+
|
374
|
+
If you're using Rails 4 or
|
375
|
+
[strong\_parameters](https://github.com/rails/strong_parameters), add the
|
376
|
+
following to your ApplicationController:
|
377
|
+
|
378
|
+
|
379
|
+
``` ruby
|
380
|
+
class ApplicationController < ActionController::Base
|
381
|
+
decent_configuration do
|
382
|
+
strategy DecentExposure::StrongParametersStrategy
|
383
|
+
end
|
384
|
+
end
|
385
|
+
```
|
386
|
+
|
387
|
+
Then, when you'd like parameters to be assigned to a model, add the
|
388
|
+
`attributes` option to your exposure:
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
class FooController < ApplicationController
|
392
|
+
expose(:foo, attributes: :foo_params)
|
393
|
+
|
394
|
+
private
|
395
|
+
def foo_params
|
396
|
+
params.require(:foo).permit(:bar, :baz)
|
397
|
+
end
|
398
|
+
end
|
399
|
+
```
|
400
|
+
|
401
|
+
In the example above, `foo_params` will only be called on a PUT, POST or
|
402
|
+
PATCH request.
|
403
|
+
|
404
|
+
|
356
405
|
## Testing
|
357
406
|
|
358
407
|
Controller testing remains trivially easy. The shift is that you now set expectations on methods rather than instance variables. With RSpec, this mostly means avoiding `assign` and `assigns`.
|
@@ -401,4 +450,4 @@ end
|
|
401
450
|
```
|
402
451
|
|
403
452
|
|
404
|
-
[1]: http://blog.voxdolo.me/a-diatribe-on-maintaining-state.html
|
453
|
+
[1]: http://blog.voxdolo.me/a-diatribe-on-maintaining-state.html
|
@@ -5,7 +5,7 @@ module DecentExposure
|
|
5
5
|
module Expose
|
6
6
|
def self.extended(base)
|
7
7
|
base.class_eval do
|
8
|
-
class_attribute :
|
8
|
+
class_attribute :_decent_configurations
|
9
9
|
self._decent_configurations ||= Hash.new(Configuration.new)
|
10
10
|
|
11
11
|
def _resources
|
@@ -20,15 +20,7 @@ module DecentExposure
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def decent_configuration(name=:default,&block)
|
23
|
-
_decent_configurations
|
24
|
-
end
|
25
|
-
|
26
|
-
def default_exposure(&block)
|
27
|
-
warn "[DEPRECATION] `default_exposure` is deprecated, and will " \
|
28
|
-
"be removed in DecentExposure 2.1 without a replacement. Please " \
|
29
|
-
"use a custom strategy instead.\n" \
|
30
|
-
"#{caller.first}"
|
31
|
-
self._default_exposure = block
|
23
|
+
self._decent_configurations = _decent_configurations.merge(name => Configuration.new(&block))
|
32
24
|
end
|
33
25
|
|
34
26
|
def expose!(*args, &block)
|
@@ -36,7 +28,7 @@ module DecentExposure
|
|
36
28
|
expose(*args, &block)
|
37
29
|
end
|
38
30
|
|
39
|
-
def expose(name, options={
|
31
|
+
def expose(name, options={}, &block)
|
40
32
|
if ActionController::Base.instance_methods.include?(name.to_sym)
|
41
33
|
Kernel.warn "[WARNING] You are exposing the `#{name}` method, " \
|
42
34
|
"which overrides an existing ActionController method of the same name. " \
|
@@ -1,22 +1,20 @@
|
|
1
1
|
require 'decent_exposure/exposure'
|
2
2
|
require 'decent_exposure/active_record_with_eager_attributes_strategy'
|
3
|
+
require 'decent_exposure/strong_parameters_strategy'
|
3
4
|
|
4
5
|
module DecentExposure
|
5
6
|
class Strategizer
|
6
|
-
attr_accessor :name, :block, :
|
7
|
+
attr_accessor :name, :block, :options, :custom_strategy_class
|
7
8
|
|
8
9
|
def initialize(name, options={})
|
9
10
|
self.name = name
|
10
|
-
self.default_exposure = options.delete(:default_exposure)
|
11
11
|
self.custom_strategy_class = options.delete(:strategy)
|
12
12
|
self.options = options
|
13
13
|
self.block = Proc.new if block_given?
|
14
14
|
end
|
15
15
|
|
16
16
|
def strategy
|
17
|
-
[block_strategy,
|
18
|
-
default_exposure_strategy,
|
19
|
-
exposure_strategy].detect(&applicable)
|
17
|
+
[block_strategy, exposure_strategy].detect(&applicable)
|
20
18
|
end
|
21
19
|
|
22
20
|
def model
|
@@ -37,21 +35,11 @@ module DecentExposure
|
|
37
35
|
BlockStrategy.new(block) if block
|
38
36
|
end
|
39
37
|
|
40
|
-
def default_exposure_strategy
|
41
|
-
DefaultStrategy.new(name, default_exposure) if default_exposure
|
42
|
-
end
|
43
|
-
|
44
38
|
def exposure_strategy_class
|
45
39
|
custom_strategy_class || ActiveRecordWithEagerAttributesStrategy
|
46
40
|
end
|
47
41
|
end
|
48
42
|
|
49
|
-
DefaultStrategy = Struct.new(:name, :block) do
|
50
|
-
def call(controller)
|
51
|
-
controller.instance_exec(name, &block)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
43
|
BlockStrategy = Struct.new(:block) do
|
56
44
|
def call(controller)
|
57
45
|
controller.instance_eval(&block)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'decent_exposure/active_record_strategy'
|
2
|
+
|
3
|
+
module DecentExposure
|
4
|
+
class StrongParametersStrategy < ActiveRecordStrategy
|
5
|
+
delegate :get?, :to => :request
|
6
|
+
delegate :delete?, :to => :request
|
7
|
+
|
8
|
+
def singular?
|
9
|
+
!plural?
|
10
|
+
end
|
11
|
+
|
12
|
+
def attributes
|
13
|
+
return @attributes if defined?(@attributes)
|
14
|
+
@attributes = controller.send(options[:attributes]) if options[:attributes]
|
15
|
+
end
|
16
|
+
|
17
|
+
def assign_attributes?
|
18
|
+
singular? && !get? && !delete? && attributes.present?
|
19
|
+
end
|
20
|
+
|
21
|
+
def resource
|
22
|
+
super.tap do |r|
|
23
|
+
r.attributes = attributes if assign_attributes?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: decent_exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.0
|
5
|
+
version: 2.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Stephen Caudill
|
@@ -11,14 +11,14 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
version_requirements: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '2.
|
21
|
+
version: '2.11'
|
22
22
|
none: false
|
23
23
|
name: rspec
|
24
24
|
type: :development
|
@@ -27,14 +27,14 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: '2.
|
30
|
+
version: '2.11'
|
31
31
|
none: false
|
32
32
|
- !ruby/object:Gem::Dependency
|
33
33
|
version_requirements: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
35
|
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: '2.
|
37
|
+
version: '2.11'
|
38
38
|
none: false
|
39
39
|
name: rspec-rails
|
40
40
|
type: :development
|
@@ -43,39 +43,39 @@ dependencies:
|
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '2.
|
46
|
+
version: '2.11'
|
47
47
|
none: false
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 3.1.0
|
54
54
|
none: false
|
55
55
|
name: actionpack
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ! '>='
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
62
|
+
version: 3.1.0
|
63
63
|
none: false
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ! '>='
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
69
|
+
version: 3.1.0
|
70
70
|
none: false
|
71
71
|
name: activesupport
|
72
72
|
type: :development
|
73
73
|
prerelease: false
|
74
74
|
requirement: !ruby/object:Gem::Requirement
|
75
75
|
requirements:
|
76
|
-
- -
|
76
|
+
- - ! '>='
|
77
77
|
- !ruby/object:Gem::Version
|
78
|
-
version:
|
78
|
+
version: 3.1.0
|
79
79
|
none: false
|
80
80
|
description: ! "\n DecentExposure helps you program to an interface, rather than
|
81
81
|
an\n implementation in your Rails controllers. The fact of the matter is that\n
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- lib/decent_exposure/inflector.rb
|
97
97
|
- lib/decent_exposure/strategizer.rb
|
98
98
|
- lib/decent_exposure/strategy.rb
|
99
|
+
- lib/decent_exposure/strong_parameters_strategy.rb
|
99
100
|
- lib/decent_exposure/version.rb
|
100
101
|
- lib/decent_exposure.rb
|
101
102
|
- README.md
|