responders 0.5.5 → 0.6.0
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.
- data/README.rdoc +10 -2
- data/Rakefile +1 -1
- data/lib/generators/rails/responders_controller_generator.rb +1 -1
- data/lib/responders/controller_method.rb +35 -0
- data/lib/responders/flash_responder.rb +6 -1
- data/lib/responders/version.rb +1 -1
- data/lib/responders.rb +9 -5
- data/test/controller_method_test.rb +72 -0
- data/test/flash_responder_test.rb +8 -0
- data/test/test_helper.rb +8 -3
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -46,7 +46,7 @@ to be used with Rails 2.3 together with Inherited Resources, please check this b
|
|
46
46
|
|
47
47
|
The first step is instal responders gem and configure it in your application:
|
48
48
|
|
49
|
-
|
49
|
+
gem install responders
|
50
50
|
|
51
51
|
Responders only provides a set of modules, to use them, you have to create your own
|
52
52
|
responder. This can be done in an initializer for example:
|
@@ -60,6 +60,14 @@ Or, for your convenience, just do:
|
|
60
60
|
|
61
61
|
rails generate responders_install
|
62
62
|
|
63
|
+
== Controller method
|
64
|
+
|
65
|
+
This gem also includes the controller method 'responders', which allows you to cherry-pick which responders you want included in your controller.
|
66
|
+
|
67
|
+
class InvitationsController < ApplicationController
|
68
|
+
responders :flash, :http_cache
|
69
|
+
end
|
70
|
+
|
63
71
|
== Generator
|
64
72
|
|
65
73
|
This gem also includes a responders controller generator, so your scaffold can be customized
|
@@ -72,4 +80,4 @@ If you discover any bugs or want to drop a line, feel free to create an issue on
|
|
72
80
|
|
73
81
|
http://github.com/plataformatec/responders/issues
|
74
82
|
|
75
|
-
MIT License. Copyright
|
83
|
+
MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ begin
|
|
29
29
|
require 'jeweler'
|
30
30
|
Jeweler::Tasks.new do |s|
|
31
31
|
s.name = "responders"
|
32
|
-
s.version = Responders::VERSION
|
32
|
+
s.version = Responders::VERSION.dup
|
33
33
|
s.summary = "A set of Rails 3 responders to dry up your application"
|
34
34
|
s.email = "contact@plataformatec.com.br"
|
35
35
|
s.homepage = "http://github.com/plataformatec/responders"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Responders
|
2
|
+
module ControllerMethod
|
3
|
+
# Adds the given responders to the current controller's responder, allowing you to cherry-pick
|
4
|
+
# which responders you want per controller.
|
5
|
+
#
|
6
|
+
# class InvitationsController < ApplicationController
|
7
|
+
# responders :flash, :http_cache
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# Takes symbols and strings and translates them to VariableResponder (eg. :flash becomes FlashResponder).
|
11
|
+
# Also allows passing in the responders modules in directly, so you could do:
|
12
|
+
#
|
13
|
+
# responders FlashResponder, HttpCacheResponder
|
14
|
+
#
|
15
|
+
# Or a mix of both methods:
|
16
|
+
#
|
17
|
+
# responders :flash, MyCustomResponder
|
18
|
+
#
|
19
|
+
def responders(*responders)
|
20
|
+
self.responder = responders.inject(Class.new(responder)) do |klass, responder|
|
21
|
+
responder = case responder
|
22
|
+
when Module
|
23
|
+
responder
|
24
|
+
when String, Symbol
|
25
|
+
"#{responder.to_s.classify}Responder".constantize
|
26
|
+
else
|
27
|
+
raise "responder has to be a string, a symbol or a module"
|
28
|
+
end
|
29
|
+
|
30
|
+
klass.send(:include, responder)
|
31
|
+
klass
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -93,6 +93,11 @@ module Responders
|
|
93
93
|
super
|
94
94
|
end
|
95
95
|
|
96
|
+
def to_js
|
97
|
+
set_flash_message! if set_flash_message?
|
98
|
+
to_format
|
99
|
+
end
|
100
|
+
|
96
101
|
protected
|
97
102
|
|
98
103
|
def set_flash_message!
|
@@ -119,7 +124,7 @@ module Responders
|
|
119
124
|
end
|
120
125
|
|
121
126
|
def set_flash_now?
|
122
|
-
(@flash_now == true) || (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success)
|
127
|
+
(@flash_now == true) || (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success) || (format == :js)
|
123
128
|
end
|
124
129
|
|
125
130
|
def set_flash_message? #:nodoc:
|
data/lib/responders/version.rb
CHANGED
data/lib/responders.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
|
+
require 'action_controller/base'
|
2
|
+
|
1
3
|
module Responders
|
2
4
|
autoload :FlashResponder, 'responders/flash_responder'
|
3
5
|
autoload :HttpCacheResponder, 'responders/http_cache_responder'
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
+
require 'responders/controller_method'
|
8
|
+
ActionController::Base.extend Responders::ControllerMethod
|
7
9
|
|
10
|
+
class Railtie < ::Rails::Railtie
|
11
|
+
config.responders = ActiveSupport::OrderedOptions.new
|
8
12
|
config.generators.scaffold_controller = :responders_controller
|
9
13
|
|
10
14
|
# Add load paths straight to I18n, so engines and application can overwrite it.
|
11
15
|
require 'active_support/i18n'
|
12
16
|
I18n.load_path << File.expand_path('../responders/locales/en.yml', __FILE__)
|
13
17
|
|
14
|
-
initializer "responders.flash_responder" do
|
15
|
-
if config.responders.flash_keys
|
16
|
-
Responders::FlashResponder.flash_keys = config.responders.flash_keys
|
18
|
+
initializer "responders.flash_responder" do |app|
|
19
|
+
if app.config.responders.flash_keys
|
20
|
+
Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/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 BazResponder
|
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 :baz
|
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
|
@@ -16,6 +16,7 @@ end
|
|
16
16
|
class AddressesController < ApplicationController
|
17
17
|
before_filter :set_resource
|
18
18
|
self.responder = FlashResponder
|
19
|
+
respond_to :js, :only => :create
|
19
20
|
|
20
21
|
def action
|
21
22
|
options = params.slice(:flash, :flash_now)
|
@@ -107,6 +108,13 @@ class FlashResponderTest < ActionController::TestCase
|
|
107
108
|
assert_equal "Resource with block created with success", flash[:success]
|
108
109
|
end
|
109
110
|
|
111
|
+
def test_sets_now_flash_message_on_javascript_requests
|
112
|
+
@now = {}
|
113
|
+
@controller.flash.expects(:now).returns(@now)
|
114
|
+
post :create, :format => :js
|
115
|
+
assert_equal "Resource created with success", @now[:success]
|
116
|
+
end
|
117
|
+
|
110
118
|
def test_sets_flash_message_can_be_set_to_now
|
111
119
|
@now = {}
|
112
120
|
@controller.flash.expects(:now).returns(@now)
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
+
gem 'activesupport', '3.0.0.beta2'
|
4
|
+
gem 'activemodel', '3.0.0.beta2'
|
5
|
+
gem 'actionpack', '3.0.0.beta2'
|
6
|
+
gem 'railties', '3.0.0.beta2'
|
7
|
+
|
3
8
|
begin
|
4
9
|
gem "test-unit"
|
5
10
|
rescue LoadError
|
@@ -33,8 +38,8 @@ I18n.reload!
|
|
33
38
|
|
34
39
|
ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
|
35
40
|
|
36
|
-
Responders::
|
37
|
-
Responders::
|
41
|
+
Responders::Routes = ActionDispatch::Routing::RouteSet.new
|
42
|
+
Responders::Routes.draw do |map|
|
38
43
|
map.connect 'admin/:action', :controller => "admin/addresses"
|
39
44
|
map.connect ':controller/:action/:id'
|
40
45
|
map.connect ':controller/:action'
|
@@ -42,7 +47,7 @@ end
|
|
42
47
|
|
43
48
|
class ActiveSupport::TestCase
|
44
49
|
setup do
|
45
|
-
@
|
50
|
+
@routes = Responders::Routes
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 6
|
8
|
+
- 0
|
9
|
+
version: 0.6.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Jos\xC3\xA9 Valim"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03
|
17
|
+
date: 2010-04-03 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/generators/rails/templates/controller.rb
|
37
37
|
- lib/generators/responders_install_generator.rb
|
38
38
|
- lib/responders.rb
|
39
|
+
- lib/responders/controller_method.rb
|
39
40
|
- lib/responders/flash_responder.rb
|
40
41
|
- lib/responders/http_cache_responder.rb
|
41
42
|
- lib/responders/locales/en.yml
|
@@ -71,6 +72,7 @@ signing_key:
|
|
71
72
|
specification_version: 3
|
72
73
|
summary: A set of Rails 3 responders to dry up your application
|
73
74
|
test_files:
|
75
|
+
- test/controller_method_test.rb
|
74
76
|
- test/flash_responder_test.rb
|
75
77
|
- test/http_cache_responder_test.rb
|
76
78
|
- test/test_helper.rb
|