responders 2.1.0 → 2.1.2
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 +9 -0
- data/MIT-LICENSE +1 -1
- data/README.md +55 -7
- data/lib/action_controller/respond_with.rb +2 -0
- data/lib/action_controller/responder.rb +9 -3
- data/lib/generators/rails/templates/api_controller.rb +51 -0
- data/lib/responders/controller_method.rb +3 -1
- data/lib/responders/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8dce735a1e17c51056e99b880daafe35f666771f
|
4
|
+
data.tar.gz: 61548c0e60e01fa193a5b01ce93f21b1e9c5181f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c5e9f72023667804fbb1589dc4512db20e10a4cca06c643fdc85475c713fd16b6cef65936c3bbc1bbe7beba1460277f32b2f82973ab5f2080acce9ae9772260
|
7
|
+
data.tar.gz: 8bc67ddcb91fb1834483cd99e53f3cfd397b3639b9c0b022996ad2e77d06dcb501cab89a8f586c0f0fc224530ed952fecce88bf9680a9c62d7808e0492420a9a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## 2.1.2
|
2
|
+
|
3
|
+
* Fix rendering when using `ActionController::API`. (by @eLod)
|
4
|
+
* Added API controller template for the controller generator. (by @vestimir)
|
5
|
+
|
6
|
+
## 2.1.1
|
7
|
+
|
8
|
+
* Added support for Rails 5.
|
9
|
+
|
1
10
|
## 2.1.0
|
2
11
|
|
3
12
|
* 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
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright 2009-
|
1
|
+
Copyright 2009-2016 Plataformatec. http://plataformatec.com.br
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Responders
|
2
2
|
|
3
|
-
[](http://badge.fury.io/rb/responders)
|
4
|
+
[](http://travis-ci.org/plataformatec/responders)
|
5
|
+
[](https://codeclimate.com/github/plataformatec/responders)
|
6
6
|
|
7
7
|
A set of responders modules to dry up your Rails 4.2+ app.
|
8
8
|
|
@@ -17,6 +17,10 @@ Update your bundle and run the install generator:
|
|
17
17
|
$ bundle install
|
18
18
|
$ rails g responders:install
|
19
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
|
23
|
+
|
20
24
|
## Responders Types
|
21
25
|
|
22
26
|
### FlashResponder
|
@@ -101,6 +105,22 @@ class ThingsController < ApplicationController
|
|
101
105
|
end
|
102
106
|
```
|
103
107
|
|
108
|
+
**Dealing with namespaced routes**
|
109
|
+
|
110
|
+
In order for the LocationResponder to find the correct route helper for namespaced routes you need to pass the namespaces to `respond_with`:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
class Api::V1::ThingsController < ApplicationController
|
114
|
+
respond_to :json
|
115
|
+
|
116
|
+
# POST /api/v1/things
|
117
|
+
def create
|
118
|
+
@thing = Thing.create(thing_params)
|
119
|
+
respond_with :api, :v1, @thing
|
120
|
+
end
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
104
124
|
## Configuring your own responder
|
105
125
|
|
106
126
|
Responders only provides a set of modules and to use them you have to create your own
|
@@ -109,7 +129,7 @@ generated in your application:
|
|
109
129
|
|
110
130
|
```ruby
|
111
131
|
# lib/application_responder.rb
|
112
|
-
class
|
132
|
+
class ApplicationResponder < ActionController::Responder
|
113
133
|
include Responders::FlashResponder
|
114
134
|
include Responders::HttpCacheResponder
|
115
135
|
end
|
@@ -122,7 +142,7 @@ Your application also needs to be configured to use it:
|
|
122
142
|
require "application_responder"
|
123
143
|
|
124
144
|
class ApplicationController < ActionController::Base
|
125
|
-
self.responder =
|
145
|
+
self.responder = ApplicationResponder
|
126
146
|
respond_to :html
|
127
147
|
end
|
128
148
|
```
|
@@ -168,13 +188,41 @@ to use `respond_with` instead of default `respond_to` blocks. From 2.1, you need
|
|
168
188
|
|
169
189
|
config.app_generators.scaffold_controller :responders_controller
|
170
190
|
|
191
|
+
#Failure handling
|
192
|
+
|
193
|
+
Responders don't use `valid?` to check for errors in models to figure out if
|
194
|
+
the request was successfull or not, and relies on your controllers to call
|
195
|
+
`save` or `create` to trigger the validations.
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
def create
|
199
|
+
@widget = Widget.new(widget_params)
|
200
|
+
# @widget will be a valid record for responders, as we haven't called `save`
|
201
|
+
# on it, and will always redirect to the `widgets_path`.
|
202
|
+
respond_with @widget, location: -> { widgets_path }
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
Responders will check if the `errors` object in your model is empty or not. Take
|
207
|
+
this in consideration when implementing different actions or writing test
|
208
|
+
assertions on this behavior for your controllers.
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
def create
|
212
|
+
@widget = Widget.new(widget_params)
|
213
|
+
@widget.errors.add(:base, :invalid)
|
214
|
+
# `respond_with` will render the `new` template again.
|
215
|
+
respond_with @widget
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
171
219
|
## Examples
|
172
220
|
|
173
221
|
Want more examples ? Check out this blog posts:
|
174
222
|
|
175
223
|
* [Embracing REST with mind, body and soul](http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/)
|
176
224
|
* [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder/)
|
177
|
-
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3
|
225
|
+
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3)
|
178
226
|
|
179
227
|
## Bugs and Feedback
|
180
228
|
|
@@ -182,4 +230,4 @@ If you discover any bugs or want to drop a line, feel free to create an issue on
|
|
182
230
|
|
183
231
|
http://github.com/plataformatec/responders/issues
|
184
232
|
|
185
|
-
MIT License. Copyright 2009-
|
233
|
+
MIT License. Copyright 2009-2016 Plataformatec. http://plataformatec.com.br
|
@@ -175,7 +175,9 @@ module ActionController #:nodoc:
|
|
175
175
|
# Also, a hash passed to +respond_with+ immediately after the specified
|
176
176
|
# resource(s) is interpreted as a set of options relevant to all
|
177
177
|
# formats. Any option accepted by +render+ can be used, e.g.
|
178
|
+
#
|
178
179
|
# respond_with @people, status: 200
|
180
|
+
#
|
179
181
|
# However, note that these options are ignored after an unsuccessful attempt
|
180
182
|
# to save a resource, e.g. when automatically rendering <tt>:new</tt>
|
181
183
|
# after a post request.
|
@@ -182,10 +182,12 @@ module ActionController #:nodoc:
|
|
182
182
|
# responds to :to_format and display it.
|
183
183
|
#
|
184
184
|
def to_format
|
185
|
-
if get?
|
185
|
+
if !get? && has_errors? && !response_overridden?
|
186
|
+
display_errors
|
187
|
+
elsif has_view_rendering? || response_overridden?
|
186
188
|
default_render
|
187
189
|
else
|
188
|
-
|
190
|
+
api_behavior
|
189
191
|
end
|
190
192
|
rescue ActionView::MissingTemplate
|
191
193
|
api_behavior
|
@@ -233,7 +235,7 @@ module ActionController #:nodoc:
|
|
233
235
|
if @default_response
|
234
236
|
@default_response.call(options)
|
235
237
|
else
|
236
|
-
controller.
|
238
|
+
controller.render(options)
|
237
239
|
end
|
238
240
|
end
|
239
241
|
|
@@ -273,6 +275,10 @@ module ActionController #:nodoc:
|
|
273
275
|
Renderers::RENDERERS.include?(format)
|
274
276
|
end
|
275
277
|
|
278
|
+
def has_view_rendering?
|
279
|
+
controller.class.include? ActionView::Rendering
|
280
|
+
end
|
281
|
+
|
276
282
|
# By default, render the <code>:edit</code> action for HTML requests with errors, unless
|
277
283
|
# the verb was POST.
|
278
284
|
#
|
@@ -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 -%>
|
data/lib/responders/version.rb
CHANGED
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: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Valim
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 4.2.0
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5'
|
22
|
+
version: '5.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: 4.2.0
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5'
|
32
|
+
version: '5.1'
|
33
33
|
description: A set of Rails responders to dry up your application
|
34
34
|
email: contact@plataformatec.com.br
|
35
35
|
executables: []
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/action_controller/responder.rb
|
44
44
|
- lib/generators/rails/USAGE
|
45
45
|
- lib/generators/rails/responders_controller_generator.rb
|
46
|
+
- lib/generators/rails/templates/api_controller.rb
|
46
47
|
- lib/generators/rails/templates/controller.rb
|
47
48
|
- lib/generators/responders/install_generator.rb
|
48
49
|
- lib/responders.rb
|
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
74
|
version: '0'
|
74
75
|
requirements: []
|
75
76
|
rubyforge_project: responders
|
76
|
-
rubygems_version: 2.
|
77
|
+
rubygems_version: 2.5.1
|
77
78
|
signing_key:
|
78
79
|
specification_version: 4
|
79
80
|
summary: A set of Rails responders to dry up your application
|