adequate_exposure 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 626c4da5b880bf6134c02ee44d83f44df6f57b75
4
- data.tar.gz: 1ec2a29a65c9e16811bea76d76ac33a3b149715a
3
+ metadata.gz: 08518a93835d972d60b024e436f478f894b811c4
4
+ data.tar.gz: 7d0c3042b2d662a44e1d22ec6415fc46c1957078
5
5
  SHA512:
6
- metadata.gz: e3ceb0f0b2246cacb2dee1ee1e3fb6d89e94b034566c5a60233bff3324255b2d484079d68e6aebe62ad797fea0d4016ab5f377600fc7720579728e2748d0813e
7
- data.tar.gz: df20c7691859271f41aaed077b00f3d4a12e589213919a8bf774ccc1e7a962044267b253054682fcf41f3992f7b62fa5bfc43332081e66c3c29215edaec29125
6
+ metadata.gz: 3925a14260bd04bf95bba1e8785595ccb2771526b056f42bcc63e88978ab218520bc911462a61b124cef4df0c8103f08e1a2314c63e5623007552387d5786f8a
7
+ data.tar.gz: 027d750fbafb02bcee26be876daa877e04c033d5656a035c8df117f0289b8ffa5cc3f4becb93563e952f8b1a40ed95c5ac57efd797136eaebaa204af7389ccdd
@@ -17,6 +17,7 @@ module AdequateExposure
17
17
  assert_singleton_option :from
18
18
  assert_incompatible_options_pair :parent, :model
19
19
  assert_incompatible_options_pair :parent, :scope
20
+ assert_incompatible_options_pair :find_by, :find
20
21
 
21
22
  normalize_options
22
23
  end
@@ -70,6 +71,10 @@ module AdequateExposure
70
71
  if from = options.delete(:from)
71
72
  merge_lambda_option :fetch, ->{ send(from).send(exposure_name) }
72
73
  end
74
+
75
+ if find_by = options.delete(:find_by)
76
+ merge_lambda_option :find, ->(id, scope){ scope.find_by!(find_by => id) }
77
+ end
73
78
  end
74
79
 
75
80
  def normalize_non_proc_option(name)
@@ -23,7 +23,8 @@ module AdequateExposure
23
23
 
24
24
  def default_fetch
25
25
  computed_scope = scope(model)
26
- id ? decorate(find(id, computed_scope)) : decorate(build(computed_scope))
26
+ instance = id ? find(id, computed_scope) : build(computed_scope)
27
+ decorate(instance)
27
28
  end
28
29
 
29
30
  def default_id
@@ -1,3 +1,3 @@
1
1
  module AdequateExposure
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -104,7 +104,6 @@ describe AdequateExposure::Controller do
104
104
  end
105
105
 
106
106
  context "default behaviour" do
107
-
108
107
  context "build" do
109
108
  let(:thing){ double("Thing") }
110
109
 
@@ -138,6 +137,12 @@ describe AdequateExposure::Controller do
138
137
  expect(Thing).to receive(:new).with(foo: :bar).and_return(thing)
139
138
  expect(controller).to receive(:custom_params_method_name).and_return(foo: :bar)
140
139
  end
140
+
141
+ it "can use custom build params" do
142
+ expose :thing, build_params: ->{ foobar }
143
+ expect(controller).to receive(:foobar).and_return(42)
144
+ expect(Thing).to receive(:new).with(42).and_return(thing)
145
+ end
141
146
  end
142
147
  end
143
148
 
@@ -163,6 +168,20 @@ describe AdequateExposure::Controller do
163
168
  end
164
169
  end
165
170
 
171
+ context "find_by" do
172
+ it "throws and error when using with :find" do
173
+ action = ->{ expose :thing, find: :foo, find_by: :bar }
174
+ expect(&action).to raise_error(ArgumentError, "Using :find_by option with :find doesn't make sense")
175
+ end
176
+
177
+ it "allows to specify what attribute to use for find" do
178
+ expect(Thing).to receive(:find_by!).with(foo: 10).and_return(42)
179
+ expose :thing, find_by: :foo
180
+ controller.params.merge! id: 10
181
+ expect(controller.thing).to eq(42)
182
+ end
183
+ end
184
+
166
185
  context "parent option" do
167
186
  context "with scope/model options" do
168
187
  it "throws an error when used with scope option" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adequate_exposure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud