responders 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - ree
5
+ - jruby-18mode
6
+ gemfile:
7
+ - gemfiles/Gemfile-rails.3.1.x
8
+ - Gemfile
9
+ notifications:
10
+ recipients:
11
+ - jose.valim@plataformatec.com.br
12
+ - carlos@plataformatec.com.br
13
+ - rafael.franca@plataformatec.com.br
@@ -0,0 +1,37 @@
1
+ ## 0.9
2
+
3
+ * Disable namespace lookup by default
4
+
5
+ ## 0.8
6
+
7
+ * Allow embedded HTML in flash messages
8
+
9
+ ## 0.7
10
+
11
+ * Support Rails 3.1 onward
12
+ * Support namespaced engines
13
+
14
+ ## 0.6
15
+
16
+ * Allow engine detection in generators
17
+ * HTTP Cache is no longer triggered for collections
18
+ * `:js` now sets the `flash.now` by default, instead of `flash`
19
+ * Renamed `responders_install` generator to `responders:install`
20
+ * Added `CollectionResponder` which allows you to always redirect to the collection path
21
+ (index action) after POST/PUT/DELETE
22
+
23
+ ## 0.5
24
+
25
+ * Added Railtie and better Rails 3 support
26
+ * Added `:flash_now` as option
27
+
28
+ ## 0.4
29
+
30
+ * Added `Responders::FlashResponder.flash_keys` and default to `[ :notice, :alert ]`
31
+ * Added support to `respond_with(@resource, :notice => "Yes!", :alert => "No!")``
32
+
33
+ ## 0.1
34
+
35
+ * Added `FlashResponder`
36
+ * Added `HttpCacheResponder`
37
+ * Added responders generators
data/Gemfile CHANGED
@@ -1,7 +1,7 @@
1
- source "http://rubygems.org"
1
+ source :rubygems
2
2
 
3
3
  gemspec
4
4
 
5
- gem "mocha"
5
+ gem 'mocha'
6
6
 
7
- gem "ruby-debug", :platform => :mri_18
7
+ gem 'ruby-debug', :platform => :mri_18
@@ -0,0 +1,135 @@
1
+ # Responders
2
+
3
+ A set of responders modules to dry up your Rails 3 app. If you are interested in the version
4
+ to be used with Rails 2.3 together with Inherited Resources, please check this branch instead:
5
+
6
+ http://github.com/plataformatec/responders/tree/v0.4
7
+
8
+ ## Responders Types
9
+
10
+ ### FlashResponder
11
+
12
+ Sets the flash based on the controller action and resource status.
13
+ For instance, if you do: `respond_with(@post)` on a POST request and the resource `@post`
14
+ does not contain errors, it will automatically set the flash message to
15
+ `"Post was successfully created"` as long as you configure your I18n file:
16
+
17
+ ```yaml
18
+ flash:
19
+ actions:
20
+ create:
21
+ notice: "%{resource_name} was successfully created."
22
+ update:
23
+ notice: "%{resource_name} was successfully updated."
24
+ destroy:
25
+ notice: "%{resource_name} was successfully destroyed."
26
+ alert: "%{resource_name} could not be destroyed."
27
+ ```
28
+
29
+ In case the resource contains errors, you should use the failure key on I18n. This is
30
+ useful to dry up flash messages from your controllers. If you need a specific message
31
+ for a controller, let's say, for `PostsController`, you can also do:
32
+
33
+ ```yaml
34
+ flash:
35
+ posts:
36
+ create:
37
+ notice: "Your post was created and will be published soon"
38
+ ```
39
+
40
+ This responder is activated in all non get requests. By default it will use the keys
41
+ `:notice` and `:alert`, but they can be changed in your application:
42
+
43
+ ```ruby
44
+ config.responders.flash_keys = [ :success, :failure ]
45
+ ```
46
+
47
+ You can also have embedded HTML. Just create a `_html` scope.
48
+
49
+ ```yaml
50
+ flash:
51
+ actions:
52
+ create:
53
+ alert_html: "<strong>OH NOES!</strong> You did it wrong!"
54
+ posts:
55
+ create:
56
+ notice_html: "<strong>Yay!</strong> You did it!"
57
+ ```
58
+
59
+ ### HttpCacheResponder
60
+
61
+ Automatically adds Last-Modified headers to API requests. This
62
+ allows clients to easily query the server if a resource changed and if the client tries
63
+ to retrieve a resource that has not been modified, it returns not_modified status.
64
+
65
+ ### CollectionResponder
66
+
67
+ Makes your create and update action redirect to the collection on success.
68
+
69
+ ## Configuring your own responder
70
+
71
+ The first step is instal responders gem and configure it in your application:
72
+
73
+ ```console
74
+ gem install responders
75
+ ```
76
+
77
+ In your Gemfile, add this line:
78
+
79
+ ```ruby
80
+ gem 'responders'
81
+ ```
82
+
83
+ Responders only provides a set of modules, to use them, you have to create your own
84
+ responder. This can be done inside the lib folder for example:
85
+
86
+ ```ruby
87
+ # lib/app_responder.rb
88
+ class AppResponder < ActionController::Responder
89
+ include Responders::FlashResponder
90
+ include Responders::HttpCacheResponder
91
+ end
92
+ ```
93
+
94
+ And then you need to configure your application to use it:
95
+
96
+ ```ruby
97
+ # app/controllers/application_controller.rb
98
+ require "app_responder"
99
+
100
+ class ApplicationController < ActionController::Base
101
+ self.responder = AppResponder
102
+ respond_to :html
103
+ end
104
+ ```
105
+
106
+ Or, for your convenience, just do:
107
+
108
+ ```console
109
+ rails generate responders:install
110
+ ```
111
+
112
+ ## Controller method
113
+
114
+ This gem also includes the controller method `responders`, which allows you to cherry-pick which
115
+ responders you want included in your controller.
116
+
117
+ ```ruby
118
+ class InvitationsController < ApplicationController
119
+ responders :flash, :http_cache
120
+ end
121
+ ```
122
+
123
+ ## Generator
124
+
125
+ This gem also includes a responders controller generator, so your scaffold can be customized
126
+ to use `respond_with` instead of default `respond_to` blocks. Installing this gem automatically
127
+ sets the generator.
128
+
129
+ ## Bugs and Feedback
130
+
131
+ If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
132
+
133
+ http://github.com/plataformatec/responders/issues
134
+
135
+ MIT License. Copyright 2012 Plataforma Tecnologia. http://blog.plataformatec.com.br
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'responders', :path => '..'
4
+
5
+ gem 'railties', '~> 3.1.0'
6
+ gem 'mocha'
@@ -9,6 +9,9 @@ module Responders
9
9
 
10
10
  class Railtie < ::Rails::Railtie
11
11
  config.responders = ActiveSupport::OrderedOptions.new
12
+ config.responders.flash_keys = [ :notice, :alert ]
13
+ config.responders.namespace_lookup = false
14
+
12
15
  if config.respond_to?(:app_generators)
13
16
  config.app_generators.scaffold_controller = :responders_controller
14
17
  else
@@ -20,9 +23,8 @@ module Responders
20
23
  I18n.load_path << File.expand_path('../responders/locales/en.yml', __FILE__)
21
24
 
22
25
  initializer "responders.flash_responder" do |app|
23
- if app.config.responders.flash_keys
24
- Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
25
- end
26
+ Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
27
+ Responders::FlashResponder.namespace_lookup = app.config.responders.namespace_lookup
26
28
  end
27
29
  end
28
30
  end
@@ -83,18 +83,20 @@ module Responders
83
83
  # and :alert.
84
84
  #
85
85
  module FlashResponder
86
- mattr_accessor :flash_keys
87
- @@flash_keys = [ :notice, :alert ]
86
+ class << self
87
+ attr_accessor :flash_keys, :namespace_lookup, :helper
88
+ end
88
89
 
89
- mattr_reader :helper
90
- @@helper = Object.new.extend(ActionView::Helpers::TranslationHelper)
90
+ self.flash_keys = [ :notice, :alert ]
91
+ self.namespace_lookup = false
92
+ self.helper = Object.new.extend(ActionView::Helpers::TranslationHelper)
91
93
 
92
94
  def initialize(controller, resources, options={})
93
95
  super
94
96
  @flash = options.delete(:flash)
95
97
  @notice = options.delete(:notice)
96
98
  @alert = options.delete(:alert)
97
- @flash_now = options.delete(:flash_now)
99
+ @flash_now = options.delete(:flash_now) { :on_failure }
98
100
  end
99
101
 
100
102
  def to_html
@@ -163,10 +165,11 @@ module Responders
163
165
  def flash_defaults_by_namespace(status) #:nodoc:
164
166
  defaults = []
165
167
  slices = controller.controller_path.split('/')
168
+ lookup = Responders::FlashResponder.namespace_lookup
166
169
 
167
- while slices.size > 0
170
+ begin
168
171
  controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join('.')}.#{controller.action_name}.#{status}"
169
- actions_scope = :"flash.#{slices.fill(:actions, -1).join('.')}.#{controller.action_name}.#{status}"
172
+ actions_scope = :"flash.#{slices.fill('actions', -1).join('.')}.#{controller.action_name}.#{status}"
170
173
 
171
174
  defaults << :"#{controller_scope}_html"
172
175
  defaults << controller_scope
@@ -175,9 +178,9 @@ module Responders
175
178
  defaults << actions_scope
176
179
 
177
180
  slices.shift
178
- end
181
+ end while slices.size > 0 && lookup
179
182
 
180
183
  defaults << ""
181
184
  end
182
185
  end
183
- end
186
+ end
@@ -1,3 +1,3 @@
1
1
  module Responders
2
- VERSION = "0.8.0".freeze
2
+ VERSION = "0.9.0".freeze
3
3
  end
@@ -34,6 +34,11 @@ class AddressesController < ApplicationController
34
34
  respond_with(@resource)
35
35
  end
36
36
 
37
+ def flexible
38
+ options = params[:responder_options] || {}
39
+ respond_with(@resource, options)
40
+ end
41
+
37
42
  protected
38
43
 
39
44
  def interpolation_options
@@ -154,6 +159,16 @@ class FlashResponderTest < ActionController::TestCase
154
159
  assert_equal "<strong>Yay!</strong> &lt;script&gt;alert(1)&lt;/script&gt;", flash[:xss]
155
160
  end
156
161
 
162
+ def test_sets_flash_now_on_failure_by_default
163
+ post :another, :fail => true
164
+ assert_flash_now :alert
165
+ end
166
+
167
+ def test_never_set_flash_now
168
+ post :flexible, :fail => true, :responder_options => { :flash_now => false, :alert => "Warning" }
169
+ assert_not_flash_now :alert
170
+ end
171
+
157
172
  # If we have flash.now, it's always marked as used.
158
173
  def assert_flash_now(k)
159
174
  assert flash.instance_variable_get(:@used).to_a.include?(k.to_sym),
@@ -161,7 +176,8 @@ class FlashResponderTest < ActionController::TestCase
161
176
  end
162
177
 
163
178
  def assert_not_flash_now(k)
164
- assert !flash.instance_variable_get(:@used).to_a.include?(k.to_sym),
179
+ assert flash[k], "Expected #{k} to be set"
180
+ assert !flash.instance_variable_get(:@used).include?(k.to_sym),
165
181
  "Expected #{k} to not be in flash.now, but it is."
166
182
  end
167
183
  end
@@ -185,7 +201,15 @@ class NamespacedFlashResponderTest < ActionController::TestCase
185
201
  end
186
202
 
187
203
  def test_fallbacks_to_non_namespaced_controller_flash_message
204
+ Responders::FlashResponder.namespace_lookup = true
188
205
  delete :destroy
189
206
  assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
207
+ ensure
208
+ Responders::FlashResponder.namespace_lookup = false
209
+ end
210
+
211
+ def test_does_not_fallbacks_to_non_namespaced_controller_flash_message_if_disabled
212
+ delete :destroy
213
+ assert_equal nil, flash[:notice]
190
214
  end
191
- end
215
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responders
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-06 00:00:00.000000000 Z
12
+ date: 2012-03-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &70303791369100 !ruby/object:Gem::Requirement
16
+ requirement: &2156123020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,19 +21,21 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70303791369100
24
+ version_requirements: *2156123020
25
25
  description: A set of Rails 3 responders to dry up your application
26
26
  email: contact@plataformatec.com.br
27
27
  executables: []
28
28
  extensions: []
29
29
  extra_rdoc_files: []
30
30
  files:
31
- - CHANGELOG.rdoc
31
+ - .travis.yml
32
+ - CHANGELOG.md
32
33
  - Gemfile
33
34
  - Gemfile.lock
34
35
  - MIT-LICENSE
35
- - README.rdoc
36
+ - README.md
36
37
  - Rakefile
38
+ - gemfiles/Gemfile-rails.3.1.x
37
39
  - lib/generators/rails/USAGE
38
40
  - lib/generators/rails/responders_controller_generator.rb
39
41
  - lib/generators/rails/templates/controller.rb
@@ -67,21 +69,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
69
  - - ! '>='
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
- segments:
71
- - 0
72
- hash: -73151246775261150
73
72
  required_rubygems_version: !ruby/object:Gem::Requirement
74
73
  none: false
75
74
  requirements:
76
75
  - - ! '>='
77
76
  - !ruby/object:Gem::Version
78
77
  version: '0'
79
- segments:
80
- - 0
81
- hash: -73151246775261150
82
78
  requirements: []
83
79
  rubyforge_project: responders
84
- rubygems_version: 1.8.11
80
+ rubygems_version: 1.8.15
85
81
  signing_key:
86
82
  specification_version: 3
87
83
  summary: A set of Rails 3 responders to dry up your application
@@ -1,32 +0,0 @@
1
- == 0.8
2
-
3
- * Allow embedded HTML in flash messages
4
-
5
- == 0.7
6
-
7
- * Support Rails 3.1 onward
8
- * Support namespaced engines
9
-
10
- == 0.6
11
-
12
- * Allow engine detection in generators
13
- * HTTP Cache is no longer triggered for collections
14
- * :js now sets the flash.now by default, instead of flash
15
- * Renamed responders_install generator to responders:install
16
- * Added CollectionResponder which allows you to always redirect to the collection path (index action) after POST/PUT/DELETE
17
-
18
- == 0.5
19
-
20
- * Added Railtie and better Rails 3 support
21
- * Added :flash_now as option
22
-
23
- == 0.4
24
-
25
- * Added Responders::FlashResponder.flash_keys and default to [ :notice, :alert ]
26
- * Added support to respond_with(@resource, :notice => "Yes!", :alert => "No!")
27
-
28
- == 0.1
29
-
30
- * Added FlashResponder
31
- * Added HttpCacheResponder
32
- * Added responders generators
@@ -1,110 +0,0 @@
1
- == Responders
2
-
3
- A set of responders modules to dry up your Rails 3 app. If you are interested in the version
4
- to be used with Rails 2.3 together with Inherited Resources, please check this branch instead:
5
-
6
- http://github.com/plataformatec/responders/tree/v0.4
7
-
8
- * FlashResponder - Sets the flash based on the controller action and resource status.
9
- For instance, if you do: respond_with(@post) on a POST request and the resource @post
10
- does not contain errors, it will automatically set the flash message to
11
- "Post was successfully created" as long as you configure your I18n file:
12
-
13
- flash:
14
- actions:
15
- create:
16
- notice: "%{resource_name} was successfully created."
17
- update:
18
- notice: "%{resource_name} was successfully updated."
19
- destroy:
20
- notice: "%{resource_name} was successfully destroyed."
21
- alert: "%{resource_name} could not be destroyed."
22
-
23
- In case the resource contains errors, you should use the failure key on I18n. This is
24
- useful to dry up flash messages from your controllers. If you need a specific message
25
- for a controller, let's say, for PostsController, you can also do:
26
-
27
- flash:
28
- posts:
29
- create:
30
- notice: "Your post was created and will be published soon"
31
-
32
- This responder is activated in all non get requests. By default it will use the keys
33
- :notice and :alert, but they can be changed in your application:
34
-
35
- config.responders.flash_keys = [ :success, :failure ]
36
-
37
- Or:
38
-
39
- Responders::FlashResponder.flash_keys = [ :success, :failure ]
40
-
41
- You can also have embedded HTML. Just create a <tt>_html</tt> scope.
42
-
43
- flash:
44
- actions:
45
- create:
46
- alert_html: "<strong>OH NOES!</strong> You did it wrong!"
47
- posts:
48
- create:
49
- notice_html: "<strong>Yay!</strong> You did it!"
50
-
51
- * HttpCacheResponder - Automatically adds Last-Modified headers to API requests. This
52
- allows clients to easily query the server if a resource changed and if the client tries
53
- to retrieve a resource that has not been modified, it returns not_modified status.
54
-
55
- * CollectionResponder - Makes your create and update action redirect to the collection on success.
56
-
57
- == Configuring your own responder
58
-
59
- The first step is instal responders gem and configure it in your application:
60
-
61
- gem install responders
62
-
63
- In your Gemfile, add this line:
64
-
65
- gem "responders"
66
-
67
- Responders only provides a set of modules, to use them, you have to create your own
68
- responder. This can be done inside the lib folder for example:
69
-
70
- # lib/app_responder.rb
71
- class AppResponder < ActionController::Responder
72
- include Responders::FlashResponder
73
- include Responders::HttpCacheResponder
74
- end
75
-
76
- And then you need to configure your application to use it:
77
-
78
- # app/controllers/application_controller.rb
79
- require "app_responder"
80
-
81
- class ApplicationController < ActionController::Base
82
- self.responder = AppResponder
83
- respond_to :html
84
- end
85
-
86
- Or, for your convenience, just do:
87
-
88
- rails generate responders:install
89
-
90
- == Controller method
91
-
92
- This gem also includes the controller method 'responders', which allows you to cherry-pick which responders you want included in your controller.
93
-
94
- class InvitationsController < ApplicationController
95
- responders :flash, :http_cache
96
- end
97
-
98
- == Generator
99
-
100
- This gem also includes a responders controller generator, so your scaffold can be customized
101
- to use respond_with instead of default respond_to blocks. Installing this gem automatically
102
- sets the generator.
103
-
104
- == Bugs and Feedback
105
-
106
- If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
107
-
108
- http://github.com/plataformatec/responders/issues
109
-
110
- MIT License. Copyright 2012 Plataforma Tecnologia. http://blog.plataformatec.com.br