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.
Files changed (37) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +54 -1
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +117 -48
  5. data/lib/action_controller/respond_with.rb +44 -15
  6. data/lib/action_controller/responder.rb +33 -24
  7. data/lib/generators/rails/responders_controller_generator.rb +4 -26
  8. data/lib/generators/rails/templates/api_controller.rb.tt +51 -0
  9. data/lib/generators/rails/templates/controller.rb.tt +59 -0
  10. data/lib/generators/responders/install_generator.rb +10 -0
  11. data/lib/responders/collection_responder.rb +2 -0
  12. data/lib/responders/controller_method.rb +7 -2
  13. data/lib/responders/flash_responder.rb +32 -18
  14. data/lib/responders/http_cache_responder.rb +5 -3
  15. data/lib/responders/location_responder.rb +3 -1
  16. data/lib/responders/version.rb +3 -1
  17. data/lib/responders.rb +13 -16
  18. metadata +24 -52
  19. data/lib/generators/rails/templates/controller.rb +0 -55
  20. data/test/action_controller/respond_with_test.rb +0 -717
  21. data/test/locales/en.yml +0 -28
  22. data/test/responders/collection_responder_test.rb +0 -82
  23. data/test/responders/controller_method_test.rb +0 -72
  24. data/test/responders/flash_responder_test.rb +0 -260
  25. data/test/responders/http_cache_responder_test.rb +0 -120
  26. data/test/test_helper.rb +0 -87
  27. data/test/views/addresses/create.js.erb +0 -1
  28. data/test/views/addresses/edit.html.erb +0 -1
  29. data/test/views/addresses/new.html.erb +0 -1
  30. data/test/views/locations/new.html.erb +0 -1
  31. data/test/views/respond_with/edit.html.erb +0 -1
  32. data/test/views/respond_with/new.html.erb +0 -1
  33. data/test/views/respond_with/respond_with_additional_params.html.erb +0 -0
  34. data/test/views/respond_with/using_invalid_resource_with_template.xml.erb +0 -1
  35. data/test/views/respond_with/using_options_with_template.xml.erb +0 -1
  36. data/test/views/respond_with/using_resource.js.erb +0 -1
  37. data/test/views/respond_with/using_resource_with_block.html.erb +0 -1
data/test/locales/en.yml DELETED
@@ -1,28 +0,0 @@
1
- en:
2
- flash:
3
- actions:
4
- create:
5
- success: "Resource created with success"
6
- failure: "Resource could not be created"
7
- with_block:
8
- success: "Resource with block created with success"
9
- with_html:
10
- failure_html: "<strong>OH NOES!</strong> You did it wrong!"
11
- xss_html: "<strong>Yay!</strong> %{xss}"
12
- addresses:
13
- update:
14
- success: "Nice! %{resource_name} was updated with success!"
15
- failure: "Oh no! We could not update your address!"
16
- destroy:
17
- success: "Successfully deleted the address at %{reference}"
18
- notice: "Successfully deleted the chosen address at %{reference}"
19
- with_html:
20
- success_html: "<strong>Yay!</strong> You did it!"
21
- admin:
22
- actions:
23
- create:
24
- notice: "Admin created address with success"
25
- addresses:
26
- update:
27
- notice: "Admin updated address with success"
28
-
@@ -1,82 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CollectionResponder < ActionController::Responder
4
- include Responders::CollectionResponder
5
- end
6
-
7
- class CollectionController < ApplicationController
8
- self.responder = CollectionResponder
9
-
10
- def single
11
- respond_with Address.new
12
- end
13
-
14
- def namespaced
15
- respond_with :admin, Address.new
16
- end
17
-
18
- def nested
19
- respond_with User.new, Address.new
20
- end
21
-
22
- def only_symbols
23
- respond_with :admin, :addresses
24
- end
25
-
26
- def with_location
27
- respond_with Address.new, :location => "given_location"
28
- end
29
-
30
- def isolated_namespace
31
- respond_with MyEngine::Business
32
- end
33
-
34
- def uncountable
35
- respond_with News.new
36
- end
37
- end
38
-
39
- class CollectionResponderTest < ActionController::TestCase
40
- tests CollectionController
41
-
42
- def test_collection_with_single_resource
43
- @controller.expects(:addresses_url).returns("addresses_url")
44
- post :single
45
- assert_redirected_to "addresses_url"
46
- end
47
-
48
- def test_collection_with_namespaced_resource
49
- @controller.expects(:admin_addresses_url).returns("admin_addresses_url")
50
- put :namespaced
51
- assert_redirected_to "admin_addresses_url"
52
- end
53
-
54
- def test_collection_with_nested_resource
55
- @controller.expects(:user_addresses_url).returns("user_addresses_url")
56
- delete :nested
57
- assert_redirected_to "user_addresses_url"
58
- end
59
-
60
- def test_collection_respects_location_option
61
- delete :with_location
62
- assert_redirected_to "given_location"
63
- end
64
-
65
- def test_collection_respects_only_symbols
66
- @controller.expects(:admin_addresses_url).returns("admin_addresses_url")
67
- post :only_symbols
68
- assert_redirected_to "admin_addresses_url"
69
- end
70
-
71
- def test_collection_respects_isolated_namespace
72
- @controller.expects(:businesses_url).returns("businesses_url")
73
- post :isolated_namespace
74
- assert_redirected_to "businesses_url"
75
- end
76
-
77
- def test_collection_respects_uncountable_resource
78
- @controller.expects(:news_index_url).returns("news_index_url")
79
- post :uncountable
80
- assert_redirected_to "news_index_url"
81
- end
82
- end
@@ -1,72 +0,0 @@
1
- require 'test_helper'
2
-
3
- ActionController::Base.extend Responders::ControllerMethod
4
-
5
- module FooResponder
6
- def to_html
7
- @resource << "foo"
8
- super
9
- end
10
- end
11
-
12
- module BarResponder
13
- def to_html
14
- @resource << "bar"
15
- super
16
- end
17
- end
18
-
19
- module PeopleResponder
20
- def to_html
21
- @resource << "baz"
22
- super
23
- end
24
- end
25
-
26
- class PeopleController < ApplicationController
27
- responders :foo, BarResponder
28
-
29
- def index
30
- @array = []
31
- respond_with(@array) do |format|
32
- format.html { render :text => "Success!" }
33
- end
34
- end
35
- end
36
-
37
- class SpecialPeopleController < PeopleController
38
- responders :people
39
- end
40
-
41
- class ControllerMethodTest < ActionController::TestCase
42
- tests PeopleController
43
-
44
- def setup
45
- @controller.stubs(:polymorphic_url).returns("/")
46
- end
47
-
48
- def test_foo_responder_gets_added
49
- get :index
50
- assert assigns(:array).include? "foo"
51
- end
52
-
53
- def test_bar_responder_gets_added
54
- get :index
55
- assert assigns(:array).include? "bar"
56
- end
57
- end
58
-
59
- class ControllerMethodInheritanceTest < ActionController::TestCase
60
- tests SpecialPeopleController
61
-
62
- def setup
63
- @controller.stubs(:polymorphic_url).returns("/")
64
- end
65
-
66
- def test_responder_is_inherited
67
- get :index
68
- assert assigns(:array).include? "foo"
69
- assert assigns(:array).include? "bar"
70
- assert assigns(:array).include? "baz"
71
- end
72
- end
@@ -1,260 +0,0 @@
1
- require 'test_helper'
2
-
3
- class FlashResponder < ActionController::Responder
4
- include Responders::FlashResponder
5
- end
6
-
7
- class AddressesController < ApplicationController
8
- before_filter :set_resource
9
- self.responder = FlashResponder
10
-
11
- respond_to :js, :only => :create
12
-
13
- def action
14
- options = params.slice(:flash, :flash_now)
15
- flash[:success] = "Flash is set" if params[:set_flash]
16
- respond_with(@resource, options)
17
- end
18
- alias :new :action
19
- alias :create :action
20
- alias :update :action
21
- alias :destroy :action
22
-
23
- def with_block
24
- respond_with(@resource) do |format|
25
- format.html { render :text => "Success!" }
26
- end
27
- end
28
-
29
- def another
30
- respond_with(@resource, :notice => "Yes, notice this!", :alert => "Warning, warning!")
31
- end
32
-
33
- def with_html
34
- respond_with(@resource)
35
- end
36
-
37
- def flexible
38
- options = params[:responder_options] || {}
39
- respond_with(@resource, options)
40
- end
41
-
42
- protected
43
-
44
- def interpolation_options
45
- { :reference => 'Ocean Avenue', :xss => '<script>alert(1)</script>' }
46
- end
47
-
48
- def set_resource
49
- @resource = Address.new
50
- @resource.errors[:fail] << "FAIL" if params[:fail]
51
- end
52
- end
53
-
54
- class PolymorphicAddesssController < AddressesController
55
- def create
56
- respond_with(User.new, Address.new)
57
- end
58
- end
59
-
60
- module Admin
61
- class AddressesController < ::AddressesController
62
- end
63
- end
64
-
65
- class FlashResponderTest < ActionController::TestCase
66
- tests AddressesController
67
-
68
- def setup
69
- Responders::FlashResponder.flash_keys = [ :success, :failure ]
70
- @controller.stubs(:polymorphic_url).returns("/")
71
- end
72
-
73
- def test_sets_success_flash_message_on_non_get_requests
74
- post :create
75
- assert_equal "Resource created with success", flash[:success]
76
- end
77
-
78
- def test_sets_failure_flash_message_on_not_get_requests
79
- post :create, :fail => true
80
- assert_equal "Resource could not be created", flash[:failure]
81
- end
82
-
83
- def test_does_not_set_flash_message_on_get_requests
84
- get :new
85
- assert flash.empty?
86
- end
87
-
88
- def test_sets_flash_message_for_the_current_controller
89
- put :update, :fail => true
90
- assert_equal "Oh no! We could not update your address!", flash[:failure]
91
- end
92
-
93
- def test_sets_flash_message_with_resource_name
94
- put :update
95
- assert_equal "Nice! Address was updated with success!", flash[:success]
96
- end
97
-
98
- def test_sets_flash_message_with_interpolation_options
99
- delete :destroy
100
- assert_equal "Successfully deleted the address at Ocean Avenue", flash[:success]
101
- end
102
-
103
- def test_does_not_set_flash_if_flash_false_is_given
104
- post :create, :flash => false
105
- assert flash.empty?
106
- end
107
-
108
- def test_does_not_overwrite_the_flash_if_already_set
109
- post :create, :set_flash => true
110
- assert_equal "Flash is set", flash[:success]
111
- end
112
-
113
- def test_sets_flash_message_even_if_block_is_given
114
- post :with_block
115
- assert_equal "Resource with block created with success", flash[:success]
116
- end
117
-
118
- def test_sets_now_flash_message_on_javascript_requests
119
- post :create, :format => :js
120
- assert_equal "Resource created with success", flash[:success]
121
- assert_flash_now :success
122
- end
123
-
124
- def test_sets_flash_message_can_be_set_to_now
125
- post :create, :flash_now => true
126
- assert_equal "Resource created with success", @controller.flash.now[:success]
127
- assert_flash_now :success
128
- end
129
-
130
- def test_sets_flash_message_can_be_set_to_now_only_on_success
131
- post :create, :flash_now => :on_success
132
- assert_equal "Resource created with success", @controller.flash.now[:success]
133
- end
134
-
135
- def test_sets_flash_message_can_be_set_to_now_only_on_failure
136
- post :create, :flash_now => :on_failure
137
- assert_not_flash_now :success
138
- end
139
-
140
- def test_sets_message_based_on_notice_key_with_custom_keys
141
- post :another
142
- assert_equal "Yes, notice this!", flash[:success]
143
- end
144
-
145
- def test_sets_message_based_on_alert_key_with_custom_keys
146
- post :another, :fail => true
147
- assert_equal "Warning, warning!", flash[:failure]
148
- end
149
-
150
- def test_sets_message_based_on_notice_key
151
- Responders::FlashResponder.flash_keys = [ :notice, :alert ]
152
- post :another
153
- assert_equal "Yes, notice this!", flash[:notice]
154
- end
155
-
156
- def test_sets_message_based_on_alert_key
157
- Responders::FlashResponder.flash_keys = [ :notice, :alert ]
158
- post :another, :fail => true
159
- assert_equal "Warning, warning!", flash[:alert]
160
- end
161
-
162
- def test_sets_html_using_controller_scope
163
- post :with_html
164
- assert_equal "<strong>Yay!</strong> You did it!", flash[:success]
165
- end
166
-
167
- def test_sets_html_using_actions_scope
168
- post :with_html, :fail => true
169
- assert_equal "<strong>OH NOES!</strong> You did it wrong!", flash[:failure]
170
- end
171
-
172
- def test_escapes_html_interpolations
173
- Responders::FlashResponder.flash_keys = [ :xss, :xss ]
174
- post :with_html
175
- assert_equal "<strong>Yay!</strong> &lt;script&gt;alert(1)&lt;/script&gt;", flash[:xss]
176
- end
177
-
178
- def test_sets_flash_now_on_failure_by_default
179
- post :another, :fail => true
180
- assert_flash_now :failure
181
- end
182
-
183
- def test_does_not_set_flash_message_to_now_with_errors_and_redirect
184
- delete :with_html, :fail => true
185
- assert_not_flash_now :failure
186
- assert_equal "<strong>OH NOES!</strong> You did it wrong!", flash[:failure]
187
- end
188
-
189
- def test_never_set_flash_now
190
- post :flexible, :fail => true, :responder_options => { :flash_now => false, :alert => "Warning" }
191
- assert_not_flash_now :failure
192
- end
193
-
194
- # If we have flash.now, it's always marked as used. Rails 4.1 has string keys,
195
- # whereas 3.2 and 4.0 has symbols, so we need to test both.
196
- def assert_flash_now(k)
197
- assert flash.used_keys.include?(k.to_sym) || flash.used_keys.include?(k.to_s),
198
- "Expected #{k} to be in flash.now, but it's not."
199
- end
200
-
201
- def assert_not_flash_now(k)
202
- assert flash[k], "Expected #{k} to be set"
203
- assert !flash.used_keys.include?(k.to_sym),
204
- "Expected #{k} to not be in flash.now, but it is."
205
- end
206
- end
207
-
208
- class NamespacedFlashResponderTest < ActionController::TestCase
209
- tests Admin::AddressesController
210
-
211
- def setup
212
- Responders::FlashResponder.flash_keys = [ :notice, :alert ]
213
- @controller.stubs(:polymorphic_url).returns("/")
214
- end
215
-
216
- def test_sets_the_flash_message_based_on_the_current_controller
217
- put :update
218
- assert_equal "Admin updated address with success", flash[:notice]
219
- end
220
-
221
- def test_sets_the_flash_message_based_on_namespace_actions
222
- Responders::FlashResponder.namespace_lookup = true
223
- post :create
224
- assert_equal "Admin created address with success", flash[:notice]
225
- ensure
226
- Responders::FlashResponder.namespace_lookup = false
227
- end
228
-
229
- def test_fallbacks_to_non_namespaced_controller_flash_message
230
- Responders::FlashResponder.namespace_lookup = true
231
- delete :destroy
232
- assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
233
- ensure
234
- Responders::FlashResponder.namespace_lookup = false
235
- end
236
-
237
- def test_does_not_fallbacks_to_namespaced_actions_if_disabled
238
- post :create
239
- assert_equal "Address was successfully created.", flash[:notice]
240
- end
241
-
242
- def test_does_not_fallbacks_to_non_namespaced_controller_flash_message_if_disabled
243
- post :new
244
- assert_equal nil, flash[:notice]
245
- end
246
- end
247
-
248
- class PolymorhicFlashResponderTest < ActionController::TestCase
249
- tests PolymorphicAddesssController
250
-
251
- def setup
252
- Responders::FlashResponder.flash_keys = [ :notice, :alert ]
253
- @controller.stubs(:polymorphic_url).returns("/")
254
- end
255
-
256
- def test_polymorhic_respond_with
257
- post :create
258
- assert_equal "Address was successfully created.", flash[:notice]
259
- end
260
- end
@@ -1,120 +0,0 @@
1
- require 'test_helper'
2
-
3
- class HttpCacheResponder < ActionController::Responder
4
- include Responders::HttpCacheResponder
5
- end
6
-
7
- class HttpCacheController < ApplicationController
8
- self.responder = HttpCacheResponder
9
-
10
- def single
11
- options = params.slice(:http_cache)
12
- response.last_modified = Time.utc(2008) if params[:last_modified]
13
- respond_with(Address.new(Time.utc(2009)), options)
14
- end
15
-
16
- def nested
17
- respond_with Address.new(Time.utc(2009)), Address.new(Time.utc(2008))
18
- end
19
-
20
- def collection
21
- respond_with [Address.new(Time.utc(2009)), Address.new(Time.utc(2008))]
22
- end
23
-
24
- def not_persisted
25
- model = Address.new(Time.utc(2009))
26
- model.persisted = false
27
- respond_with(model)
28
- end
29
-
30
- def empty
31
- respond_with []
32
- end
33
- end
34
-
35
- class HttpCacheResponderTest < ActionController::TestCase
36
- tests HttpCacheController
37
-
38
- def setup
39
- @request.accept = "application/xml"
40
- @controller.stubs(:polymorphic_url).returns("/")
41
- end
42
-
43
- def test_last_modified_at_is_set_with_single_resource_on_get
44
- get :single
45
- assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
46
- assert_equal "<xml />", @response.body
47
- assert_equal 200, @response.status
48
- end
49
-
50
- def test_returns_not_modified_if_return_is_cache_is_still_valid
51
- @request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
52
- get :single
53
- assert_equal 304, @response.status
54
- assert_includes [" ", ""], @response.body
55
- end
56
-
57
- def test_refreshes_last_modified_if_cache_is_expired
58
- @request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2008, 6).httpdate
59
- get :single
60
- assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
61
- assert_equal "<xml />", @response.body
62
- assert_equal 200, @response.status
63
- end
64
-
65
- def test_does_not_set_cache_unless_get_requests
66
- post :single
67
- assert_nil @response.headers["Last-Modified"]
68
- assert_equal 201, @response.status
69
- end
70
-
71
- def test_does_not_use_cache_unless_get_requests
72
- @request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
73
- post :single
74
- assert_equal 201, @response.status
75
- end
76
-
77
- def test_does_not_set_cache_if_http_cache_is_false
78
- get :single, :http_cache => false
79
- assert_nil @response.headers["Last-Modified"]
80
- assert_equal 200, @response.status
81
- end
82
-
83
- def test_does_not_use_cache_if_http_cache_is_false
84
- @request.env["HTTP_IF_MODIFIED_SINCE"] = Time.utc(2009, 6).httpdate
85
- get :single, :http_cache => false
86
- assert_equal 200, @response.status
87
- end
88
-
89
- def test_does_not_set_cache_for_collection
90
- get :collection
91
- assert_nil @response.headers["Last-Modified"]
92
- assert_equal 200, @response.status
93
- end
94
-
95
- def test_works_for_nested_resources
96
- get :nested
97
- assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
98
- assert_match /xml/, @response.body
99
- assert_equal 200, @response.status
100
- end
101
-
102
- def test_work_with_an_empty_array
103
- get :empty
104
- assert_nil @response.headers["Last-Modified"]
105
- assert_match /xml/, @response.body
106
- assert_equal 200, @response.status
107
- end
108
-
109
- def test_it_does_not_set_body_etag_for_single_resource
110
- get :single
111
- assert_nil @response.headers["ETag"]
112
- end
113
-
114
- def test_does_not_set_cache_for_new_records
115
- get :not_persisted
116
- assert_nil @response.headers["Last-Modified"]
117
- assert_equal "<xml />", @response.body
118
- assert_equal 200, @response.status
119
- end
120
- end
data/test/test_helper.rb DELETED
@@ -1,87 +0,0 @@
1
- require 'bundler/setup'
2
- require 'minitest/autorun'
3
- require 'mocha/setup'
4
-
5
- # Configure Rails
6
- ENV["RAILS_ENV"] = "test"
7
-
8
- require 'active_support'
9
- require 'action_controller'
10
- require 'active_model'
11
- require 'rails/engine'
12
- require 'rails/railtie'
13
-
14
- $:.unshift File.expand_path('../../lib', __FILE__)
15
- require 'responders'
16
-
17
- I18n.enforce_available_locales = true
18
- I18n.load_path << File.expand_path('../locales/en.yml', __FILE__)
19
- I18n.reload!
20
-
21
- Responders::Routes = ActionDispatch::Routing::RouteSet.new
22
- Responders::Routes.draw do
23
- resources :news
24
- get '/admin/:action', :controller => "admin/addresses"
25
- get '/:controller(/:action(/:id))'
26
- end
27
-
28
- class ApplicationController < ActionController::Base
29
- include Responders::Routes.url_helpers
30
-
31
- self.view_paths = File.join(File.dirname(__FILE__), 'views')
32
- respond_to :html, :xml
33
- end
34
-
35
- class ActiveSupport::TestCase
36
- setup do
37
- @routes = Responders::Routes
38
- end
39
- end
40
-
41
- module ActionDispatch
42
- class Flash
43
- class FlashHash
44
- def used_keys
45
- # Rails 3 || Rails 4
46
- @used || @discard
47
- end
48
- end
49
- end
50
- end
51
-
52
- class Model
53
- include ActiveModel::Conversion
54
- include ActiveModel::Validations
55
-
56
- attr_accessor :persisted, :updated_at
57
- alias :persisted? :persisted
58
-
59
- def persisted?
60
- @persisted
61
- end
62
-
63
- def to_xml(*args)
64
- "<xml />"
65
- end
66
-
67
- def initialize(updated_at=nil)
68
- @persisted = true
69
- self.updated_at = updated_at
70
- end
71
- end
72
-
73
- class Address < Model
74
- end
75
-
76
- class User < Model
77
- end
78
-
79
- class News < Model
80
- end
81
-
82
- module MyEngine
83
- class Business < Rails::Engine
84
- isolate_namespace MyEngine
85
- extend ActiveModel::Naming
86
- end
87
- end
@@ -1 +0,0 @@
1
- create.js.erb
@@ -1 +0,0 @@
1
- edit.html.erb
@@ -1 +0,0 @@
1
- new.html.erb
@@ -1 +0,0 @@
1
- new.html.erb
@@ -1 +0,0 @@
1
- Edit world!
@@ -1 +0,0 @@
1
- New world!
@@ -1 +0,0 @@
1
- <content>I should not be displayed</content>
@@ -1 +0,0 @@
1
- <customer-name><%= @customer.name %></customer-name>
@@ -1 +0,0 @@
1
- alert("Hi");
@@ -1 +0,0 @@
1
- Hello world!