decent_exposure 3.0.0.beta1 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +12 -0
- data/CHANGELOG.md +19 -0
- data/Gemfile +2 -0
- data/README.md +58 -7
- data/decent_exposure.gemspec +16 -16
- data/gemfiles/Gemfile.rails-4.2.0 +5 -0
- data/gemfiles/Gemfile.rails-4.2.6 +5 -0
- data/gemfiles/Gemfile.rails-5.0.0 +5 -0
- data/lib/decent_exposure.rb +12 -5
- data/lib/decent_exposure/attribute.rb +0 -1
- data/lib/decent_exposure/behavior.rb +1 -1
- data/lib/decent_exposure/context.rb +2 -2
- data/lib/decent_exposure/controller.rb +4 -4
- data/lib/decent_exposure/exposure.rb +24 -23
- data/lib/decent_exposure/flow.rb +1 -2
- data/lib/decent_exposure/mailer.rb +15 -0
- data/lib/decent_exposure/version.rb +1 -1
- data/lib/generators/decent_exposure/USAGE +13 -0
- data/lib/generators/decent_exposure/scaffold_templates_generator.rb +46 -0
- data/lib/generators/decent_exposure/templates/_form.html.erb +34 -0
- data/lib/generators/decent_exposure/templates/_form.html.haml +15 -0
- data/lib/generators/decent_exposure/templates/controller.rb +41 -0
- data/lib/generators/decent_exposure/templates/edit.html.erb +6 -0
- data/lib/generators/decent_exposure/templates/edit.html.haml +7 -0
- data/lib/generators/decent_exposure/templates/index.html.erb +31 -0
- data/lib/generators/decent_exposure/templates/index.html.haml +25 -0
- data/lib/generators/decent_exposure/templates/new.html.erb +5 -0
- data/lib/generators/decent_exposure/templates/new.html.haml +5 -0
- data/lib/generators/decent_exposure/templates/show.html.erb +11 -0
- data/lib/generators/decent_exposure/templates/show.html.haml +11 -0
- data/spec/{controller_spec.rb → decent_exposure/controller_spec.rb} +45 -39
- data/spec/features/api_birds_controller_spec.rb +70 -0
- data/spec/features/birds_controller_spec.rb +70 -0
- data/spec/features/birds_mailer_spec.rb +54 -0
- data/spec/generators/decent_exposure/scaffold_templates_generator_spec.rb +45 -0
- data/spec/support/rails_app.rb +39 -7
- metadata +57 -35
- data/hashrocket_logo.png +0 -0
- data/spec/integration_spec.rb +0 -26
data/lib/decent_exposure/flow.rb
CHANGED
@@ -46,7 +46,7 @@ module DecentExposure
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def params_method_name
|
49
|
-
options.fetch(:build_params_method){ "#{name}_params" }
|
49
|
+
options.fetch(:build_params_method) { "#{name}_params" }
|
50
50
|
end
|
51
51
|
|
52
52
|
def handle_flow_method(name, *args, &block)
|
@@ -75,7 +75,6 @@ module DecentExposure
|
|
75
75
|
method.bind(self).call(*args, &block)
|
76
76
|
end
|
77
77
|
|
78
|
-
|
79
78
|
def fetch_ivar(name)
|
80
79
|
ivar_name = "@#{name}"
|
81
80
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DecentExposure
|
2
|
+
module Mailer
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
attr_accessor :params
|
6
|
+
|
7
|
+
def process_action(*args)
|
8
|
+
arg = args.second
|
9
|
+
self.params = arg.stringify_keys if arg && Hash === arg
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Description:
|
2
|
+
Generate DecentExposure scaffold template files
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate decent_exposure:scaffold_templates [--template_engine erb|haml]
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
lib/templates/rails/scaffold_controller/controller.rb
|
9
|
+
lib/templates/erb/scaffold/_form.html.erb
|
10
|
+
lib/templates/erb/scaffold/edit.html.erb
|
11
|
+
lib/templates/erb/scaffold/index.html.erb
|
12
|
+
lib/templates/erb/scaffold/new.html.erb
|
13
|
+
lib/templates/erb/scaffold/show.html.erb
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module DecentExposure
|
4
|
+
module Generators
|
5
|
+
class ScaffoldTemplatesGenerator < Rails::Generators::Base
|
6
|
+
desc "Generate DecentExposure scaffold template files"
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
class_option :template_engine, desc: "Template engine to be invoked (erb)."
|
9
|
+
|
10
|
+
VIEWS = %i[_form edit index new show]
|
11
|
+
AVAILABLE_ENGINES = %w[erb haml]
|
12
|
+
|
13
|
+
def generate
|
14
|
+
validate_template_engine
|
15
|
+
|
16
|
+
generate_controller
|
17
|
+
VIEWS.each { |view| generate_view(view) }
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def generate_controller
|
23
|
+
copy_template("rails/scaffold_controller", "controller.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate_view(view)
|
27
|
+
copy_template("#{engine}/scaffold", "#{view}.html.#{engine}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def copy_template(generator, file)
|
31
|
+
copy_file(file, "lib/templates/#{generator}/#{file}")
|
32
|
+
end
|
33
|
+
|
34
|
+
def engine
|
35
|
+
options[:template_engine]
|
36
|
+
end
|
37
|
+
|
38
|
+
def validate_template_engine
|
39
|
+
unless AVAILABLE_ENGINES.include?(engine.to_s)
|
40
|
+
message = "ERROR: template_engine must be: #{AVAILABLE_ENGINES}."
|
41
|
+
raise ArgumentError, message
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
<%%= form_for(<%= singular_table_name %>) do |f| %>
|
2
|
+
<%% if <%= singular_table_name %>.errors.any? %>
|
3
|
+
<div id="error_explanation">
|
4
|
+
<h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
|
5
|
+
|
6
|
+
<ul>
|
7
|
+
<%% <%= singular_table_name %>.errors.full_messages.each do |message| %>
|
8
|
+
<li><%%= message %></li>
|
9
|
+
<%% end %>
|
10
|
+
</ul>
|
11
|
+
</div>
|
12
|
+
<%% end %>
|
13
|
+
|
14
|
+
<% attributes.each do |attribute| -%>
|
15
|
+
<div class="field">
|
16
|
+
<% if attribute.password_digest? -%>
|
17
|
+
<%%= f.label :password %>
|
18
|
+
<%%= f.password_field :password %>
|
19
|
+
</div>
|
20
|
+
|
21
|
+
<div class="field">
|
22
|
+
<%%= f.label :password_confirmation %>
|
23
|
+
<%%= f.password_field :password_confirmation %>
|
24
|
+
<% else -%>
|
25
|
+
<%%= f.label :<%= attribute.column_name %> %>
|
26
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %>
|
27
|
+
<% end -%>
|
28
|
+
</div>
|
29
|
+
|
30
|
+
<% end -%>
|
31
|
+
<div class="actions">
|
32
|
+
<%%= f.submit %>
|
33
|
+
</div>
|
34
|
+
<%% end %>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
= form_for <%= singular_table_name %> do |f|
|
2
|
+
- if <%= singular_table_name %>.errors.any?
|
3
|
+
#error_explanation
|
4
|
+
%h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:"
|
5
|
+
%ul
|
6
|
+
- <%= singular_table_name %>.errors.full_messages.each do |msg|
|
7
|
+
%li= msg
|
8
|
+
|
9
|
+
<% for attribute in attributes -%>
|
10
|
+
.field
|
11
|
+
= f.label :<%= attribute.name %>
|
12
|
+
= f.<%= attribute.field_type %> :<%= attribute.name %>
|
13
|
+
<% end -%>
|
14
|
+
.actions
|
15
|
+
= f.submit 'Save'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
+
expose :<%= plural_table_name %>, ->{ <%= orm_class.all(class_name) %> }
|
8
|
+
expose :<%= singular_table_name %>
|
9
|
+
|
10
|
+
def create
|
11
|
+
if <%= orm_instance.save %>
|
12
|
+
redirect_to <%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
|
13
|
+
else
|
14
|
+
render :new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def update
|
19
|
+
if <%= orm_instance.update("#{singular_table_name}_params") %>
|
20
|
+
redirect_to <%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
|
21
|
+
else
|
22
|
+
render :edit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def destroy
|
27
|
+
<%= orm_instance.destroy %>
|
28
|
+
redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def <%= "#{singular_table_name}_params" %>
|
34
|
+
<%- if attributes_names.empty? -%>
|
35
|
+
params.fetch(:<%= singular_table_name %>, {})
|
36
|
+
<%- else -%>
|
37
|
+
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
38
|
+
<%- end -%>
|
39
|
+
end
|
40
|
+
end
|
41
|
+
<% end -%>
|
@@ -0,0 +1,31 @@
|
|
1
|
+
<p id="notice"><%%= notice %></p>
|
2
|
+
|
3
|
+
<h1><%= plural_table_name.titleize %></h1>
|
4
|
+
|
5
|
+
<table>
|
6
|
+
<thead>
|
7
|
+
<tr>
|
8
|
+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
|
9
|
+
<th><%= attribute.human_name %></th>
|
10
|
+
<% end -%>
|
11
|
+
<th colspan="3"></th>
|
12
|
+
</tr>
|
13
|
+
</thead>
|
14
|
+
|
15
|
+
<tbody>
|
16
|
+
<%% <%= plural_table_name %>.each do |<%= singular_table_name %>| %>
|
17
|
+
<tr>
|
18
|
+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
|
19
|
+
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
|
20
|
+
<% end -%>
|
21
|
+
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
|
22
|
+
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
|
23
|
+
<td><%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
24
|
+
</tr>
|
25
|
+
<%% end %>
|
26
|
+
</tbody>
|
27
|
+
</table>
|
28
|
+
|
29
|
+
<br>
|
30
|
+
|
31
|
+
<%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path %>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
%h1 Listing <%= plural_table_name %>
|
2
|
+
|
3
|
+
%table
|
4
|
+
%thead
|
5
|
+
%tr
|
6
|
+
<% for attribute in attributes -%>
|
7
|
+
%th <%= attribute.human_name %>
|
8
|
+
<% end -%>
|
9
|
+
%th
|
10
|
+
%th
|
11
|
+
%th
|
12
|
+
|
13
|
+
%tbody
|
14
|
+
- <%= plural_table_name %>.each do |<%= singular_table_name %>|
|
15
|
+
%tr
|
16
|
+
<% for attribute in attributes -%>
|
17
|
+
%td= <%= singular_table_name %>.<%= attribute.name %>
|
18
|
+
<% end -%>
|
19
|
+
%td= link_to 'Show', <%= singular_table_name %>
|
20
|
+
%td= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>)
|
21
|
+
%td= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' }
|
22
|
+
|
23
|
+
%br
|
24
|
+
|
25
|
+
= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<p id="notice"><%%= notice %></p>
|
2
|
+
|
3
|
+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
|
4
|
+
<p>
|
5
|
+
<strong><%= attribute.human_name %>:</strong>
|
6
|
+
<%%= <%= singular_table_name %>.<%= attribute.name %> %>
|
7
|
+
</p>
|
8
|
+
|
9
|
+
<% end -%>
|
10
|
+
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %> |
|
11
|
+
<%%= link_to 'Back', <%= index_helper %>_path %>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
%p#notice= notice
|
2
|
+
|
3
|
+
<% for attribute in attributes -%>
|
4
|
+
%p
|
5
|
+
%b <%= attribute.human_name %>:
|
6
|
+
= <%= singular_table_name %>.<%= attribute.name %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>)
|
10
|
+
\|
|
11
|
+
= link_to 'Back', <%= index_helper %>_path
|
@@ -1,11 +1,12 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe DecentExposure::Controller do
|
3
|
+
RSpec.describe DecentExposure::Controller do
|
4
4
|
class Thing; end
|
5
5
|
class DifferentThing; end
|
6
6
|
|
7
7
|
class BaseController
|
8
|
-
def self.helper_method(*)
|
8
|
+
def self.helper_method(*)
|
9
|
+
end
|
9
10
|
|
10
11
|
def params
|
11
12
|
@params ||= HashWithIndifferentAccess.new
|
@@ -18,18 +19,18 @@ describe DecentExposure::Controller do
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
|
-
let(:request){ double("Request") }
|
22
|
-
let(:controller){ controller_klass.new }
|
23
|
-
before{ allow(controller).to receive(:request){ request } }
|
22
|
+
let(:request) { double("Request") }
|
23
|
+
let(:controller) { controller_klass.new }
|
24
|
+
before { allow(controller).to receive(:request) { request } }
|
24
25
|
|
25
26
|
%w[expose expose! exposure_config].each do |method_name|
|
26
|
-
define_method method_name do |*args, &block|
|
27
|
-
controller_klass.send method_name, *args, &block
|
27
|
+
define_method method_name do |*args, **options, &block|
|
28
|
+
controller_klass.send method_name, *args, **options, &block
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
context "getter/setter methods" do
|
32
|
-
before{ expose :thing }
|
33
|
+
before { expose :thing }
|
33
34
|
|
34
35
|
it "defines getter method" do
|
35
36
|
expect(controller).to respond_to(:thing)
|
@@ -41,8 +42,13 @@ describe DecentExposure::Controller do
|
|
41
42
|
end
|
42
43
|
|
43
44
|
context "helper methods" do
|
44
|
-
it "exposes getter
|
45
|
-
expect(controller_klass).to receive(:helper_method).with(:thing
|
45
|
+
it "exposes getter as controller helper methods" do
|
46
|
+
expect(controller_klass).to receive(:helper_method).with(:thing)
|
47
|
+
expose :thing
|
48
|
+
end
|
49
|
+
|
50
|
+
it "does not expose setter as controller helper methods" do
|
51
|
+
expect(controller_klass).to_not receive(:helper_method).with(:thing=)
|
46
52
|
expose :thing
|
47
53
|
end
|
48
54
|
end
|
@@ -65,7 +71,7 @@ describe DecentExposure::Controller do
|
|
65
71
|
end
|
66
72
|
|
67
73
|
context "applying" do
|
68
|
-
let(:thing){ double("Thing") }
|
74
|
+
let(:thing) { double("Thing") }
|
69
75
|
|
70
76
|
before do
|
71
77
|
exposure_config :sluggable, find_by: :slug
|
@@ -75,7 +81,7 @@ describe DecentExposure::Controller do
|
|
75
81
|
controller.params.merge! check_this_out: "foo", whee: "wut"
|
76
82
|
end
|
77
83
|
|
78
|
-
after{ expect(controller.thing).to eq(thing) }
|
84
|
+
after { expect(controller.thing).to eq(thing) }
|
79
85
|
|
80
86
|
it "can be reused later" do
|
81
87
|
expose :thing, with: :weird_id_name
|
@@ -105,7 +111,7 @@ describe DecentExposure::Controller do
|
|
105
111
|
end
|
106
112
|
|
107
113
|
context "with block" do
|
108
|
-
before{ expose(:thing){ compute_thing } }
|
114
|
+
before { expose(:thing) { compute_thing } }
|
109
115
|
|
110
116
|
it "executes block to calculate the value" do
|
111
117
|
allow(controller).to receive(:compute_thing).and_return(42)
|
@@ -114,7 +120,7 @@ describe DecentExposure::Controller do
|
|
114
120
|
|
115
121
|
it "executes the block once and memoizes the result" do
|
116
122
|
expect(controller).to receive(:compute_thing).once.and_return(42)
|
117
|
-
10.times{ controller.thing }
|
123
|
+
10.times { controller.thing }
|
118
124
|
end
|
119
125
|
|
120
126
|
it "allows setting value directly" do
|
@@ -124,20 +130,20 @@ describe DecentExposure::Controller do
|
|
124
130
|
end
|
125
131
|
|
126
132
|
it "throws and error when providing options with block" do
|
127
|
-
action = ->{ expose(:thing, id: :some_id){ some_code } }
|
133
|
+
action = -> { expose(:thing, id: :some_id) { some_code } }
|
128
134
|
expect(&action).to raise_error(ArgumentError, "Using :fetch option with other options doesn't make sense")
|
129
135
|
end
|
130
136
|
end
|
131
137
|
|
132
138
|
context "passing fetch block as an argument instead of block" do
|
133
139
|
it "is equivalent to passing block" do
|
134
|
-
expose :thing, ->{ compute_thing }
|
140
|
+
expose :thing, -> { compute_thing }
|
135
141
|
expect(controller).to receive(:compute_thing).and_return(42)
|
136
142
|
expect(controller.thing).to eq(42)
|
137
143
|
end
|
138
144
|
|
139
145
|
it "throws an error when passing both block and block-argument" do
|
140
|
-
action = ->{ expose(:thing, ->{}){} }
|
146
|
+
action = -> { expose(:thing, -> {}) {} }
|
141
147
|
expect(&action).to raise_error(ArgumentError, "Fetch block is already defined")
|
142
148
|
end
|
143
149
|
end
|
@@ -152,7 +158,7 @@ describe DecentExposure::Controller do
|
|
152
158
|
|
153
159
|
context "redefine fetch" do
|
154
160
|
before do
|
155
|
-
expose :thing, fetch: ->{ compute_thing }
|
161
|
+
expose :thing, fetch: -> { compute_thing }
|
156
162
|
allow(controller).to receive(:compute_thing).and_return(42)
|
157
163
|
end
|
158
164
|
|
@@ -163,9 +169,9 @@ describe DecentExposure::Controller do
|
|
163
169
|
|
164
170
|
context "default behaviour" do
|
165
171
|
context "build" do
|
166
|
-
let(:thing){ double("Thing") }
|
172
|
+
let(:thing) { double("Thing") }
|
167
173
|
|
168
|
-
after{ expect(controller.thing).to eq(thing) }
|
174
|
+
after { expect(controller.thing).to eq(thing) }
|
169
175
|
|
170
176
|
context "params method is not available" do
|
171
177
|
it "builds a new instance with empty hash" do
|
@@ -197,7 +203,7 @@ describe DecentExposure::Controller do
|
|
197
203
|
end
|
198
204
|
|
199
205
|
it "can use custom build params" do
|
200
|
-
expose :thing, build_params: ->{ foobar }
|
206
|
+
expose :thing, build_params: -> { foobar }
|
201
207
|
expect(controller).to receive(:foobar).and_return(42)
|
202
208
|
expect(Thing).to receive(:new).with(42).and_return(thing)
|
203
209
|
end
|
@@ -210,7 +216,7 @@ describe DecentExposure::Controller do
|
|
210
216
|
expect(DifferentThing).to receive(:find).with(10)
|
211
217
|
end
|
212
218
|
|
213
|
-
after{ controller.thing }
|
219
|
+
after { controller.thing }
|
214
220
|
|
215
221
|
it "checks params[:different_thing_id] first" do
|
216
222
|
controller.params.merge! different_thing_id: 10, thing_id: 11, id: 12
|
@@ -227,14 +233,14 @@ describe DecentExposure::Controller do
|
|
227
233
|
|
228
234
|
context "find_by" do
|
229
235
|
it "throws and error when using with :find" do
|
230
|
-
action = ->{ expose :thing, find: :foo, find_by: :bar }
|
236
|
+
action = -> { expose :thing, find: :foo, find_by: :bar }
|
231
237
|
expect(&action).to raise_error(ArgumentError, "Using :find_by option with :find doesn't make sense")
|
232
238
|
end
|
233
239
|
|
234
240
|
it "allows to specify what attribute to use for find" do
|
235
241
|
expect(Thing).to receive(:find_by!).with(foo: 10).and_return(42)
|
236
242
|
expose :thing, find_by: :foo
|
237
|
-
controller.params
|
243
|
+
controller.params[:id] = 10
|
238
244
|
expect(controller.thing).to eq(42)
|
239
245
|
end
|
240
246
|
end
|
@@ -242,19 +248,19 @@ describe DecentExposure::Controller do
|
|
242
248
|
context "parent option" do
|
243
249
|
context "with scope/model options" do
|
244
250
|
it "throws an error when used with scope option" do
|
245
|
-
action = ->{ expose :thing, scope: :foo, parent: :something }
|
251
|
+
action = -> { expose :thing, scope: :foo, parent: :something }
|
246
252
|
expect(&action).to raise_error(ArgumentError, "Using :parent option with :scope doesn't make sense")
|
247
253
|
end
|
248
254
|
|
249
255
|
it "throws an error when used with model option" do
|
250
|
-
action = ->{ expose :thing, model: :foo, parent: :something }
|
256
|
+
action = -> { expose :thing, model: :foo, parent: :something }
|
251
257
|
expect(&action).to raise_error(ArgumentError, "Using :parent option with :model doesn't make sense")
|
252
258
|
end
|
253
259
|
end
|
254
260
|
|
255
261
|
context "build/find" do
|
256
|
-
let(:current_user){ double("User") }
|
257
|
-
let(:scope){ double("Scope") }
|
262
|
+
let(:current_user) { double("User") }
|
263
|
+
let(:scope) { double("Scope") }
|
258
264
|
|
259
265
|
before do
|
260
266
|
expect(controller).to receive(:current_user).and_return(current_user)
|
@@ -262,26 +268,26 @@ describe DecentExposure::Controller do
|
|
262
268
|
expose :thing, parent: :current_user
|
263
269
|
end
|
264
270
|
|
265
|
-
after{ expect(controller.thing).to eq(42) }
|
271
|
+
after { expect(controller.thing).to eq(42) }
|
266
272
|
|
267
273
|
it "sets the scope to belong to parent defined by controller method" do
|
268
274
|
expect(scope).to receive(:new).with({}).and_return(42)
|
269
275
|
end
|
270
276
|
|
271
277
|
it "scopes the find to proper scope" do
|
272
|
-
controller.params
|
278
|
+
controller.params[:thing_id] = 10
|
273
279
|
expect(scope).to receive(:find).with(10).and_return(42)
|
274
280
|
end
|
275
281
|
end
|
276
282
|
end
|
277
283
|
|
278
284
|
context "override model" do
|
279
|
-
let(:different_thing){ double("DifferentThing") }
|
280
|
-
before{ expect(DifferentThing).to receive(:new).with({}).and_return(different_thing) }
|
281
|
-
after{ expect(controller.thing).to eq(different_thing) }
|
285
|
+
let(:different_thing) { double("DifferentThing") }
|
286
|
+
before { expect(DifferentThing).to receive(:new).with({}).and_return(different_thing) }
|
287
|
+
after { expect(controller.thing).to eq(different_thing) }
|
282
288
|
|
283
289
|
it "allows overriding model class with proc" do
|
284
|
-
expose :thing, model: ->{ DifferentThing }
|
290
|
+
expose :thing, model: -> { DifferentThing }
|
285
291
|
end
|
286
292
|
|
287
293
|
it "allows overriding model with class" do
|
@@ -300,7 +306,7 @@ describe DecentExposure::Controller do
|
|
300
306
|
context "override scope" do
|
301
307
|
it "allows overriding scope with proc" do
|
302
308
|
scope = double("Scope")
|
303
|
-
expose :thing, scope: ->{ scope }
|
309
|
+
expose :thing, scope: -> { scope }
|
304
310
|
expect(scope).to receive(:new).and_return(42)
|
305
311
|
expect(controller.thing).to eq(42)
|
306
312
|
end
|
@@ -321,7 +327,7 @@ describe DecentExposure::Controller do
|
|
321
327
|
end
|
322
328
|
|
323
329
|
it "allows overriding id with proc" do
|
324
|
-
expose :thing, id: ->{ get_thing_id_somehow }
|
330
|
+
expose :thing, id: -> { get_thing_id_somehow }
|
325
331
|
expect(controller).to receive(:get_thing_id_somehow).and_return(42)
|
326
332
|
end
|
327
333
|
|
@@ -338,7 +344,7 @@ describe DecentExposure::Controller do
|
|
338
344
|
|
339
345
|
context "override decorator" do
|
340
346
|
it "allows specify decorator" do
|
341
|
-
expose :thing, decorate: ->(thing){ decorate(thing) }
|
347
|
+
expose :thing, decorate: ->(thing) { decorate(thing) }
|
342
348
|
thing = double("Thing")
|
343
349
|
expect(Thing).to receive(:new).with({}).and_return(thing)
|
344
350
|
expect(controller).to receive(:decorate).with(thing)
|
@@ -357,7 +363,7 @@ describe DecentExposure::Controller do
|
|
357
363
|
end
|
358
364
|
|
359
365
|
it "should throw error when used with other options" do
|
360
|
-
action = ->{ expose :thing, from: :foo, parent: :bar }
|
366
|
+
action = -> { expose :thing, from: :foo, parent: :bar }
|
361
367
|
expect(&action).to raise_error(ArgumentError, "Using :from option with other options doesn't make sense")
|
362
368
|
end
|
363
369
|
|
@@ -367,7 +373,7 @@ describe DecentExposure::Controller do
|
|
367
373
|
foo = double("Foo", thing: thing)
|
368
374
|
expect(controller).to receive(:foo).and_return(foo)
|
369
375
|
expect(controller).to receive(:decorate).with(thing).and_return(decorated_thing)
|
370
|
-
expose :thing, from: :foo, decorate: ->(thing){ decorate(thing) }
|
376
|
+
expose :thing, from: :foo, decorate: ->(thing) { decorate(thing) }
|
371
377
|
expect(controller.thing).to eq(decorated_thing)
|
372
378
|
end
|
373
379
|
end
|