adequate_exposure 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -9
- data/adequate_exposure.gemspec +1 -2
- data/lib/adequate_exposure/exposure.rb +7 -0
- data/lib/adequate_exposure/flow.rb +20 -8
- data/lib/adequate_exposure/version.rb +1 -1
- data/spec/controller_spec.rb +27 -9
- data/spec/support/rails_app.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8ab37daff9fc8399d3a3064a25281e230c8dbad
|
4
|
+
data.tar.gz: 6a904e6c25efc1ba0d761f56a29410ed6f6523ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef93b26d40c676dd4b0b7431dfb91fa1968012965d12b84974704c656c8e028e69f59deda1d150d83a45368a8942a2d9306536c7a9cf537bd126ea1c4e2695cb
|
7
|
+
data.tar.gz: 1cc709f6e906d563cfbfe3fd3ea23e1fe318e3ea9970d4f7bd78d26ab05b00d783489b0eaa0778085d3aedc8c5936e269f6b1962db49c2e4edb7420234a991d7
|
data/README.md
CHANGED
@@ -109,8 +109,14 @@ Or if you (like me) absolutely hate parens in side-effect methods:
|
|
109
109
|
expose :thing, ->{ get_thing_some_way_or_another }
|
110
110
|
```
|
111
111
|
|
112
|
-
|
113
|
-
|
112
|
+
or even shorter
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
expose :thing, :get_thing_some_way_or_another
|
116
|
+
```
|
117
|
+
|
118
|
+
There is another shortcut that allows you to redefine the entire fetch block
|
119
|
+
with less code:
|
114
120
|
|
115
121
|
```ruby
|
116
122
|
expose :comments, from: :post
|
@@ -137,8 +143,8 @@ But nothing is stopping you from throwing in any arbitrary code:
|
|
137
143
|
expose :thing, id: ->{ 42 }
|
138
144
|
```
|
139
145
|
|
140
|
-
Passing lambdas might not always be fun, so here are a couple of shortcuts that
|
141
|
-
help make life easier.
|
146
|
+
Passing lambdas might not always be fun, so here are a couple of shortcuts that
|
147
|
+
could help make life easier.
|
142
148
|
|
143
149
|
```ruby
|
144
150
|
expose :thing, id: :custom_thing_id
|
@@ -178,8 +184,8 @@ expose :thing, find_by: :slug
|
|
178
184
|
|
179
185
|
### `build`
|
180
186
|
|
181
|
-
When an ID is not present, Adequate Exposure tries to build an object for you.
|
182
|
-
default, it behaves like this:
|
187
|
+
When an ID is not present, Adequate Exposure tries to build an object for you.
|
188
|
+
By default, it behaves like this:
|
183
189
|
|
184
190
|
```ruby
|
185
191
|
expose :thing, build: ->(thing_params, scope){ scope.new(thing_params) }
|
@@ -189,8 +195,9 @@ expose :thing, build: ->(thing_params, scope){ scope.new(thing_params) }
|
|
189
195
|
|
190
196
|
These options are responsible for calulating params before passing them to the
|
191
197
|
build step. The default behavior was modeled with Strong Parameters in mind and
|
192
|
-
is somewhat smart: it calls the `thing_params` controller method if it's
|
193
|
-
and the request method
|
198
|
+
is somewhat smart: it calls the `thing_params` controller method if it's
|
199
|
+
available and the request method is not `GET`. In all other cases it produces
|
200
|
+
an empty hash.
|
194
201
|
|
195
202
|
You can easily specify which controller method you want it to call instead of
|
196
203
|
`thing_params`, or just provide your own logic:
|
@@ -255,7 +262,7 @@ expose :thing, decorate: ->(thing){ ThingDecorator.new(thing) }
|
|
255
262
|
|
256
263
|
## `exposure_config`
|
257
264
|
|
258
|
-
You can pre-save some configuration
|
265
|
+
You can pre-save some configuration with `exposure_config` method to reuse it
|
259
266
|
later.
|
260
267
|
|
261
268
|
```ruby
|
data/adequate_exposure.gemspec
CHANGED
@@ -41,6 +41,7 @@ module AdequateExposure
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def normalize_options
|
44
|
+
normalize_fetch_option
|
44
45
|
normalize_with_option
|
45
46
|
normalize_id_option
|
46
47
|
normalize_model_option
|
@@ -51,6 +52,12 @@ module AdequateExposure
|
|
51
52
|
normalize_find_by_option
|
52
53
|
end
|
53
54
|
|
55
|
+
def normalize_fetch_option
|
56
|
+
normalize_non_proc_option :fetch do |method_name|
|
57
|
+
->{ send(method_name) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
54
61
|
def normalize_find_by_option
|
55
62
|
if find_by = options.delete(:find_by)
|
56
63
|
merge_lambda_option :find, ->(id, scope){ scope.find_by!(find_by => id) }
|
@@ -1,14 +1,11 @@
|
|
1
1
|
module AdequateExposure
|
2
2
|
class Flow
|
3
|
-
attr_reader :controller, :options
|
4
|
-
delegate :params, to: :controller
|
3
|
+
attr_reader :controller, :options, :name
|
5
4
|
|
6
5
|
def initialize(controller, options)
|
7
|
-
@controller
|
8
|
-
|
9
|
-
|
10
|
-
def name
|
11
|
-
options.fetch(:name)
|
6
|
+
@controller = controller
|
7
|
+
@options = options
|
8
|
+
@name = options.fetch(:name)
|
12
9
|
end
|
13
10
|
|
14
11
|
%w[fetch find build build_params scope model id decorate].each do |method_name|
|
@@ -28,7 +25,12 @@ module AdequateExposure
|
|
28
25
|
end
|
29
26
|
|
30
27
|
def default_id
|
31
|
-
|
28
|
+
params_id_key_candidates.each do |key|
|
29
|
+
value = params[key]
|
30
|
+
return value if value.present?
|
31
|
+
end
|
32
|
+
|
33
|
+
nil
|
32
34
|
end
|
33
35
|
|
34
36
|
def default_scope(model)
|
@@ -61,6 +63,8 @@ module AdequateExposure
|
|
61
63
|
|
62
64
|
private
|
63
65
|
|
66
|
+
delegate :params, to: :controller
|
67
|
+
|
64
68
|
def get_request?
|
65
69
|
controller.request.get?
|
66
70
|
end
|
@@ -87,5 +91,13 @@ module AdequateExposure
|
|
87
91
|
fail ArgumentError, "Can't handle #{name.inspect} => #{value.inspect} option"
|
88
92
|
end
|
89
93
|
end
|
94
|
+
|
95
|
+
def params_id_key_candidates
|
96
|
+
[ "#{model.name.underscore}_id", "#{name}_id", "id" ].uniq
|
97
|
+
end
|
98
|
+
|
99
|
+
def model_param_key
|
100
|
+
model.name.underscore
|
101
|
+
end
|
90
102
|
end
|
91
103
|
end
|
data/spec/controller_spec.rb
CHANGED
@@ -71,6 +71,7 @@ describe AdequateExposure::Controller do
|
|
71
71
|
exposure_config :sluggable, find_by: :slug
|
72
72
|
exposure_config :weird_id_name, id: :check_this_out
|
73
73
|
exposure_config :another_id_name, id: :whee
|
74
|
+
exposure_config :multi, find_by: :slug, id: :check_this_out
|
74
75
|
controller.params.merge! check_this_out: "foo", whee: "wut"
|
75
76
|
end
|
76
77
|
|
@@ -90,6 +91,16 @@ describe AdequateExposure::Controller do
|
|
90
91
|
expose :thing, with: [:another_id_name, :weird_id_name]
|
91
92
|
expect(Thing).to receive(:find).with("wut").and_return(thing)
|
92
93
|
end
|
94
|
+
|
95
|
+
it "can apply multiple options in a config" do
|
96
|
+
expose :thing, with: :multi
|
97
|
+
expect(Thing).to receive(:find_by!).with(slug: "foo").and_return(thing)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "applies multiple configs with multiple options in a correct order" do
|
101
|
+
expose :thing, with: [:another_id_name, :multi]
|
102
|
+
expect(Thing).to receive(:find_by!).with(slug: "wut").and_return(thing)
|
103
|
+
end
|
93
104
|
end
|
94
105
|
end
|
95
106
|
|
@@ -131,6 +142,14 @@ describe AdequateExposure::Controller do
|
|
131
142
|
end
|
132
143
|
end
|
133
144
|
|
145
|
+
context "passing fetch block as a symbol" do
|
146
|
+
it "is equivalent to passing a block alling controller method" do
|
147
|
+
expose :thing, :calculate_thing_in_controller
|
148
|
+
expect(controller).to receive(:calculate_thing_in_controller).and_return(42)
|
149
|
+
expect(controller.thing).to eq(42)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
134
153
|
context "redefine fetch" do
|
135
154
|
before do
|
136
155
|
expose :thing, fetch: ->{ compute_thing }
|
@@ -187,22 +206,21 @@ describe AdequateExposure::Controller do
|
|
187
206
|
|
188
207
|
context "find" do
|
189
208
|
before do
|
190
|
-
expose :thing
|
191
|
-
expect(
|
209
|
+
expose :thing, model: :different_thing
|
210
|
+
expect(DifferentThing).to receive(:find).with(10)
|
192
211
|
end
|
193
212
|
|
194
213
|
after{ controller.thing }
|
195
214
|
|
196
|
-
it "
|
197
|
-
controller.params.merge! thing_id:
|
215
|
+
it "checks params[:different_thing_id] first" do
|
216
|
+
controller.params.merge! different_thing_id: 10, thing_id: 11, id: 12
|
198
217
|
end
|
199
|
-
|
200
|
-
|
201
|
-
controller.params.merge! id: 10
|
218
|
+
it "checks params[:thing_id] second" do
|
219
|
+
controller.params.merge! thing_id: 10, id: 11
|
202
220
|
end
|
203
221
|
|
204
|
-
it "
|
205
|
-
controller.params.merge! id:
|
222
|
+
it "checks params[:id] in the end" do
|
223
|
+
controller.params.merge! id: 10
|
206
224
|
end
|
207
225
|
end
|
208
226
|
end
|
data/spec/support/rails_app.rb
CHANGED
@@ -24,11 +24,11 @@ end
|
|
24
24
|
class Bird
|
25
25
|
end
|
26
26
|
|
27
|
-
class
|
27
|
+
class ApplicationController < ActionController::Base
|
28
28
|
include Rails.application.routes.url_helpers
|
29
29
|
end
|
30
30
|
|
31
|
-
class BirdsController <
|
31
|
+
class BirdsController < ApplicationController
|
32
32
|
expose :bird
|
33
33
|
|
34
34
|
def show
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adequate_exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Pravosud
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|