responders 2.4.1 → 3.1.0
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 +4 -4
- data/CHANGELOG.md +22 -2
- data/MIT-LICENSE +2 -1
- data/README.md +45 -11
- data/lib/action_controller/respond_with.rb +17 -12
- data/lib/action_controller/responder.rb +26 -17
- data/lib/generators/rails/responders_controller_generator.rb +3 -1
- data/lib/generators/responders/install_generator.rb +15 -9
- data/lib/responders/collection_responder.rb +3 -0
- data/lib/responders/controller_method.rb +5 -2
- data/lib/responders/flash_responder.rb +16 -23
- data/lib/responders/http_cache_responder.rb +3 -1
- data/lib/responders/version.rb +3 -1
- data/lib/responders.rb +16 -11
- metadata +20 -30
- data/lib/responders/location_responder.rb +0 -8
- /data/lib/generators/rails/templates/{api_controller.rb → api_controller.rb.tt} +0 -0
- /data/lib/generators/rails/templates/{controller.rb → controller.rb.tt} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbd11b54b7dff0d570ff0231c4947ef6c086e75eec2939a6b96d4ebb65bc40f1
|
4
|
+
data.tar.gz: 5f6df15df62211cdc47025a70f4037a493dc1d679aa2a6a5a834d90135b07de1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62f893c63385f0d2de498143a4bcf0dafde98ef920778e5115af3d7b14f2d4c7dcdae08712193db9543e205e9830e307758b702970fe2480e7ccc322e8a81c38
|
7
|
+
data.tar.gz: 888a7bae0e92cdbdd8037f5cbfde45d09ec23d528d783aa11ac436bf21992aae7c63252285161a42513fb9c7dbb1ab4abf45b6371cadebfe9efd05bb2175d8e7
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,31 @@
|
|
1
|
+
## Unreleased
|
2
|
+
|
3
|
+
|
4
|
+
## 3.1.0
|
5
|
+
|
6
|
+
* Add config `responders.redirect_status` to allow overriding the redirect code/status used in redirects. The default is `302 Found`, which matches Rails, but it allows to change responders to redirect with `303 See Other` for example, to make it more compatible with how Hotwire/Turbo expects redirects to work.
|
7
|
+
* Add config `responders.error_status` to allow overriding the status code used to respond to `HTML` or `JS` requests that have errors on the resource. The default is `200 OK`, but it allows to change the response to be `422 Unprocessable Entity` in such cases for example, which makes it more consistent with other statuses more commonly used in APIs (like JSON/XML), and works by default with Turbo/Hotwire which expects a 422 on form error HTML responses. Note that changing this may break your application if you're relying on the previous 2xx status to handle error cases.
|
8
|
+
* Add support for Ruby 3.0, 3.1, and 3.2, drop support for Ruby < 2.5.
|
9
|
+
* Add support for Rails 6.1 and 7.0, drop support for Rails < 5.2.
|
10
|
+
* Move CI to GitHub Actions.
|
11
|
+
|
12
|
+
## 3.0.1
|
13
|
+
|
14
|
+
* Add support to Ruby 2.7
|
15
|
+
|
16
|
+
## 3.0.0
|
17
|
+
|
18
|
+
* Remove support for Rails 4.2
|
19
|
+
* Remove support for Ruby < 2.4
|
20
|
+
|
1
21
|
## 2.4.1
|
2
22
|
|
3
23
|
* Add support for Rails 6 beta
|
4
24
|
|
5
25
|
## 2.4.0
|
6
26
|
|
7
|
-
* `respond_with` now accepts a new
|
8
|
-
call after an unsuccessful post request.
|
27
|
+
* `respond_with` now accepts a new kwarg called `:render` which goes straight to the `render`
|
28
|
+
call after an unsuccessful post request. Useful if for example you need to render a template
|
9
29
|
which is outside of controller's path eg:
|
10
30
|
|
11
31
|
`respond_with resource, render: { template: 'path/to/template' }`
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
Copyright
|
1
|
+
Copyright (c) 2020-2022 Rafael França, Carlos Antônio da Silva
|
2
|
+
Copyright 2009-2019 Plataformatec. http://plataformatec.com.br
|
2
3
|
|
3
4
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
5
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
# Responders
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/responders)
|
4
|
-
[](http://travis-ci.org/plataformatec/responders)
|
5
|
-
[](https://codeclimate.com/github/plataformatec/responders)
|
6
4
|
|
7
|
-
A set of responders modules to dry up your Rails
|
5
|
+
A set of responders modules to dry up your Rails app.
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
@@ -179,19 +177,21 @@ class InvitationsController < ApplicationController
|
|
179
177
|
end
|
180
178
|
```
|
181
179
|
|
182
|
-
Now you would see the message "
|
180
|
+
Now you would see the message `"name@example.com was successfully created"` instead of the default `"Invitation was successfully created."`
|
183
181
|
|
184
182
|
## Generator
|
185
183
|
|
186
184
|
This gem also includes a responders controller generator, so your scaffold can be customized
|
187
185
|
to use `respond_with` instead of default `respond_to` blocks. From 2.1, you need to explicitly opt-in to use this generator by adding the following to your `config/application.rb`:
|
188
186
|
|
189
|
-
|
187
|
+
```ruby
|
188
|
+
config.app_generators.scaffold_controller :responders_controller
|
189
|
+
```
|
190
190
|
|
191
191
|
## Failure handling
|
192
192
|
|
193
193
|
Responders don't use `valid?` to check for errors in models to figure out if
|
194
|
-
the request was
|
194
|
+
the request was successful or not, and relies on your controllers to call
|
195
195
|
`save` or `create` to trigger the validations.
|
196
196
|
|
197
197
|
```ruby
|
@@ -211,7 +211,8 @@ assertions on this behavior for your controllers.
|
|
211
211
|
def create
|
212
212
|
@widget = Widget.new(widget_params)
|
213
213
|
@widget.errors.add(:base, :invalid)
|
214
|
-
# `respond_with` will render the `new` template again
|
214
|
+
# `respond_with` will render the `new` template again,
|
215
|
+
# and set the status based on the configured `error_status`.
|
215
216
|
respond_with @widget
|
216
217
|
end
|
217
218
|
```
|
@@ -219,7 +220,7 @@ end
|
|
219
220
|
## Verifying request formats
|
220
221
|
|
221
222
|
`respond_with` will raise an `ActionController::UnknownFormat` if the request
|
222
|
-
|
223
|
+
MIME type was not configured through the class level `respond_to`, but the
|
223
224
|
action will still be executed and any side effects (like creating a new record)
|
224
225
|
will still occur. To raise the `UnknownFormat` exception before your action
|
225
226
|
is invoked you can set the `verify_requested_format!` method as a `before_action`
|
@@ -236,9 +237,37 @@ class WidgetsController < ApplicationController
|
|
236
237
|
respond_with widget
|
237
238
|
end
|
238
239
|
end
|
240
|
+
```
|
241
|
+
|
242
|
+
## Configuring error and redirect statuses
|
243
|
+
|
244
|
+
By default, `respond_with` will respond to errors on `HTML` & `JS` requests using the HTTP status code `200 OK`,
|
245
|
+
and perform redirects using the HTTP status code `302 Found`, both for backwards compatibility reasons.
|
246
|
+
|
247
|
+
You can configure this behavior by setting `config.responders.error_status` and `config.responders.redirect_status` to the desired status codes.
|
239
248
|
|
249
|
+
```ruby
|
250
|
+
config.responders.error_status = :unprocessable_entity
|
251
|
+
config.responders.redirect_status = :see_other
|
240
252
|
```
|
241
253
|
|
254
|
+
These can also be set in your custom `ApplicationResponder` if you have generated one: (see install instructions)
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
class ApplicationResponder < ActionController::Responder
|
258
|
+
self.error_status = :unprocessable_entity
|
259
|
+
self.redirect_status = :see_other
|
260
|
+
end
|
261
|
+
```
|
262
|
+
|
263
|
+
_Note_: the application responder generated for new apps already configures a different set of defaults: `422 Unprocessable Entity` for errors, and `303 See Other` for redirects. _Responders may change the defaults to match these in a future major release._
|
264
|
+
|
265
|
+
### Hotwire/Turbo and fetch APIs
|
266
|
+
|
267
|
+
Hotwire/Turbo expects successful redirects after form submissions to respond with HTTP status `303 See Other`, and error responses to be 4xx or 5xx statuses, for example `422 Unprocessable Entity` for displaying form validation errors and `500 Internal Server Error` for other server errors. [Turbo documentation: Redirecting After a Form Submission](https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission).
|
268
|
+
|
269
|
+
The example configuration showed above matches the statuses that better integrate with Hotwire/Turbo.
|
270
|
+
|
242
271
|
## Examples
|
243
272
|
|
244
273
|
Want more examples ? Check out these blog posts:
|
@@ -247,10 +276,15 @@ Want more examples ? Check out these blog posts:
|
|
247
276
|
* [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder/)
|
248
277
|
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3)
|
249
278
|
|
279
|
+
## Supported Ruby / Rails versions
|
280
|
+
|
281
|
+
We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.
|
282
|
+
|
283
|
+
For more information about specific versions please check [Ruby](https://www.ruby-lang.org/en/downloads/branches/)
|
284
|
+
and [Rails](https://guides.rubyonrails.org/maintenance_policy.html) maintenance policies, and our test matrix.
|
285
|
+
|
250
286
|
## Bugs and Feedback
|
251
287
|
|
252
288
|
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
253
289
|
|
254
|
-
|
255
|
-
|
256
|
-
MIT License. Copyright 2009-2016 Plataformatec. http://plataformatec.com.br
|
290
|
+
MIT License. Copyright 2020 Rafael França, Carlos Antônio da Silva. Copyright 2009-2019 Plataformatec.
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
require 'action_controller/metal/mime_responds'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
require "active_support/core_ext/array/extract_options"
|
4
|
+
require "action_controller/metal/mime_responds"
|
5
|
+
|
6
|
+
module ActionController # :nodoc:
|
5
7
|
module RespondWith
|
6
8
|
extend ActiveSupport::Concern
|
7
9
|
|
@@ -37,8 +39,8 @@ module ActionController #:nodoc:
|
|
37
39
|
def respond_to(*mimes)
|
38
40
|
options = mimes.extract_options!
|
39
41
|
|
40
|
-
only_actions = Array(options.delete(:only)).map(&:
|
41
|
-
except_actions = Array(options.delete(:except)).map(&:
|
42
|
+
only_actions = Array(options.delete(:only)).map(&:to_sym)
|
43
|
+
except_actions = Array(options.delete(:except)).map(&:to_sym)
|
42
44
|
|
43
45
|
hash = mimes_for_respond_to.dup
|
44
46
|
mimes.each do |mime|
|
@@ -93,7 +95,10 @@ module ActionController #:nodoc:
|
|
93
95
|
# i.e. its +show+ action.
|
94
96
|
# 2. If there are validation errors, the response
|
95
97
|
# renders a default action, which is <tt>:new</tt> for a
|
96
|
-
# +post+ request or <tt>:edit</tt> for +patch+ or +put
|
98
|
+
# +post+ request or <tt>:edit</tt> for +patch+ or +put+,
|
99
|
+
# and the status is set based on the configured `error_status`.
|
100
|
+
# (defaults to `422 Unprocessable Entity` on new apps,
|
101
|
+
# `200 OK` for compatibility reasons on old apps.)
|
97
102
|
# Thus an example like this -
|
98
103
|
#
|
99
104
|
# respond_to :html, :xml
|
@@ -114,8 +119,8 @@ module ActionController #:nodoc:
|
|
114
119
|
# format.html { redirect_to(@user) }
|
115
120
|
# format.xml { render xml: @user }
|
116
121
|
# else
|
117
|
-
# format.html { render action: "new" }
|
118
|
-
# format.xml { render xml: @user }
|
122
|
+
# format.html { render action: "new", status: :unprocessable_entity }
|
123
|
+
# format.xml { render xml: @user, status: :unprocessable_entity }
|
119
124
|
# end
|
120
125
|
# end
|
121
126
|
# end
|
@@ -188,11 +193,11 @@ module ActionController #:nodoc:
|
|
188
193
|
# 2. <tt>:action</tt> - overwrites the default render action used after an
|
189
194
|
# unsuccessful html +post+ request.
|
190
195
|
# 3. <tt>:render</tt> - allows to pass any options directly to the <tt>:render<tt/>
|
191
|
-
# call after unsuccessful html +post+ request.
|
196
|
+
# call after unsuccessful html +post+ request. Useful if for example you
|
192
197
|
# need to render a template which is outside of controller's path or you
|
193
198
|
# want to override the default http <tt>:status</tt> code, e.g.
|
194
199
|
#
|
195
|
-
# respond_with(resource, render: { template: 'path/to/template', status:
|
200
|
+
# respond_with(resource, render: { template: 'path/to/template', status: 418 })
|
196
201
|
def respond_with(*resources, &block)
|
197
202
|
if self.class.mimes_for_respond_to.empty?
|
198
203
|
raise "In order to use respond_with, first you need to declare the " \
|
@@ -237,8 +242,8 @@ module ActionController #:nodoc:
|
|
237
242
|
|
238
243
|
# Collect mimes declared in the class method respond_to valid for the
|
239
244
|
# current action.
|
240
|
-
def collect_mimes_from_class_level
|
241
|
-
action = action_name.
|
245
|
+
def collect_mimes_from_class_level # :nodoc:
|
246
|
+
action = action_name.to_sym
|
242
247
|
|
243
248
|
self.class.mimes_for_respond_to.keys.select do |mime|
|
244
249
|
config = self.class.mimes_for_respond_to[mime]
|
@@ -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)
|
@@ -200,9 +205,9 @@ module ActionController #:nodoc:
|
|
200
205
|
if get?
|
201
206
|
raise error
|
202
207
|
elsif has_errors? && default_action
|
203
|
-
render
|
208
|
+
render error_rendering_options
|
204
209
|
else
|
205
|
-
redirect_to navigation_location
|
210
|
+
redirect_to navigation_location, status: redirect_status
|
206
211
|
end
|
207
212
|
end
|
208
213
|
|
@@ -213,7 +218,7 @@ module ActionController #:nodoc:
|
|
213
218
|
if get?
|
214
219
|
display resource
|
215
220
|
elsif post?
|
216
|
-
display resource, :
|
221
|
+
display resource, status: :created, location: api_location
|
217
222
|
else
|
218
223
|
head :no_content
|
219
224
|
end
|
@@ -234,6 +239,8 @@ module ActionController #:nodoc:
|
|
234
239
|
def default_render
|
235
240
|
if @default_response
|
236
241
|
@default_response.call(options)
|
242
|
+
elsif !get? && has_errors?
|
243
|
+
controller.render({ status: error_status }.merge!(options))
|
237
244
|
else
|
238
245
|
controller.render(options)
|
239
246
|
end
|
@@ -256,11 +263,13 @@ module ActionController #:nodoc:
|
|
256
263
|
#
|
257
264
|
# render xml: @user, status: :created
|
258
265
|
#
|
259
|
-
def display(resource, given_options={})
|
266
|
+
def display(resource, given_options = {})
|
260
267
|
controller.render given_options.merge!(options).merge!(format => resource)
|
261
268
|
end
|
262
269
|
|
263
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.
|
264
273
|
controller.render format => resource_errors, :status => :unprocessable_entity
|
265
274
|
end
|
266
275
|
|
@@ -291,18 +300,18 @@ module ActionController #:nodoc:
|
|
291
300
|
end
|
292
301
|
|
293
302
|
def json_resource_errors
|
294
|
-
{:
|
303
|
+
{ errors: resource.errors }
|
295
304
|
end
|
296
305
|
|
297
306
|
def response_overridden?
|
298
307
|
@default_response.present?
|
299
308
|
end
|
300
309
|
|
301
|
-
def
|
310
|
+
def error_rendering_options
|
302
311
|
if options[:render]
|
303
312
|
options[:render]
|
304
313
|
else
|
305
|
-
{ action: default_action }
|
314
|
+
{ action: default_action, status: error_status }
|
306
315
|
end
|
307
316
|
end
|
308
317
|
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,19 @@ 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
|
18
24
|
RUBY
|
19
25
|
end
|
20
26
|
|
@@ -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,6 +37,6 @@ module Responders
|
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
|
-
ActiveSupport.on_load(:
|
40
|
+
ActiveSupport.on_load(:action_controller_base) do
|
38
41
|
ActionController::Base.extend Responders::ControllerMethod
|
39
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
|
@@ -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,15 +140,15 @@ 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)
|
147
|
+
def mount_i18n_options(status) # :nodoc:
|
150
148
|
options = {
|
151
|
-
:
|
152
|
-
:
|
153
|
-
:
|
149
|
+
default: flash_defaults_by_namespace(status),
|
150
|
+
resource_name: resource_name,
|
151
|
+
downcase_resource_name: resource_name.downcase
|
154
152
|
}
|
155
153
|
|
156
154
|
controller_options = controller_interpolation_options
|
@@ -162,12 +160,7 @@ module Responders
|
|
162
160
|
end
|
163
161
|
|
164
162
|
def controller_interpolation_options
|
165
|
-
if controller.respond_to?(:flash_interpolation_options, true)
|
166
|
-
controller.send(:flash_interpolation_options)
|
167
|
-
elsif controller.respond_to?(:interpolation_options, true)
|
168
|
-
ActiveSupport::Deprecation.warn("[responders] `#{controller.class}#interpolation_options` is deprecated, please rename it to `flash_interpolation_options`.")
|
169
|
-
controller.send(:interpolation_options)
|
170
|
-
end
|
163
|
+
controller.send(:flash_interpolation_options) if controller.respond_to?(:flash_interpolation_options, true)
|
171
164
|
end
|
172
165
|
|
173
166
|
def resource_name
|
@@ -178,15 +171,15 @@ module Responders
|
|
178
171
|
end
|
179
172
|
end
|
180
173
|
|
181
|
-
def flash_defaults_by_namespace(status)
|
174
|
+
def flash_defaults_by_namespace(status) # :nodoc:
|
182
175
|
defaults = []
|
183
|
-
slices = controller.controller_path.split(
|
176
|
+
slices = controller.controller_path.split("/")
|
184
177
|
lookup = Responders::FlashResponder.namespace_lookup
|
185
178
|
|
186
179
|
begin
|
187
|
-
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}"
|
188
181
|
|
189
|
-
actions_scope = lookup ? slices.fill(
|
182
|
+
actions_scope = lookup ? slices.fill("actions", -1).join(".") : :actions
|
190
183
|
actions_scope = :"flash.#{actions_scope}.#{controller.action_name}.#{status}"
|
191
184
|
|
192
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
|
data/lib/responders/version.rb
CHANGED
data/lib/responders.rb
CHANGED
@@ -1,31 +1,36 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_controller"
|
4
|
+
require "rails/railtie"
|
3
5
|
|
4
6
|
module ActionController
|
5
|
-
autoload :Responder,
|
6
|
-
autoload :RespondWith,
|
7
|
+
autoload :Responder, "action_controller/responder"
|
8
|
+
autoload :RespondWith, "action_controller/respond_with"
|
7
9
|
end
|
8
10
|
|
9
11
|
module Responders
|
10
|
-
autoload :FlashResponder,
|
11
|
-
autoload :HttpCacheResponder,
|
12
|
-
autoload :CollectionResponder,
|
13
|
-
autoload :LocationResponder, 'responders/location_responder'
|
12
|
+
autoload :FlashResponder, "responders/flash_responder"
|
13
|
+
autoload :HttpCacheResponder, "responders/http_cache_responder"
|
14
|
+
autoload :CollectionResponder, "responders/collection_responder"
|
14
15
|
|
15
|
-
require
|
16
|
+
require "responders/controller_method"
|
16
17
|
|
17
18
|
class Railtie < ::Rails::Railtie
|
18
19
|
config.responders = ActiveSupport::OrderedOptions.new
|
19
20
|
config.responders.flash_keys = [:notice, :alert]
|
20
21
|
config.responders.namespace_lookup = false
|
22
|
+
config.responders.error_status = :ok
|
23
|
+
config.responders.redirect_status = :found
|
21
24
|
|
22
25
|
# Add load paths straight to I18n, so engines and application can overwrite it.
|
23
|
-
require
|
24
|
-
I18n.load_path << File.expand_path(
|
26
|
+
require "active_support/i18n"
|
27
|
+
I18n.load_path << File.expand_path("../responders/locales/en.yml", __FILE__)
|
25
28
|
|
26
29
|
initializer "responders.flash_responder" do |app|
|
27
30
|
Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
|
28
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
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: responders
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Valim
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -16,42 +16,30 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '6.0'
|
19
|
+
version: '5.2'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '6.0'
|
26
|
+
version: '5.2'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: actionpack
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
40
|
-
- - "<"
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: '6.0'
|
33
|
+
version: '5.2'
|
43
34
|
type: :runtime
|
44
35
|
prerelease: false
|
45
36
|
version_requirements: !ruby/object:Gem::Requirement
|
46
37
|
requirements:
|
47
38
|
- - ">="
|
48
39
|
- !ruby/object:Gem::Version
|
49
|
-
version:
|
50
|
-
- - "<"
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: '6.0'
|
40
|
+
version: '5.2'
|
53
41
|
description: A set of Rails responders to dry up your application
|
54
|
-
email:
|
42
|
+
email: heartcombo@googlegroups.com
|
55
43
|
executables: []
|
56
44
|
extensions: []
|
57
45
|
extra_rdoc_files: []
|
@@ -63,8 +51,8 @@ files:
|
|
63
51
|
- lib/action_controller/responder.rb
|
64
52
|
- lib/generators/rails/USAGE
|
65
53
|
- lib/generators/rails/responders_controller_generator.rb
|
66
|
-
- lib/generators/rails/templates/api_controller.rb
|
67
|
-
- lib/generators/rails/templates/controller.rb
|
54
|
+
- lib/generators/rails/templates/api_controller.rb.tt
|
55
|
+
- lib/generators/rails/templates/controller.rb.tt
|
68
56
|
- lib/generators/responders/install_generator.rb
|
69
57
|
- lib/responders.rb
|
70
58
|
- lib/responders/collection_responder.rb
|
@@ -72,13 +60,16 @@ files:
|
|
72
60
|
- lib/responders/flash_responder.rb
|
73
61
|
- lib/responders/http_cache_responder.rb
|
74
62
|
- lib/responders/locales/en.yml
|
75
|
-
- lib/responders/location_responder.rb
|
76
63
|
- lib/responders/version.rb
|
77
|
-
homepage: https://github.com/
|
64
|
+
homepage: https://github.com/heartcombo/responders
|
78
65
|
licenses:
|
79
66
|
- MIT
|
80
|
-
metadata:
|
81
|
-
|
67
|
+
metadata:
|
68
|
+
homepage_uri: https://github.com/heartcombo/responders
|
69
|
+
changelog_uri: https://github.com/heartcombo/responders/blob/master/CHANGELOG.md
|
70
|
+
source_code_uri: https://github.com/heartcombo/responders
|
71
|
+
bug_tracker_uri: https://github.com/heartcombo/responders/issues
|
72
|
+
post_install_message:
|
82
73
|
rdoc_options: []
|
83
74
|
require_paths:
|
84
75
|
- lib
|
@@ -86,16 +77,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
77
|
requirements:
|
87
78
|
- - ">="
|
88
79
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
80
|
+
version: 2.5.0
|
90
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
82
|
requirements:
|
92
83
|
- - ">="
|
93
84
|
- !ruby/object:Gem::Version
|
94
85
|
version: '0'
|
95
86
|
requirements: []
|
96
|
-
|
97
|
-
|
98
|
-
signing_key:
|
87
|
+
rubygems_version: 3.4.5
|
88
|
+
signing_key:
|
99
89
|
specification_version: 4
|
100
90
|
summary: A set of Rails responders to dry up your application
|
101
91
|
test_files: []
|
File without changes
|
File without changes
|