responders 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +6 -1
- data/README.rdoc +25 -8
- data/lib/responders/flash_responder.rb +66 -25
- data/lib/responders/version.rb +1 -1
- data/test/flash_responder_test.rb +32 -3
- data/test/test_helper.rb +2 -2
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
== 0.2
|
2
|
+
|
3
|
+
* Added Responders::FlashResponder.flash_keys and default to [ :notice, :alert ]
|
4
|
+
* Added support to respond_with(@resource, :notice => "Yes!", :alert => "No!")
|
5
|
+
|
1
6
|
== 0.1
|
2
7
|
|
3
8
|
* Added FlashResponder
|
4
9
|
* Added HttpCacheResponder
|
5
|
-
* Added responders generators
|
10
|
+
* Added responders generators
|
data/README.rdoc
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
== Responders
|
2
2
|
|
3
|
-
A set of responders to dry up your Rails 3 app:
|
3
|
+
A set of responders modules to dry up your Rails 3 app:
|
4
4
|
|
5
5
|
* FlashResponder - Sets the flash based on the controller action and resource status.
|
6
6
|
For instance, if you do: respond_with(@post) on a POST request and the resource @post
|
7
7
|
does not contain errors, it will automatically set the flash message to
|
8
8
|
"Post was successfully created" as long as you configure your I18n file:
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
flash:
|
11
|
+
actions:
|
12
|
+
create:
|
13
|
+
notice: "{resource_name} was successfully created"
|
14
|
+
update:
|
15
|
+
notice: "{resource_name} was successfully updated"
|
16
|
+
destroy:
|
17
|
+
alert: "{resource_name} could not be destroyed"
|
16
18
|
|
17
19
|
In case the resource contains errors, you should use the failure key on I18n. This is
|
18
20
|
useful to dry up flash messages from your controllers. If you need a specific message
|
@@ -21,12 +23,27 @@ A set of responders to dry up your Rails 3 app:
|
|
21
23
|
flash:
|
22
24
|
posts:
|
23
25
|
create:
|
24
|
-
|
26
|
+
notice: "Your post was created and will be published soon"
|
27
|
+
|
28
|
+
This responder is activated in all non get requests. By default it will use the keys
|
29
|
+
:notice and :alert, but they can be changed as well:
|
30
|
+
|
31
|
+
Responders::FlashResponder.flash_keys = [ :success, :failure ]
|
25
32
|
|
26
33
|
* HttpCacheResponder - Automatically adds Last-Modified headers to API requests. This
|
27
34
|
allows clients to easily query the server if a resource changed and if the client tries
|
28
35
|
to retrieve a resource that has not been modified, it returns not_modified status.
|
29
36
|
|
37
|
+
== Configuring your own responder
|
38
|
+
|
39
|
+
Responders only provides a set of modules, to use them, you have to create your own
|
40
|
+
responder. This can be done in an initializer for example:
|
41
|
+
|
42
|
+
class AppResponder < ActionController::Responder
|
43
|
+
include Responders::FlashResponder
|
44
|
+
include Responders::HttpCacheResponder
|
45
|
+
end
|
46
|
+
|
30
47
|
== Generator
|
31
48
|
|
32
49
|
This gem also includes a responders controller generator, so your scaffold can be customized to use respond_with instead of default respond_to blocks just by configuring your environment:
|
@@ -11,16 +11,17 @@ module Responders
|
|
11
11
|
# flash.cars.create.status
|
12
12
|
# flash.actions.create.status
|
13
13
|
#
|
14
|
-
# The statuses
|
15
|
-
# or destroyed with success)
|
14
|
+
# The statuses by default are :notice (when the object can be created, updated
|
15
|
+
# or destroyed with success) and :alert (when the objecy cannot be created
|
16
16
|
# or updated).
|
17
17
|
#
|
18
|
-
#
|
18
|
+
# On I18n, the resource_name given is available as interpolation option,
|
19
|
+
# this means you can set:
|
19
20
|
#
|
20
21
|
# flash:
|
21
22
|
# actions:
|
22
23
|
# create:
|
23
|
-
#
|
24
|
+
# notice: "Hooray! {{resource_name}} was successfully created!"
|
24
25
|
#
|
25
26
|
# But sometimes, flash messages are not that simple. Going back
|
26
27
|
# to cars example, you might want to say the brand of the car when it's
|
@@ -29,7 +30,7 @@ module Responders
|
|
29
30
|
# flash:
|
30
31
|
# cars:
|
31
32
|
# update:
|
32
|
-
#
|
33
|
+
# notice: "Hooray! You just tuned your {{car_brand}}!"
|
33
34
|
#
|
34
35
|
# Since :car_name is not available for interpolation by default, you have
|
35
36
|
# to overwrite interpolation_options in your controller.
|
@@ -50,43 +51,83 @@ module Responders
|
|
50
51
|
# flash.cars.create.status
|
51
52
|
# flash.actions.create.status
|
52
53
|
#
|
54
|
+
# == Options
|
55
|
+
#
|
56
|
+
# FlashResponder also accepts some options through respond_with API.
|
57
|
+
#
|
58
|
+
# * :flash - When set to false, no flash message is set.
|
59
|
+
#
|
60
|
+
# respond_with(@user, :flash => true)
|
61
|
+
#
|
62
|
+
# * :notice - Supply the message to be set if the record has no errors.
|
63
|
+
# * :alert - Supply the message to be set if the record has errors.
|
64
|
+
#
|
65
|
+
# respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
|
66
|
+
#
|
67
|
+
# == Configure status keys
|
68
|
+
#
|
69
|
+
# As said previously, FlashResponder by default use :notice and :alert
|
70
|
+
# keys. You can change that by setting the status_keys:
|
71
|
+
#
|
72
|
+
# Responders::FlashResponder.flash_keys = [ :success, :failure ]
|
73
|
+
#
|
74
|
+
# However, the options :notice and :alert to respond_with are kept :notice
|
75
|
+
# and :alert.
|
76
|
+
#
|
53
77
|
module FlashResponder
|
78
|
+
mattr_accessor :flash_keys
|
79
|
+
@@flash_keys = [ :notice, :alert ]
|
80
|
+
|
54
81
|
def initialize(controller, resources, options={})
|
55
82
|
super
|
56
|
-
@flash
|
83
|
+
@flash = options.delete(:flash)
|
84
|
+
@notice = options.delete(:notice)
|
85
|
+
@alert = options.delete(:alert)
|
57
86
|
end
|
58
87
|
|
59
|
-
def
|
88
|
+
def to_html
|
60
89
|
super
|
61
90
|
|
62
|
-
unless get? || @flash == false
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
resource_name = if resource.class.respond_to?(:human_name)
|
67
|
-
resource.class.human_name
|
91
|
+
unless !respond_to?(:"to_#{format}") || get? || @flash == false
|
92
|
+
if has_errors?
|
93
|
+
controller.flash[:alert] ||= @alert if @alert
|
94
|
+
status = Responders::FlashResponder.flash_keys.last
|
68
95
|
else
|
69
|
-
|
96
|
+
controller.flash[:notice] ||= @notice if @notice
|
97
|
+
status = Responders::FlashResponder.flash_keys.first
|
70
98
|
end
|
71
99
|
|
72
|
-
|
73
|
-
:default => flash_defaults_by_namespace(status),
|
74
|
-
:resource_name => resource_name,
|
75
|
-
:resource_sym => resource_name.downcase
|
76
|
-
}
|
77
|
-
|
78
|
-
if controller.respond_to?(:interpolation_options, true)
|
79
|
-
options.merge!(controller.send(:interpolation_options))
|
80
|
-
end
|
100
|
+
return if controller.flash[status].present?
|
81
101
|
|
102
|
+
options = mount_i18n_options(status)
|
82
103
|
message = ::I18n.t options[:default].shift, options
|
83
|
-
controller.
|
104
|
+
controller.flash[status] = message unless message.blank?
|
84
105
|
end
|
85
106
|
end
|
86
107
|
|
87
108
|
protected
|
88
109
|
|
89
|
-
def
|
110
|
+
def mount_i18n_options(status) #:nodoc:
|
111
|
+
resource_name = if resource.class.respond_to?(:human_name)
|
112
|
+
resource.class.human_name
|
113
|
+
else
|
114
|
+
resource.class.name.underscore.humanize
|
115
|
+
end
|
116
|
+
|
117
|
+
options = {
|
118
|
+
:default => flash_defaults_by_namespace(status),
|
119
|
+
:resource_name => resource_name,
|
120
|
+
:resource_sym => resource_name.downcase
|
121
|
+
}
|
122
|
+
|
123
|
+
if controller.respond_to?(:interpolation_options, true)
|
124
|
+
options.merge!(controller.send(:interpolation_options))
|
125
|
+
end
|
126
|
+
|
127
|
+
options
|
128
|
+
end
|
129
|
+
|
130
|
+
def flash_defaults_by_namespace(status) #:nodoc:
|
90
131
|
defaults = []
|
91
132
|
slices = controller.controller_path.split('/')
|
92
133
|
|
data/lib/responders/version.rb
CHANGED
@@ -27,6 +27,16 @@ class AddressesController < ApplicationController
|
|
27
27
|
alias :update :action
|
28
28
|
alias :destroy :action
|
29
29
|
|
30
|
+
def with_block
|
31
|
+
respond_with(@resource) do |format|
|
32
|
+
format.html { render :text => "Success!" }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def another
|
37
|
+
respond_with(@resource, :notice => "Yes, notice this!", :alert => "Warning, warning!")
|
38
|
+
end
|
39
|
+
|
30
40
|
protected
|
31
41
|
|
32
42
|
def interpolation_options
|
@@ -48,6 +58,7 @@ class FlashResponderTest < ActionController::TestCase
|
|
48
58
|
tests AddressesController
|
49
59
|
|
50
60
|
def setup
|
61
|
+
Responders::FlashResponder.flash_keys = [ :success, :failure ]
|
51
62
|
@controller.stubs(:polymorphic_url).returns("/")
|
52
63
|
end
|
53
64
|
|
@@ -90,27 +101,45 @@ class FlashResponderTest < ActionController::TestCase
|
|
90
101
|
post :create, :set_flash => true
|
91
102
|
assert_equal "Flash is set", flash[:success]
|
92
103
|
end
|
104
|
+
|
105
|
+
def test_sets_flash_message_even_if_block_is_given
|
106
|
+
post :with_block
|
107
|
+
assert_equal "Resource with block created with success", flash[:success]
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_sets_message_based_on_notice_key
|
111
|
+
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
|
112
|
+
post :another
|
113
|
+
assert_equal "Yes, notice this!", flash[:notice]
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_sets_message_based_on_alert_key
|
117
|
+
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
|
118
|
+
post :another, :fail => true
|
119
|
+
assert_equal "Warning, warning!", flash[:alert]
|
120
|
+
end
|
93
121
|
end
|
94
122
|
|
95
123
|
class NamespacedFlashResponderTest < ActionController::TestCase
|
96
124
|
tests Admin::AddressesController
|
97
125
|
|
98
126
|
def setup
|
127
|
+
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
|
99
128
|
@controller.stubs(:polymorphic_url).returns("/")
|
100
129
|
end
|
101
130
|
|
102
131
|
def test_sets_the_flash_message_based_on_the_current_controller
|
103
132
|
put :update
|
104
|
-
assert_equal "Admin updated address with success", flash[:
|
133
|
+
assert_equal "Admin updated address with success", flash[:notice]
|
105
134
|
end
|
106
135
|
|
107
136
|
def test_sets_the_flash_message_based_on_namespace_actions
|
108
137
|
post :create
|
109
|
-
assert_equal "Admin created address with success", flash[:
|
138
|
+
assert_equal "Admin created address with success", flash[:notice]
|
110
139
|
end
|
111
140
|
|
112
141
|
def test_fallbacks_to_non_namespaced_controller_flash_message
|
113
142
|
delete :destroy
|
114
|
-
assert_equal "Successfully deleted the address at Ocean Avenue", flash[:
|
143
|
+
assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
|
115
144
|
end
|
116
145
|
end
|
data/test/test_helper.rb
CHANGED
@@ -18,10 +18,10 @@ require 'mocha'
|
|
18
18
|
ENV["RAILS_ENV"] = "test"
|
19
19
|
RAILS_ROOT = "anywhere"
|
20
20
|
|
21
|
-
require File.expand_path(File.dirname(__FILE__) + "
|
21
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../rails/vendor/gems/environment")
|
22
22
|
require 'active_support'
|
23
23
|
require 'action_controller'
|
24
|
-
require 'action_controller/
|
24
|
+
require 'action_controller/test_case'
|
25
25
|
|
26
26
|
class ApplicationController < ActionController::Base
|
27
27
|
respond_to :html, :xml
|
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.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-22 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|