responders 2.0.0 → 3.1.1
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 +5 -5
- data/CHANGELOG.md +66 -1
- data/MIT-LICENSE +2 -1
- data/README.md +150 -49
- data/lib/action_controller/respond_with.rb +52 -20
- data/lib/action_controller/responder.rb +44 -28
- data/lib/generators/rails/responders_controller_generator.rb +4 -26
- data/lib/generators/rails/templates/api_controller.rb.tt +51 -0
- data/lib/generators/rails/templates/controller.rb.tt +59 -0
- data/lib/generators/responders/install_generator.rb +23 -9
- data/lib/responders/collection_responder.rb +3 -0
- data/lib/responders/controller_method.rb +7 -2
- data/lib/responders/flash_responder.rb +32 -27
- data/lib/responders/http_cache_responder.rb +5 -3
- data/lib/responders/version.rb +3 -1
- data/lib/responders.rb +16 -16
- metadata +32 -57
- data/lib/generators/rails/templates/controller.rb +0 -55
- data/lib/responders/location_responder.rb +0 -8
- data/test/action_controller/respond_with_test.rb +0 -717
- data/test/locales/en.yml +0 -28
- data/test/responders/collection_responder_test.rb +0 -82
- data/test/responders/controller_method_test.rb +0 -72
- data/test/responders/flash_responder_test.rb +0 -260
- data/test/responders/http_cache_responder_test.rb +0 -120
- data/test/test_helper.rb +0 -87
- data/test/views/addresses/create.js.erb +0 -1
- data/test/views/addresses/edit.html.erb +0 -1
- data/test/views/addresses/new.html.erb +0 -1
- data/test/views/locations/new.html.erb +0 -1
- data/test/views/respond_with/edit.html.erb +0 -1
- data/test/views/respond_with/new.html.erb +0 -1
- data/test/views/respond_with/respond_with_additional_params.html.erb +0 -0
- data/test/views/respond_with/using_invalid_resource_with_template.xml.erb +0 -1
- data/test/views/respond_with/using_options_with_template.xml.erb +0 -1
- data/test/views/respond_with/using_resource.js.erb +0 -1
- data/test/views/respond_with/using_resource_with_block.html.erb +0 -1
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "active_support/json"
|
4
|
+
|
5
|
+
module ActionController # :nodoc:
|
4
6
|
# Responsible for exposing a resource to different mime requests,
|
5
7
|
# usually depending on the HTTP verb. The responder is triggered when
|
6
8
|
# <code>respond_with</code> is called. The simplest case to study is a GET request:
|
@@ -47,7 +49,7 @@ module ActionController #:nodoc:
|
|
47
49
|
# format.html { redirect_to(@user) }
|
48
50
|
# format.xml { render xml: @user, status: :created, location: @user }
|
49
51
|
# else
|
50
|
-
# format.html { render action: "new" }
|
52
|
+
# format.html { render action: "new", status: :unprocessable_entity }
|
51
53
|
# format.xml { render xml: @user.errors, status: :unprocessable_entity }
|
52
54
|
# end
|
53
55
|
# end
|
@@ -111,22 +113,25 @@ module ActionController #:nodoc:
|
|
111
113
|
# if @task.save
|
112
114
|
# flash[:notice] = 'Task was successfully created.'
|
113
115
|
# else
|
114
|
-
# format.html { render "some_special_template" }
|
116
|
+
# format.html { render "some_special_template", status: :unprocessable_entity }
|
115
117
|
# end
|
116
118
|
# end
|
117
119
|
# end
|
118
120
|
#
|
119
121
|
# Using <code>respond_with</code> with a block follows the same syntax as <code>respond_to</code>.
|
120
122
|
class Responder
|
123
|
+
class_attribute :error_status, default: :ok, instance_writer: false, instance_predicate: false
|
124
|
+
class_attribute :redirect_status, default: :found, instance_writer: false, instance_predicate: false
|
125
|
+
|
121
126
|
attr_reader :controller, :request, :format, :resource, :resources, :options
|
122
127
|
|
123
128
|
DEFAULT_ACTIONS_FOR_VERBS = {
|
124
|
-
:
|
125
|
-
:
|
126
|
-
:
|
129
|
+
post: :new,
|
130
|
+
patch: :edit,
|
131
|
+
put: :edit
|
127
132
|
}
|
128
133
|
|
129
|
-
def initialize(controller, resources, options={})
|
134
|
+
def initialize(controller, resources, options = {})
|
130
135
|
@controller = controller
|
131
136
|
@request = @controller.request
|
132
137
|
@format = @controller.formats.first
|
@@ -142,8 +147,8 @@ module ActionController #:nodoc:
|
|
142
147
|
end
|
143
148
|
end
|
144
149
|
|
145
|
-
delegate :head, :render, :redirect_to, :
|
146
|
-
delegate :get?, :post?, :patch?, :put?, :delete?, :
|
150
|
+
delegate :head, :render, :redirect_to, to: :controller
|
151
|
+
delegate :get?, :post?, :patch?, :put?, :delete?, to: :request
|
147
152
|
|
148
153
|
# Undefine :to_json and :to_yaml since it's defined on Object
|
149
154
|
undef_method(:to_json) if method_defined?(:to_json)
|
@@ -182,13 +187,15 @@ module ActionController #:nodoc:
|
|
182
187
|
# responds to :to_format and display it.
|
183
188
|
#
|
184
189
|
def to_format
|
185
|
-
if get?
|
190
|
+
if !get? && has_errors? && !response_overridden?
|
191
|
+
display_errors
|
192
|
+
elsif has_view_rendering? || response_overridden?
|
186
193
|
default_render
|
187
194
|
else
|
188
|
-
|
195
|
+
api_behavior
|
189
196
|
end
|
190
|
-
rescue ActionView::MissingTemplate
|
191
|
-
api_behavior
|
197
|
+
rescue ActionView::MissingTemplate
|
198
|
+
api_behavior
|
192
199
|
end
|
193
200
|
|
194
201
|
protected
|
@@ -198,32 +205,25 @@ module ActionController #:nodoc:
|
|
198
205
|
if get?
|
199
206
|
raise error
|
200
207
|
elsif has_errors? && default_action
|
201
|
-
render
|
208
|
+
render error_rendering_options
|
202
209
|
else
|
203
|
-
redirect_to navigation_location
|
210
|
+
redirect_to navigation_location, status: redirect_status
|
204
211
|
end
|
205
212
|
end
|
206
213
|
|
207
214
|
# This is the common behavior for formats associated with APIs, such as :xml and :json.
|
208
|
-
def api_behavior
|
209
|
-
raise error unless resourceful?
|
215
|
+
def api_behavior
|
210
216
|
raise MissingRenderer.new(format) unless has_renderer?
|
211
217
|
|
212
218
|
if get?
|
213
219
|
display resource
|
214
220
|
elsif post?
|
215
|
-
display resource, :
|
221
|
+
display resource, status: :created, location: api_location
|
216
222
|
else
|
217
223
|
head :no_content
|
218
224
|
end
|
219
225
|
end
|
220
226
|
|
221
|
-
# Checks whether the resource responds to the current format or not.
|
222
|
-
#
|
223
|
-
def resourceful?
|
224
|
-
resource.respond_to?("to_#{format}")
|
225
|
-
end
|
226
|
-
|
227
227
|
# Returns the resource location by retrieving it from the options or
|
228
228
|
# returning the resources array.
|
229
229
|
#
|
@@ -239,8 +239,10 @@ module ActionController #:nodoc:
|
|
239
239
|
def default_render
|
240
240
|
if @default_response
|
241
241
|
@default_response.call(options)
|
242
|
+
elsif !get? && has_errors?
|
243
|
+
controller.render({ status: error_status }.merge!(options))
|
242
244
|
else
|
243
|
-
controller.
|
245
|
+
controller.render(options)
|
244
246
|
end
|
245
247
|
end
|
246
248
|
|
@@ -261,11 +263,13 @@ module ActionController #:nodoc:
|
|
261
263
|
#
|
262
264
|
# render xml: @user, status: :created
|
263
265
|
#
|
264
|
-
def display(resource, given_options={})
|
266
|
+
def display(resource, given_options = {})
|
265
267
|
controller.render given_options.merge!(options).merge!(format => resource)
|
266
268
|
end
|
267
269
|
|
268
270
|
def display_errors
|
271
|
+
# TODO: use `error_status` once we switch the default to be `unprocessable_entity`,
|
272
|
+
# otherwise we'd be changing this behavior here now.
|
269
273
|
controller.render format => resource_errors, :status => :unprocessable_entity
|
270
274
|
end
|
271
275
|
|
@@ -280,6 +284,10 @@ module ActionController #:nodoc:
|
|
280
284
|
Renderers::RENDERERS.include?(format)
|
281
285
|
end
|
282
286
|
|
287
|
+
def has_view_rendering?
|
288
|
+
controller.class.include? ActionView::Rendering
|
289
|
+
end
|
290
|
+
|
283
291
|
# By default, render the <code>:edit</code> action for HTML requests with errors, unless
|
284
292
|
# the verb was POST.
|
285
293
|
#
|
@@ -292,11 +300,19 @@ module ActionController #:nodoc:
|
|
292
300
|
end
|
293
301
|
|
294
302
|
def json_resource_errors
|
295
|
-
{:
|
303
|
+
{ errors: resource.errors }
|
296
304
|
end
|
297
305
|
|
298
306
|
def response_overridden?
|
299
307
|
@default_response.present?
|
300
308
|
end
|
309
|
+
|
310
|
+
def error_rendering_options
|
311
|
+
if options[:render]
|
312
|
+
options[:render]
|
313
|
+
else
|
314
|
+
{ action: default_action, status: error_status }
|
315
|
+
end
|
316
|
+
end
|
301
317
|
end
|
302
318
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
|
2
4
|
|
3
5
|
module Rails
|
4
6
|
module Generators
|
@@ -15,32 +17,8 @@ module Rails
|
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
def orm_instance_update(params)
|
19
|
-
if orm_instance.respond_to?(:update)
|
20
|
-
orm_instance.update params
|
21
|
-
else
|
22
|
-
orm_instance.update_attributes params
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def controller_before_filter
|
27
|
-
if ActionController::Base.respond_to?(:before_action)
|
28
|
-
"before_action"
|
29
|
-
else
|
30
|
-
"before_filter"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
20
|
def attributes_params
|
35
|
-
|
36
|
-
"#{file_name}_params"
|
37
|
-
else
|
38
|
-
"params[:#{file_name}]"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def strong_parameters_defined?
|
43
|
-
defined?(ActionController::StrongParameters)
|
21
|
+
"#{singular_table_name}_params"
|
44
22
|
end
|
45
23
|
end
|
46
24
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_file_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
|
8
|
+
|
9
|
+
respond_to :json
|
10
|
+
|
11
|
+
<% unless options[:singleton] -%>
|
12
|
+
def index
|
13
|
+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
14
|
+
respond_with(@<%= plural_table_name %>)
|
15
|
+
end
|
16
|
+
<% end -%>
|
17
|
+
|
18
|
+
def show
|
19
|
+
respond_with(@<%= singular_table_name %>)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create
|
23
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, attributes_params) %>
|
24
|
+
<%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
|
25
|
+
respond_with(@<%= singular_table_name %>)
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
<%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance.update(attributes_params) %>
|
30
|
+
respond_with(@<%= singular_table_name %>)
|
31
|
+
end
|
32
|
+
|
33
|
+
def destroy
|
34
|
+
@<%= orm_instance.destroy %>
|
35
|
+
respond_with(@<%= singular_table_name %>)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def set_<%= singular_table_name %>
|
40
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
41
|
+
end
|
42
|
+
|
43
|
+
def <%= "#{singular_table_name}_params" %>
|
44
|
+
<%- if attributes_names.empty? -%>
|
45
|
+
params[:<%= singular_table_name %>]
|
46
|
+
<%- else -%>
|
47
|
+
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
48
|
+
<%- end -%>
|
49
|
+
end
|
50
|
+
end
|
51
|
+
<% end -%>
|
@@ -0,0 +1,59 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_file_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
8
|
+
|
9
|
+
respond_to :html
|
10
|
+
|
11
|
+
<% unless options[:singleton] -%>
|
12
|
+
def index
|
13
|
+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
14
|
+
respond_with(@<%= plural_table_name %>)
|
15
|
+
end
|
16
|
+
<% end -%>
|
17
|
+
|
18
|
+
def show
|
19
|
+
respond_with(@<%= singular_table_name %>)
|
20
|
+
end
|
21
|
+
|
22
|
+
def new
|
23
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
|
24
|
+
respond_with(@<%= singular_table_name %>)
|
25
|
+
end
|
26
|
+
|
27
|
+
def edit
|
28
|
+
end
|
29
|
+
|
30
|
+
def create
|
31
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, attributes_params) %>
|
32
|
+
<%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
|
33
|
+
respond_with(@<%= singular_table_name %>)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
<%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance.update(attributes_params) %>
|
38
|
+
respond_with(@<%= singular_table_name %>)
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy
|
42
|
+
@<%= orm_instance.destroy %>
|
43
|
+
respond_with(@<%= singular_table_name %>)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def set_<%= singular_table_name %>
|
48
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
49
|
+
end
|
50
|
+
|
51
|
+
def <%= "#{singular_table_name}_params" %>
|
52
|
+
<%- if attributes_names.empty? -%>
|
53
|
+
params[:<%= singular_table_name %>]
|
54
|
+
<%- else -%>
|
55
|
+
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
56
|
+
<%- end -%>
|
57
|
+
end
|
58
|
+
end
|
59
|
+
<% end -%>
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
module Generators
|
3
5
|
class InstallGenerator < Rails::Generators::Base
|
@@ -6,15 +8,27 @@ module Responders
|
|
6
8
|
desc "Creates an initializer with default responder configuration and copy locale file"
|
7
9
|
|
8
10
|
def create_responder_file
|
9
|
-
create_file "lib/application_responder.rb",
|
10
|
-
class ApplicationResponder < ActionController::Responder
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
create_file "lib/application_responder.rb", <<~RUBY
|
12
|
+
class ApplicationResponder < ActionController::Responder
|
13
|
+
include Responders::FlashResponder
|
14
|
+
include Responders::HttpCacheResponder
|
15
|
+
|
16
|
+
# Redirects resources to the collection path (index action) instead
|
17
|
+
# of the resource path (show action) for POST/PUT/DELETE requests.
|
18
|
+
# include Responders::CollectionResponder
|
19
|
+
|
20
|
+
# Configure default status codes for responding to errors and redirects.
|
21
|
+
self.error_status = :unprocessable_entity
|
22
|
+
self.redirect_status = :see_other
|
23
|
+
end
|
24
|
+
RUBY
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_application
|
28
|
+
inject_into_class "config/application.rb", "Application", <<-RUBY
|
29
|
+
# Use the responders controller from the responders gem
|
30
|
+
config.app_generators.scaffold_controller :responders_controller
|
31
|
+
|
18
32
|
RUBY
|
19
33
|
end
|
20
34
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
# This responder modifies your current responder to redirect
|
3
5
|
# to the collection page on POST/PUT/DELETE.
|
@@ -18,6 +20,7 @@ module Responders
|
|
18
20
|
#
|
19
21
|
def navigation_location
|
20
22
|
return options[:location] if options[:location]
|
23
|
+
|
21
24
|
klass = resources.last.class
|
22
25
|
|
23
26
|
if klass.respond_to?(:model_name)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
module ControllerMethod
|
3
5
|
# Adds the given responders to the current controller's responder, allowing you to cherry-pick
|
@@ -18,7 +20,8 @@ module Responders
|
|
18
20
|
#
|
19
21
|
def responders(*responders)
|
20
22
|
self.responder = responders.inject(Class.new(responder)) do |klass, responder|
|
21
|
-
responder =
|
23
|
+
responder = \
|
24
|
+
case responder
|
22
25
|
when Module
|
23
26
|
responder
|
24
27
|
when String, Symbol
|
@@ -34,4 +37,6 @@ module Responders
|
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
|
-
|
40
|
+
ActiveSupport.on_load(:action_controller_base) do
|
41
|
+
ActionController::Base.extend Responders::ControllerMethod
|
42
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
# Responder to automatically set flash messages based on I18n API. It checks for
|
3
5
|
# message based on the current action, but also allows defaults to be set, using
|
@@ -33,9 +35,9 @@ module Responders
|
|
33
35
|
# notice: "Hooray! You just tuned your %{car_brand}!"
|
34
36
|
#
|
35
37
|
# Since :car_name is not available for interpolation by default, you have
|
36
|
-
# to overwrite
|
38
|
+
# to overwrite `flash_interpolation_options` in your controller.
|
37
39
|
#
|
38
|
-
# def
|
40
|
+
# def flash_interpolation_options
|
39
41
|
# { :car_brand => @car.brand }
|
40
42
|
# end
|
41
43
|
#
|
@@ -70,7 +72,7 @@ module Responders
|
|
70
72
|
#
|
71
73
|
# respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
|
72
74
|
#
|
73
|
-
# * :flash_now - Sets the flash message using flash.now. Accepts true, :on_failure or :
|
75
|
+
# * :flash_now - Sets the flash message using flash.now. Accepts true, :on_failure or :on_success.
|
74
76
|
#
|
75
77
|
# == Configure status keys
|
76
78
|
#
|
@@ -84,17 +86,13 @@ module Responders
|
|
84
86
|
#
|
85
87
|
module FlashResponder
|
86
88
|
class << self
|
87
|
-
attr_accessor :flash_keys, :namespace_lookup
|
89
|
+
attr_accessor :flash_keys, :namespace_lookup
|
88
90
|
end
|
89
91
|
|
90
92
|
self.flash_keys = [ :notice, :alert ]
|
91
93
|
self.namespace_lookup = false
|
92
|
-
self.helper = Object.new.extend(
|
93
|
-
ActionView::Helpers::TranslationHelper,
|
94
|
-
ActionView::Helpers::TagHelper
|
95
|
-
)
|
96
94
|
|
97
|
-
def initialize(controller, resources, options={})
|
95
|
+
def initialize(controller, resources, options = {})
|
98
96
|
super
|
99
97
|
@flash = options.delete(:flash)
|
100
98
|
@notice = options.delete(:notice)
|
@@ -126,7 +124,7 @@ module Responders
|
|
126
124
|
return if controller.flash[status].present?
|
127
125
|
|
128
126
|
options = mount_i18n_options(status)
|
129
|
-
message =
|
127
|
+
message = controller.helpers.t options[:default].shift, **options
|
130
128
|
set_flash(status, message)
|
131
129
|
end
|
132
130
|
|
@@ -142,39 +140,46 @@ module Responders
|
|
142
140
|
(default_action && (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success))
|
143
141
|
end
|
144
142
|
|
145
|
-
def set_flash_message?
|
143
|
+
def set_flash_message? # :nodoc:
|
146
144
|
!get? && @flash != false
|
147
145
|
end
|
148
146
|
|
149
|
-
def mount_i18n_options(status)
|
150
|
-
resource_name = if resource.class.respond_to?(:model_name)
|
151
|
-
resource.class.model_name.human
|
152
|
-
else
|
153
|
-
resource.class.name.underscore.humanize
|
154
|
-
end
|
155
|
-
|
147
|
+
def mount_i18n_options(status) # :nodoc:
|
156
148
|
options = {
|
157
|
-
:
|
158
|
-
:
|
159
|
-
:
|
149
|
+
default: flash_defaults_by_namespace(status),
|
150
|
+
resource_name: resource_name,
|
151
|
+
downcase_resource_name: resource_name.downcase
|
160
152
|
}
|
161
153
|
|
162
|
-
|
163
|
-
|
154
|
+
controller_options = controller_interpolation_options
|
155
|
+
if controller_options
|
156
|
+
options.merge!(controller_options)
|
164
157
|
end
|
165
158
|
|
166
159
|
options
|
167
160
|
end
|
168
161
|
|
169
|
-
def
|
162
|
+
def controller_interpolation_options
|
163
|
+
controller.send(:flash_interpolation_options) if controller.respond_to?(:flash_interpolation_options, true)
|
164
|
+
end
|
165
|
+
|
166
|
+
def resource_name
|
167
|
+
if resource.class.respond_to?(:model_name)
|
168
|
+
resource.class.model_name.human
|
169
|
+
else
|
170
|
+
resource.class.name.underscore.humanize
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def flash_defaults_by_namespace(status) # :nodoc:
|
170
175
|
defaults = []
|
171
|
-
slices = controller.controller_path.split(
|
176
|
+
slices = controller.controller_path.split("/")
|
172
177
|
lookup = Responders::FlashResponder.namespace_lookup
|
173
178
|
|
174
179
|
begin
|
175
|
-
controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join(
|
180
|
+
controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join(".")}.#{controller.action_name}.#{status}"
|
176
181
|
|
177
|
-
actions_scope = lookup ? slices.fill(
|
182
|
+
actions_scope = lookup ? slices.fill("actions", -1).join(".") : :actions
|
178
183
|
actions_scope = :"flash.#{actions_scope}.#{controller.action_name}.#{status}"
|
179
184
|
|
180
185
|
defaults << :"#{controller_scope}_html"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
# Set HTTP Last-Modified headers based on the given resource. It's used only
|
3
5
|
# on API behavior (to_format) and is useful for a client to check in the server
|
@@ -9,7 +11,7 @@ module Responders
|
|
9
11
|
# the digest of the body.
|
10
12
|
#
|
11
13
|
module HttpCacheResponder
|
12
|
-
def initialize(controller, resources, options={})
|
14
|
+
def initialize(controller, resources, options = {})
|
13
15
|
super
|
14
16
|
@http_cache = options.delete(:http_cache)
|
15
17
|
end
|
@@ -34,11 +36,11 @@ module Responders
|
|
34
36
|
|
35
37
|
def do_http_cache?
|
36
38
|
get? && @http_cache != false && ActionController::Base.perform_caching &&
|
37
|
-
persisted? &&
|
39
|
+
persisted? && resource.respond_to?(:updated_at)
|
38
40
|
end
|
39
41
|
|
40
42
|
def persisted?
|
41
43
|
resource.respond_to?(:persisted?) ? resource.persisted? : true
|
42
44
|
end
|
43
45
|
end
|
44
|
-
end
|
46
|
+
end
|
data/lib/responders/version.rb
CHANGED
data/lib/responders.rb
CHANGED
@@ -1,36 +1,36 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_controller"
|
4
|
+
require "rails/railtie"
|
2
5
|
|
3
6
|
module ActionController
|
4
|
-
autoload :Responder,
|
5
|
-
autoload :RespondWith,
|
7
|
+
autoload :Responder, "action_controller/responder"
|
8
|
+
autoload :RespondWith, "action_controller/respond_with"
|
6
9
|
end
|
7
10
|
|
8
11
|
module Responders
|
9
|
-
autoload :FlashResponder,
|
10
|
-
autoload :HttpCacheResponder,
|
11
|
-
autoload :CollectionResponder,
|
12
|
-
autoload :LocationResponder, 'responders/location_responder'
|
12
|
+
autoload :FlashResponder, "responders/flash_responder"
|
13
|
+
autoload :HttpCacheResponder, "responders/http_cache_responder"
|
14
|
+
autoload :CollectionResponder, "responders/collection_responder"
|
13
15
|
|
14
|
-
require
|
16
|
+
require "responders/controller_method"
|
15
17
|
|
16
18
|
class Railtie < ::Rails::Railtie
|
17
19
|
config.responders = ActiveSupport::OrderedOptions.new
|
18
20
|
config.responders.flash_keys = [:notice, :alert]
|
19
21
|
config.responders.namespace_lookup = false
|
20
|
-
|
21
|
-
|
22
|
-
config.app_generators.scaffold_controller = :responders_controller
|
23
|
-
else
|
24
|
-
config.generators.scaffold_controller = :responders_controller
|
25
|
-
end
|
22
|
+
config.responders.error_status = :ok
|
23
|
+
config.responders.redirect_status = :found
|
26
24
|
|
27
25
|
# Add load paths straight to I18n, so engines and application can overwrite it.
|
28
|
-
require
|
29
|
-
I18n.load_path << File.expand_path(
|
26
|
+
require "active_support/i18n"
|
27
|
+
I18n.load_path << File.expand_path("../responders/locales/en.yml", __FILE__)
|
30
28
|
|
31
29
|
initializer "responders.flash_responder" do |app|
|
32
30
|
Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
|
33
31
|
Responders::FlashResponder.namespace_lookup = app.config.responders.namespace_lookup
|
32
|
+
ActionController::Responder.error_status = app.config.responders.error_status
|
33
|
+
ActionController::Responder.redirect_status = app.config.responders.redirect_status
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|