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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b88489e2058c7c2fba7667cdc33d64f165b110c
4
- data.tar.gz: c20dc0f0cc013ff4eccdd7545e1a07ee94fc46c7
3
+ metadata.gz: c8ab37daff9fc8399d3a3064a25281e230c8dbad
4
+ data.tar.gz: 6a904e6c25efc1ba0d761f56a29410ed6f6523ab
5
5
  SHA512:
6
- metadata.gz: dd4ff215f7602dd2ce22f1029047d379b8e25180ae8483e1dc963d7a9ea669fe1a3b3e788e8f12d0685e906338d8b748aeac482ce5abfab6c5313bb149ffefbd
7
- data.tar.gz: d5b6b9e3a051f099dd1fa8e30d81cf3a92dc96089f1c664e1a20b77c12065c7d351247cd13ad8546559324e88dd7917ca121da0203d8b154b382ad8cb029278f
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
- There is another shortcut that allows you to redefine the entire fetch block with
113
- less code:
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 could
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. By
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 available
193
- and the request method it not `GET`. In all other cases it produces an empty hash.
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 using `exposure_config` method reuse it
265
+ You can pre-save some configuration with `exposure_config` method to reuse it
259
266
  later.
260
267
 
261
268
  ```ruby
@@ -1,5 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
- require "adequate_exposure/version"
1
+ require File.expand_path("../lib/adequate_exposure/version", __FILE__)
3
2
 
4
3
  Gem::Specification.new do |spec|
5
4
  spec.name = "adequate_exposure"
@@ -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, @options = controller, options.with_indifferent_access
8
- end
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
- params["#{name}_id"] || params[:id]
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
@@ -1,3 +1,3 @@
1
1
  module AdequateExposure
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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(Thing).to receive(:find).with(10)
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 "finds Thing if thing_id param is provided" do
197
- controller.params.merge! thing_id: 10
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
- it "finds Thing if id param if provided" do
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 "prefers :thing_id to :id" do
205
- controller.params.merge! id: 15, thing_id: 10
222
+ it "checks params[:id] in the end" do
223
+ controller.params.merge! id: 10
206
224
  end
207
225
  end
208
226
  end
@@ -24,11 +24,11 @@ end
24
24
  class Bird
25
25
  end
26
26
 
27
- class ApplicationConroller < ActionController::Base
27
+ class ApplicationController < ActionController::Base
28
28
  include Rails.application.routes.url_helpers
29
29
  end
30
30
 
31
- class BirdsController < ApplicationConroller
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.1.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 00:00:00.000000000 Z
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties