adequate_exposure 0.0.2 → 0.0.3

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: 82a9cfe00dccb013cca09b85f68073902bb5928d
4
- data.tar.gz: 15ec7db35c84401f82a3d8bab5af8079ccf5ea81
3
+ metadata.gz: 626c4da5b880bf6134c02ee44d83f44df6f57b75
4
+ data.tar.gz: 1ec2a29a65c9e16811bea76d76ac33a3b149715a
5
5
  SHA512:
6
- metadata.gz: 8ade244f9a2785594a66b5ba042b2639a15157eb39f72234f3e2133d21a27bffe2dcecf3d61a6a79d9768b4a0d6ab5605cd3336600c3d49e848767708385c22a
7
- data.tar.gz: 84260440dbd08e8444ce307994f4c564e97b76efc48a5e554d349a5018ba8c3d7d4ec0cf7801d860876cd306ba014ef1b5c2ff0f396b3ded2fdbe1faaedd7660
6
+ metadata.gz: e3ceb0f0b2246cacb2dee1ee1e3fb6d89e94b034566c5a60233bff3324255b2d484079d68e6aebe62ad797fea0d4016ab5f377600fc7720579728e2748d0813e
7
+ data.tar.gz: df20c7691859271f41aaed077b00f3d4a12e589213919a8bf774ccc1e7a962044267b253054682fcf41f3992f7b62fa5bfc43332081e66c3c29215edaec29125
data/README.md CHANGED
@@ -36,11 +36,12 @@ expressed with the following pseudocode:
36
36
 
37
37
  ```ruby
38
38
  def fetch(scope, id)
39
- id ? decorate(find(id, scope)) : decorate(build(scope))
39
+ instance = id ? find(id, scope) : build(scope)
40
+ decorate(instance)
40
41
  end
41
42
 
42
43
  def id
43
- params[:thing_id] || params[:id]
44
+ params[:id] || params[:thing_id]
44
45
  end
45
46
 
46
47
  def find(id, scope)
@@ -48,7 +49,7 @@ def find(id, scope)
48
49
  end
49
50
 
50
51
  def build(scope)
51
- scope.new # Thing.new
52
+ scope.new(build_params) # Thing.new(thing_params)
52
53
  end
53
54
 
54
55
  def scope
@@ -59,6 +60,14 @@ def model
59
60
  exposure_name.classify.constantize # :thing -> Thing
60
61
  end
61
62
 
63
+ def build_params
64
+ if respond_to?(:thing_params, true) && !request.get?
65
+ thing_params
66
+ else
67
+ {}
68
+ end
69
+ end
70
+
62
71
  def decorate(thing)
63
72
  thing
64
73
  end
@@ -54,8 +54,9 @@ module AdequateExposure
54
54
  ->{ model }
55
55
  end
56
56
 
57
- normalize_non_proc_option :build do |params_method_name|
58
- ->(scope){ scope.new(send(params_method_name)) }
57
+ normalize_non_proc_option :build_params do |value|
58
+ options[:build_params_method] = value
59
+ nil
59
60
  end
60
61
 
61
62
  normalize_non_proc_option :scope do |custom_scope|
@@ -75,7 +76,12 @@ module AdequateExposure
75
76
  option_value = options[name]
76
77
  return if Proc === option_value
77
78
  if option_value.present?
78
- merge_lambda_option name, yield(option_value)
79
+ normalized_value = yield(option_value)
80
+ if normalized_value
81
+ merge_lambda_option name, normalized_value
82
+ else
83
+ options.delete name
84
+ end
79
85
  end
80
86
  end
81
87
 
@@ -11,7 +11,7 @@ module AdequateExposure
11
11
  options.fetch(:name)
12
12
  end
13
13
 
14
- %i[fetch find build scope model id decorate].each do |method_name|
14
+ %i[fetch find build build_params scope model id decorate].each do |method_name|
15
15
  define_method method_name do |*args|
16
16
  ivar_name = "@#{method_name}"
17
17
  return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)
@@ -43,17 +43,15 @@ module AdequateExposure
43
43
  end
44
44
 
45
45
  def default_build(scope)
46
- scope.new(exposure_params)
46
+ scope.new(build_params)
47
47
  end
48
48
 
49
49
  def default_decorate(instance)
50
50
  instance
51
51
  end
52
52
 
53
- def exposure_params
54
- params_method_name = "#{name}_params"
55
-
56
- if controller.respond_to?(params_method_name, true)
53
+ def default_build_params
54
+ if controller.respond_to?(params_method_name, true) && !get_request?
57
55
  controller.send(params_method_name)
58
56
  else
59
57
  {}
@@ -62,6 +60,14 @@ module AdequateExposure
62
60
 
63
61
  private
64
62
 
63
+ def get_request?
64
+ controller.request.get?
65
+ end
66
+
67
+ def params_method_name
68
+ options.fetch(:build_params_method){ "#{name}_params" }
69
+ end
70
+
65
71
  def handle_action(name, *args)
66
72
  if options.key?(name)
67
73
  handle_custom_action(name, *args)
@@ -1,3 +1,3 @@
1
1
  module AdequateExposure
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -18,10 +18,14 @@ describe AdequateExposure::Controller do
18
18
  end
19
19
  end
20
20
 
21
+ let(:request){ double("Request") }
21
22
  let(:controller){ controller_klass.new }
23
+ before{ allow(controller).to receive(:request){ request } }
22
24
 
23
- def expose(*args, &block)
24
- controller_klass.expose(*args, &block)
25
+ %i[expose expose!].each do |method_name|
26
+ define_method method_name do |*args, &block|
27
+ controller_klass.send method_name, *args, &block
28
+ end
25
29
  end
26
30
 
27
31
  context "getter/setter methods" do
@@ -46,7 +50,7 @@ describe AdequateExposure::Controller do
46
50
  context ".expose!" do
47
51
  it "supports eager expose" do
48
52
  expect(controller_klass).to receive(:before_action).with(:thing)
49
- controller_klass.expose! :thing
53
+ expose! :thing
50
54
  end
51
55
  end
52
56
 
@@ -106,21 +110,34 @@ describe AdequateExposure::Controller do
106
110
 
107
111
  after{ expect(controller.thing).to eq(thing) }
108
112
 
109
- it "builds a new instance with empty hash when strong parameters method is not available" do
110
- expose :thing
111
- expect(Thing).to receive(:new).with({}).and_return(thing)
113
+ context "params method is not available" do
114
+ it "builds a new instance with empty hash" do
115
+ expose :thing
116
+ expect(Thing).to receive(:new).with({}).and_return(thing)
117
+ end
112
118
  end
113
119
 
114
- it "builds a new instance with attributes when strong parameters method is available" do
115
- expose :thing
116
- expect(Thing).to receive(:new).with(foo: :bar).and_return(thing)
117
- expect(controller).to receive(:thing_params).and_return(foo: :bar)
118
- end
119
-
120
- it "allows to specify strong parameters method name with a symbol passed to build option" do
121
- expose :thing, build: :custom_params_method_name
122
- expect(Thing).to receive(:new).with(foo: :bar).and_return(thing)
123
- expect(controller).to receive(:custom_params_method_name).and_return(foo: :bar)
120
+ context "params method is available" do
121
+ it "ignores params on get request" do
122
+ expose :thing
123
+ expect(request).to receive(:get?).and_return(true)
124
+ expect(controller).not_to receive(:thing_params)
125
+ expect(Thing).to receive(:new).with({}).and_return(thing)
126
+ end
127
+
128
+ it "uses params method on non-get request" do
129
+ expose :thing
130
+ expect(request).to receive(:get?).and_return(false)
131
+ expect(Thing).to receive(:new).with(foo: :bar).and_return(thing)
132
+ expect(controller).to receive(:thing_params).and_return(foo: :bar)
133
+ end
134
+
135
+ it "can use custom params method name" do
136
+ expose :thing, build_params: :custom_params_method_name
137
+ expect(request).to receive(:get?).and_return(false)
138
+ expect(Thing).to receive(:new).with(foo: :bar).and_return(thing)
139
+ expect(controller).to receive(:custom_params_method_name).and_return(foo: :bar)
140
+ end
124
141
  end
125
142
  end
126
143
 
@@ -139,6 +156,10 @@ describe AdequateExposure::Controller do
139
156
  it "finds Thing if id param if provided" do
140
157
  controller.params.merge! id: 10
141
158
  end
159
+
160
+ it "prefers :thing_id to :id" do
161
+ controller.params.merge! id: 15, thing_id: 10
162
+ end
142
163
  end
143
164
  end
144
165
 
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.0.2
4
+ version: 0.0.3
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-03 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties