responders 2.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 921e032d21bf201f9199b401ecdca88cbbc9668f
4
- data.tar.gz: e2f710246648471f2f62fa37305c972497a2320a
3
+ metadata.gz: 8dce735a1e17c51056e99b880daafe35f666771f
4
+ data.tar.gz: 61548c0e60e01fa193a5b01ce93f21b1e9c5181f
5
5
  SHA512:
6
- metadata.gz: 7a1de1eab1ecd945eed1c5ea80c14bb9570902c00a2f19f9de2404bd267a5cc6ffad885c737594ec6ed559df473ae5e64ce293a9ed986a724cac458ba54ac56f
7
- data.tar.gz: d02ec53cc1cb3b7001e03effb6167fdfb57b8823a8dbbf3d04eeaa029f94249e98dde1cb7154db9c451585a3c524b9ec6b680f4e56a9695ba87862fc7a74b5e0
6
+ metadata.gz: 1c5e9f72023667804fbb1589dc4512db20e10a4cca06c643fdc85475c713fd16b6cef65936c3bbc1bbe7beba1460277f32b2f82973ab5f2080acce9ae9772260
7
+ data.tar.gz: 8bc67ddcb91fb1834483cd99e53f3cfd397b3639b9c0b022996ad2e77d06dcb501cab89a8f586c0f0fc224530ed952fecce88bf9680a9c62d7808e0492420a9a
@@ -1,3 +1,8 @@
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
+
1
6
  ## 2.1.1
2
7
 
3
8
  * Added support for Rails 5.
@@ -1,4 +1,4 @@
1
- Copyright 2009-2015 Plataformatec. http://plataformatec.com.br
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
@@ -188,13 +188,41 @@ to use `respond_with` instead of default `respond_to` blocks. From 2.1, you need
188
188
 
189
189
  config.app_generators.scaffold_controller :responders_controller
190
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
+
191
219
  ## Examples
192
220
 
193
221
  Want more examples ? Check out this blog posts:
194
222
 
195
223
  * [Embracing REST with mind, body and soul](http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/)
196
224
  * [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder/)
197
- * [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)
198
226
 
199
227
  ## Bugs and Feedback
200
228
 
@@ -202,4 +230,4 @@ If you discover any bugs or want to drop a line, feel free to create an issue on
202
230
 
203
231
  http://github.com/plataformatec/responders/issues
204
232
 
205
- MIT License. Copyright 2009-2015 Plataformatec. http://plataformatec.com.br
233
+ MIT License. Copyright 2009-2016 Plataformatec. http://plataformatec.com.br
@@ -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? || !has_errors? || response_overridden?
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
- display_errors
190
+ api_behavior
189
191
  end
190
192
  rescue ActionView::MissingTemplate
191
193
  api_behavior
@@ -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 -%>
@@ -1,3 +1,3 @@
1
1
  module Responders
2
- VERSION = "2.1.1".freeze
2
+ VERSION = "2.1.2".freeze
3
3
  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: 2.1.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: 2015-12-19 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -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.4.5
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