responders 2.0.0 → 3.0.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 +54 -1
- data/MIT-LICENSE +2 -1
- data/README.md +117 -48
- data/lib/action_controller/respond_with.rb +44 -15
- data/lib/action_controller/responder.rb +33 -24
- 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 +10 -0
- data/lib/responders/collection_responder.rb +2 -0
- data/lib/responders/controller_method.rb +7 -2
- data/lib/responders/flash_responder.rb +32 -18
- data/lib/responders/http_cache_responder.rb +5 -3
- data/lib/responders/location_responder.rb +3 -1
- data/lib/responders/version.rb +3 -1
- data/lib/responders.rb +13 -16
- metadata +24 -52
- data/lib/generators/rails/templates/controller.rb +0 -55
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 00a016ab308a0960fc75a6dc1d6f8b35b3165fe0edfef25cb3b898631c7c0454
|
4
|
+
data.tar.gz: bf658c0505d528307b5b61b9c1854fa76b50269c7ddedc27f7c3816e588c2a1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 316cf7158bbfede88e505b1b2dfe25cf86ef873f678362da6062d5b4093849a190da2f730265021e7a3613f5a5bb1622553bc423d8850226ee5235083a9cf38b
|
7
|
+
data.tar.gz: 7aa5dbc3ef20b9c7ddafa0d8342067eaccfc2b50231eef76c00cf7c9a5e07703d449110ee4ec8dc4f3dcb9fa7c06472e0635cc3c41d05820a193c9f7445d14e7
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,61 @@
|
|
1
|
+
## 3.0.1
|
2
|
+
|
3
|
+
* Add support to Ruby 2.7
|
4
|
+
|
5
|
+
## 3.0.0
|
6
|
+
|
7
|
+
* Remove support for Rails 4.2
|
8
|
+
* Remove support for Ruby < 2.4
|
9
|
+
|
10
|
+
## 2.4.1
|
11
|
+
|
12
|
+
* Add support for Rails 6 beta
|
13
|
+
|
14
|
+
## 2.4.0
|
15
|
+
|
16
|
+
* `respond_with` now accepts a new kwargs called `:render` which goes straight to the `render`
|
17
|
+
call after an unsuccessful post request. Usefull if for example you need to render a template
|
18
|
+
which is outside of controller's path eg:
|
19
|
+
|
20
|
+
`respond_with resource, render: { template: 'path/to/template' }`
|
21
|
+
|
22
|
+
## 2.3.0
|
23
|
+
|
24
|
+
* `verify_request_format!` is aliased to `verify_requested_format!` now.
|
25
|
+
* Implementing the `interpolation_options` method on your controller is deprecated
|
26
|
+
in favor of naming it `flash_interpolation_options` instead.
|
27
|
+
|
28
|
+
## 2.2.0
|
29
|
+
|
30
|
+
* Added the `verify_request_format!` method, that can be used as a `before_action`
|
31
|
+
callback to prevent your actions from being invoked when the controller does
|
32
|
+
not respond to the request mime type, preventing the execution of complex
|
33
|
+
queries or creating/deleting records from your app.
|
34
|
+
|
35
|
+
## 2.1.2
|
36
|
+
|
37
|
+
* Fix rendering when using `ActionController::API`. (by @eLod)
|
38
|
+
* Added API controller template for the controller generator. (by @vestimir)
|
39
|
+
|
40
|
+
## 2.1.1
|
41
|
+
|
42
|
+
* Added support for Rails 5.
|
43
|
+
|
44
|
+
## 2.1.0
|
45
|
+
|
46
|
+
* No longer automatically set the responders generator as many projects may use this gem as a dependency. When upgrading, users will need to add `config.app_generators.scaffold_controller :responders_controller` to their application. The `responders:install` generator has been updated to automatically insert it in new applications
|
47
|
+
|
48
|
+
## 2.0.1
|
49
|
+
|
50
|
+
* Require `rails/railtie` explicitly before using it
|
51
|
+
* Require `action_controller` explicitly before using it
|
52
|
+
* Remove unnecessary and limiting `resourceful?` check that required models to implement `to_#{format}` (such checks are responsibility of the rendering layer)
|
53
|
+
|
1
54
|
## 2.0.0
|
2
55
|
|
3
56
|
* Import `respond_with` and class-level `respond_to` from Rails
|
4
57
|
* Support only Rails ~> 4.2
|
5
|
-
* `Responders::LocationResponder` is now included by
|
58
|
+
* `Responders::LocationResponder` is now included by the default responder (and therefore deprecated)
|
6
59
|
|
7
60
|
## 1.1.0
|
8
61
|
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
Copyright
|
1
|
+
Copyright (c) 2020 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,25 @@
|
|
1
1
|
# Responders
|
2
2
|
|
3
|
-
[](http://badge.fury.io/rb/responders)
|
4
|
+
[](http://travis-ci.org/heartcombo/responders)
|
5
|
+
[](https://codeclimate.com/github/heartcombo/responders)
|
6
6
|
|
7
|
-
A set of responders modules to dry up your Rails
|
7
|
+
A set of responders modules to dry up your Rails app.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the responders gem to your Gemfile:
|
12
|
+
|
13
|
+
gem "responders"
|
14
|
+
|
15
|
+
Update your bundle and run the install generator:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
$ rails g responders:install
|
19
|
+
|
20
|
+
If you are including this gem to support backwards compatibilty for responders in previous releases of Rails, you only need to include the gem and bundle.
|
21
|
+
|
22
|
+
$ bundle install
|
8
23
|
|
9
24
|
## Responders Types
|
10
25
|
|
@@ -60,77 +75,78 @@ You can also have embedded HTML. Just create a `_html` scope.
|
|
60
75
|
|
61
76
|
See also the `namespace_lookup` option to search the full hierarchy of possible keys.
|
62
77
|
|
63
|
-
###
|
78
|
+
### HttpCacheResponder
|
79
|
+
|
80
|
+
Automatically adds Last-Modified headers to API requests. This
|
81
|
+
allows clients to easily query the server if a resource changed and if the client tries
|
82
|
+
to retrieve a resource that has not been modified, it returns not_modified status.
|
83
|
+
|
84
|
+
### CollectionResponder
|
85
|
+
|
86
|
+
Makes your create and update action redirect to the collection on success.
|
87
|
+
|
88
|
+
### LocationResponder
|
64
89
|
|
65
90
|
This responder allows you to use callable objects as the redirect location.
|
66
91
|
Useful when you want to use the `respond_with` method with
|
67
92
|
a custom route that requires persisted objects, but the validation may fail.
|
68
93
|
|
94
|
+
Note: this responder is included by default, and doesn't need to be included
|
95
|
+
on the top of your controller (including it will issue a deprecation warning).
|
96
|
+
|
69
97
|
```ruby
|
70
98
|
class ThingsController < ApplicationController
|
71
|
-
responders :location, :flash
|
72
99
|
respond_to :html
|
73
100
|
|
74
101
|
def create
|
75
|
-
@thing =
|
102
|
+
@thing = Thing.create(params[:thing])
|
76
103
|
respond_with @thing, location: -> { thing_path(@thing) }
|
77
104
|
end
|
78
105
|
end
|
79
106
|
```
|
80
107
|
|
81
|
-
|
82
|
-
|
83
|
-
Automatically adds Last-Modified headers to API requests. This
|
84
|
-
allows clients to easily query the server if a resource changed and if the client tries
|
85
|
-
to retrieve a resource that has not been modified, it returns not_modified status.
|
86
|
-
|
87
|
-
### CollectionResponder
|
88
|
-
|
89
|
-
Makes your create and update action redirect to the collection on success.
|
108
|
+
**Dealing with namespaced routes**
|
90
109
|
|
91
|
-
|
110
|
+
In order for the LocationResponder to find the correct route helper for namespaced routes you need to pass the namespaces to `respond_with`:
|
92
111
|
|
93
|
-
|
112
|
+
```ruby
|
113
|
+
class Api::V1::ThingsController < ApplicationController
|
114
|
+
respond_to :json
|
94
115
|
|
95
|
-
|
96
|
-
|
116
|
+
# POST /api/v1/things
|
117
|
+
def create
|
118
|
+
@thing = Thing.create(thing_params)
|
119
|
+
respond_with :api, :v1, @thing
|
120
|
+
end
|
121
|
+
end
|
97
122
|
```
|
98
123
|
|
99
|
-
|
100
|
-
|
101
|
-
```ruby
|
102
|
-
gem 'responders'
|
103
|
-
```
|
124
|
+
## Configuring your own responder
|
104
125
|
|
105
|
-
Responders only provides a set of modules
|
106
|
-
responder.
|
126
|
+
Responders only provides a set of modules and to use them you have to create your own
|
127
|
+
responder. After you run the install command, the following responder will be
|
128
|
+
generated in your application:
|
107
129
|
|
108
130
|
```ruby
|
109
|
-
# lib/
|
110
|
-
class
|
131
|
+
# lib/application_responder.rb
|
132
|
+
class ApplicationResponder < ActionController::Responder
|
111
133
|
include Responders::FlashResponder
|
112
134
|
include Responders::HttpCacheResponder
|
113
135
|
end
|
114
136
|
```
|
115
137
|
|
116
|
-
|
138
|
+
Your application also needs to be configured to use it:
|
117
139
|
|
118
140
|
```ruby
|
119
141
|
# app/controllers/application_controller.rb
|
120
|
-
require "
|
142
|
+
require "application_responder"
|
121
143
|
|
122
144
|
class ApplicationController < ActionController::Base
|
123
|
-
self.responder =
|
145
|
+
self.responder = ApplicationResponder
|
124
146
|
respond_to :html
|
125
147
|
end
|
126
148
|
```
|
127
149
|
|
128
|
-
Or, for your convenience, just do:
|
129
|
-
|
130
|
-
```console
|
131
|
-
rails generate responders:install
|
132
|
-
```
|
133
|
-
|
134
150
|
## Controller method
|
135
151
|
|
136
152
|
This gem also includes the controller method `responders`, which allows you to cherry-pick which
|
@@ -144,7 +160,7 @@ end
|
|
144
160
|
|
145
161
|
## Interpolation Options
|
146
162
|
|
147
|
-
You can pass in extra interpolation options for the translation by adding an `
|
163
|
+
You can pass in extra interpolation options for the translation by adding an `flash_interpolation_options` method to your controller:
|
148
164
|
|
149
165
|
```ruby
|
150
166
|
class InvitationsController < ApplicationController
|
@@ -157,33 +173,86 @@ class InvitationsController < ApplicationController
|
|
157
173
|
|
158
174
|
private
|
159
175
|
|
160
|
-
def
|
176
|
+
def flash_interpolation_options
|
161
177
|
{ resource_name: @invitation.email }
|
162
178
|
end
|
163
179
|
end
|
164
180
|
```
|
165
181
|
|
166
|
-
Now you would see the message "
|
182
|
+
Now you would see the message `"name@example.com was successfully created"` instead of the default `"Invitation was successfully created."`
|
167
183
|
|
168
184
|
## Generator
|
169
185
|
|
170
186
|
This gem also includes a responders controller generator, so your scaffold can be customized
|
171
|
-
to use `respond_with` instead of default `respond_to` blocks.
|
172
|
-
|
187
|
+
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
|
+
|
189
|
+
```ruby
|
190
|
+
config.app_generators.scaffold_controller :responders_controller
|
191
|
+
```
|
192
|
+
|
193
|
+
## Failure handling
|
194
|
+
|
195
|
+
Responders don't use `valid?` to check for errors in models to figure out if
|
196
|
+
the request was successful or not, and relies on your controllers to call
|
197
|
+
`save` or `create` to trigger the validations.
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
def create
|
201
|
+
@widget = Widget.new(widget_params)
|
202
|
+
# @widget will be a valid record for responders, as we haven't called `save`
|
203
|
+
# on it, and will always redirect to the `widgets_path`.
|
204
|
+
respond_with @widget, location: -> { widgets_path }
|
205
|
+
end
|
206
|
+
```
|
207
|
+
|
208
|
+
Responders will check if the `errors` object in your model is empty or not. Take
|
209
|
+
this in consideration when implementing different actions or writing test
|
210
|
+
assertions on this behavior for your controllers.
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
def create
|
214
|
+
@widget = Widget.new(widget_params)
|
215
|
+
@widget.errors.add(:base, :invalid)
|
216
|
+
# `respond_with` will render the `new` template again.
|
217
|
+
respond_with @widget
|
218
|
+
end
|
219
|
+
```
|
220
|
+
|
221
|
+
## Verifying request formats
|
222
|
+
|
223
|
+
`respond_with` will raise an `ActionController::UnknownFormat` if the request
|
224
|
+
MIME type was not configured through the class level `respond_to`, but the
|
225
|
+
action will still be executed and any side effects (like creating a new record)
|
226
|
+
will still occur. To raise the `UnknownFormat` exception before your action
|
227
|
+
is invoked you can set the `verify_requested_format!` method as a `before_action`
|
228
|
+
on your controller.
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
class WidgetsController < ApplicationController
|
232
|
+
respond_to :json
|
233
|
+
before_action :verify_requested_format!
|
234
|
+
|
235
|
+
# POST /widgets.html won't reach the `create` action.
|
236
|
+
def create
|
237
|
+
widget = Widget.create(widget_params)
|
238
|
+
respond_with widget
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
```
|
173
243
|
|
174
244
|
## Examples
|
175
245
|
|
176
|
-
Want more examples ? Check out
|
246
|
+
Want more examples ? Check out these blog posts:
|
177
247
|
|
178
|
-
* [One in Three: Inherited Resources, Has Scope and Responders](http://blog.plataformatec.com.br/2009/12/one-in-three-inherited-resources-has-scope-and-responders/)
|
179
248
|
* [Embracing REST with mind, body and soul](http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/)
|
180
249
|
* [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder/)
|
181
|
-
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3
|
250
|
+
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3)
|
182
251
|
|
183
252
|
## Bugs and Feedback
|
184
253
|
|
185
254
|
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
186
255
|
|
187
|
-
http://github.com/
|
256
|
+
http://github.com/heartcombo/responders/issues
|
188
257
|
|
189
|
-
MIT License. Copyright
|
258
|
+
MIT License. Copyright 2020 Rafael França, Carlos Antônio da Silva. Copyright 2009-2019 Plataformatec.
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/extract_options"
|
4
|
+
require "action_controller/metal/mime_responds"
|
3
5
|
|
4
6
|
module ActionController #:nodoc:
|
5
7
|
module RespondWith
|
@@ -37,17 +39,17 @@ 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|
|
45
47
|
mime = mime.to_sym
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
hash[mime] = {}
|
49
|
+
hash[mime][:only] = only_actions unless only_actions.empty?
|
50
|
+
hash[mime][:except] = except_actions unless except_actions.empty?
|
49
51
|
end
|
50
|
-
self.mimes_for_respond_to =
|
52
|
+
self.mimes_for_respond_to = hash.freeze
|
51
53
|
end
|
52
54
|
|
53
55
|
# Clear all mime types in <tt>respond_to</tt>.
|
@@ -175,26 +177,34 @@ module ActionController #:nodoc:
|
|
175
177
|
# Also, a hash passed to +respond_with+ immediately after the specified
|
176
178
|
# resource(s) is interpreted as a set of options relevant to all
|
177
179
|
# formats. Any option accepted by +render+ can be used, e.g.
|
180
|
+
#
|
178
181
|
# respond_with @people, status: 200
|
182
|
+
#
|
179
183
|
# However, note that these options are ignored after an unsuccessful attempt
|
180
184
|
# to save a resource, e.g. when automatically rendering <tt>:new</tt>
|
181
185
|
# after a post request.
|
182
186
|
#
|
183
|
-
#
|
187
|
+
# Three additional options are relevant specifically to +respond_with+ -
|
184
188
|
# 1. <tt>:location</tt> - overwrites the default redirect location used after
|
185
189
|
# a successful html +post+ request.
|
186
190
|
# 2. <tt>:action</tt> - overwrites the default render action used after an
|
187
191
|
# unsuccessful html +post+ request.
|
192
|
+
# 3. <tt>:render</tt> - allows to pass any options directly to the <tt>:render<tt/>
|
193
|
+
# call after unsuccessful html +post+ request. Useful if for example you
|
194
|
+
# need to render a template which is outside of controller's path or you
|
195
|
+
# want to override the default http <tt>:status</tt> code, e.g.
|
196
|
+
#
|
197
|
+
# respond_with(resource, render: { template: 'path/to/template', status: 422 })
|
188
198
|
def respond_with(*resources, &block)
|
189
199
|
if self.class.mimes_for_respond_to.empty?
|
190
200
|
raise "In order to use respond_with, first you need to declare the " \
|
191
201
|
"formats your controller responds to in the class level."
|
192
202
|
end
|
193
203
|
|
194
|
-
mimes = collect_mimes_from_class_level
|
204
|
+
mimes = collect_mimes_from_class_level
|
195
205
|
collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
|
196
206
|
block.call(collector) if block_given?
|
197
|
-
|
207
|
+
|
198
208
|
if format = collector.negotiate_format(request)
|
199
209
|
_process_format(format)
|
200
210
|
options = resources.size == 1 ? {} : resources.extract_options!
|
@@ -206,12 +216,31 @@ module ActionController #:nodoc:
|
|
206
216
|
end
|
207
217
|
end
|
208
218
|
|
209
|
-
|
219
|
+
protected
|
220
|
+
|
221
|
+
# Before action callback that can be used to prevent requests that do not
|
222
|
+
# match the mime types defined through <tt>respond_to</tt> from being executed.
|
223
|
+
#
|
224
|
+
# class PeopleController < ApplicationController
|
225
|
+
# respond_to :html, :xml, :json
|
226
|
+
#
|
227
|
+
# before_action :verify_requested_format!
|
228
|
+
# end
|
229
|
+
def verify_requested_format!
|
230
|
+
mimes = collect_mimes_from_class_level
|
231
|
+
collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
|
232
|
+
|
233
|
+
unless collector.negotiate_format(request)
|
234
|
+
raise ActionController::UnknownFormat
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
alias :verify_request_format! :verify_requested_format!
|
210
239
|
|
211
240
|
# Collect mimes declared in the class method respond_to valid for the
|
212
241
|
# current action.
|
213
242
|
def collect_mimes_from_class_level #:nodoc:
|
214
|
-
action = action_name.
|
243
|
+
action = action_name.to_sym
|
215
244
|
|
216
245
|
self.class.mimes_for_respond_to.keys.select do |mime|
|
217
246
|
config = self.class.mimes_for_respond_to[mime]
|
@@ -226,4 +255,4 @@ module ActionController #:nodoc:
|
|
226
255
|
end
|
227
256
|
end
|
228
257
|
end
|
229
|
-
end
|
258
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/json"
|
2
4
|
|
3
5
|
module ActionController #:nodoc:
|
4
6
|
# Responsible for exposing a resource to different mime requests,
|
@@ -121,12 +123,12 @@ module ActionController #:nodoc:
|
|
121
123
|
attr_reader :controller, :request, :format, :resource, :resources, :options
|
122
124
|
|
123
125
|
DEFAULT_ACTIONS_FOR_VERBS = {
|
124
|
-
:
|
125
|
-
:
|
126
|
-
:
|
126
|
+
post: :new,
|
127
|
+
patch: :edit,
|
128
|
+
put: :edit
|
127
129
|
}
|
128
130
|
|
129
|
-
def initialize(controller, resources, options={})
|
131
|
+
def initialize(controller, resources, options = {})
|
130
132
|
@controller = controller
|
131
133
|
@request = @controller.request
|
132
134
|
@format = @controller.formats.first
|
@@ -142,8 +144,8 @@ module ActionController #:nodoc:
|
|
142
144
|
end
|
143
145
|
end
|
144
146
|
|
145
|
-
delegate :head, :render, :redirect_to, :
|
146
|
-
delegate :get?, :post?, :patch?, :put?, :delete?, :
|
147
|
+
delegate :head, :render, :redirect_to, to: :controller
|
148
|
+
delegate :get?, :post?, :patch?, :put?, :delete?, to: :request
|
147
149
|
|
148
150
|
# Undefine :to_json and :to_yaml since it's defined on Object
|
149
151
|
undef_method(:to_json) if method_defined?(:to_json)
|
@@ -182,13 +184,15 @@ module ActionController #:nodoc:
|
|
182
184
|
# responds to :to_format and display it.
|
183
185
|
#
|
184
186
|
def to_format
|
185
|
-
if get?
|
187
|
+
if !get? && has_errors? && !response_overridden?
|
188
|
+
display_errors
|
189
|
+
elsif has_view_rendering? || response_overridden?
|
186
190
|
default_render
|
187
191
|
else
|
188
|
-
|
192
|
+
api_behavior
|
189
193
|
end
|
190
|
-
rescue ActionView::MissingTemplate
|
191
|
-
api_behavior
|
194
|
+
rescue ActionView::MissingTemplate
|
195
|
+
api_behavior
|
192
196
|
end
|
193
197
|
|
194
198
|
protected
|
@@ -198,32 +202,25 @@ module ActionController #:nodoc:
|
|
198
202
|
if get?
|
199
203
|
raise error
|
200
204
|
elsif has_errors? && default_action
|
201
|
-
render
|
205
|
+
render rendering_options
|
202
206
|
else
|
203
207
|
redirect_to navigation_location
|
204
208
|
end
|
205
209
|
end
|
206
210
|
|
207
211
|
# This is the common behavior for formats associated with APIs, such as :xml and :json.
|
208
|
-
def api_behavior
|
209
|
-
raise error unless resourceful?
|
212
|
+
def api_behavior
|
210
213
|
raise MissingRenderer.new(format) unless has_renderer?
|
211
214
|
|
212
215
|
if get?
|
213
216
|
display resource
|
214
217
|
elsif post?
|
215
|
-
display resource, :
|
218
|
+
display resource, status: :created, location: api_location
|
216
219
|
else
|
217
220
|
head :no_content
|
218
221
|
end
|
219
222
|
end
|
220
223
|
|
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
224
|
# Returns the resource location by retrieving it from the options or
|
228
225
|
# returning the resources array.
|
229
226
|
#
|
@@ -240,7 +237,7 @@ module ActionController #:nodoc:
|
|
240
237
|
if @default_response
|
241
238
|
@default_response.call(options)
|
242
239
|
else
|
243
|
-
controller.
|
240
|
+
controller.render(options)
|
244
241
|
end
|
245
242
|
end
|
246
243
|
|
@@ -261,7 +258,7 @@ module ActionController #:nodoc:
|
|
261
258
|
#
|
262
259
|
# render xml: @user, status: :created
|
263
260
|
#
|
264
|
-
def display(resource, given_options={})
|
261
|
+
def display(resource, given_options = {})
|
265
262
|
controller.render given_options.merge!(options).merge!(format => resource)
|
266
263
|
end
|
267
264
|
|
@@ -280,6 +277,10 @@ module ActionController #:nodoc:
|
|
280
277
|
Renderers::RENDERERS.include?(format)
|
281
278
|
end
|
282
279
|
|
280
|
+
def has_view_rendering?
|
281
|
+
controller.class.include? ActionView::Rendering
|
282
|
+
end
|
283
|
+
|
283
284
|
# By default, render the <code>:edit</code> action for HTML requests with errors, unless
|
284
285
|
# the verb was POST.
|
285
286
|
#
|
@@ -292,11 +293,19 @@ module ActionController #:nodoc:
|
|
292
293
|
end
|
293
294
|
|
294
295
|
def json_resource_errors
|
295
|
-
{:
|
296
|
+
{ errors: resource.errors }
|
296
297
|
end
|
297
298
|
|
298
299
|
def response_overridden?
|
299
300
|
@default_response.present?
|
300
301
|
end
|
302
|
+
|
303
|
+
def rendering_options
|
304
|
+
if options[:render]
|
305
|
+
options[:render]
|
306
|
+
else
|
307
|
+
{ action: default_action }
|
308
|
+
end
|
309
|
+
end
|
301
310
|
end
|
302
311
|
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 -%>
|