responders 0.4.8 → 0.5.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/CHANGELOG.rdoc +4 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +34 -10
- data/Rakefile +1 -1
- data/lib/generators/rails/USAGE +11 -0
- data/lib/generators/rails/responders_controller_generator.rb +17 -0
- data/lib/generators/rails/templates/controller.rb +53 -0
- data/lib/generators/responders_install_generator.rb +23 -0
- data/lib/responders.rb +16 -0
- data/lib/responders/flash_responder.rb +9 -28
- data/lib/responders/http_cache_responder.rb +1 -2
- data/lib/responders/locales/en.yml +10 -0
- data/lib/responders/version.rb +1 -1
- data/test/flash_responder_test.rb +1 -28
- data/test/http_cache_responder_test.rb +7 -2
- data/test/test_helper.rb +3 -2
- metadata +12 -22
data/CHANGELOG.rdoc
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright
|
1
|
+
Copyright 2010 Plataforma Tecnologia. http://blog.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.rdoc
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
== Responders
|
2
2
|
|
3
|
-
A set of responders modules to dry up your Rails
|
4
|
-
to be used with Rails 2.3 with Inherited Resources
|
5
|
-
|
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
|
6
7
|
|
7
8
|
* FlashResponder - Sets the flash based on the controller action and resource status.
|
8
9
|
For instance, if you do: respond_with(@post) on a POST request and the resource @post
|
@@ -12,12 +13,11 @@ version.
|
|
12
13
|
flash:
|
13
14
|
actions:
|
14
15
|
create:
|
15
|
-
notice: "{
|
16
|
+
notice: "{resource_name} was successfully created"
|
16
17
|
update:
|
17
|
-
notice: "{
|
18
|
+
notice: "{resource_name} was successfully updated"
|
18
19
|
destroy:
|
19
|
-
|
20
|
-
alert: "{{resource_name}} could not be destroyed."
|
20
|
+
alert: "{resource_name} could not be destroyed"
|
21
21
|
|
22
22
|
In case the resource contains errors, you should use the failure key on I18n. This is
|
23
23
|
useful to dry up flash messages from your controllers. If you need a specific message
|
@@ -29,7 +29,11 @@ version.
|
|
29
29
|
notice: "Your post was created and will be published soon"
|
30
30
|
|
31
31
|
This responder is activated in all non get requests. By default it will use the keys
|
32
|
-
:notice and :alert, but they can be changed
|
32
|
+
:notice and :alert, but they can be changed in your application:
|
33
|
+
|
34
|
+
config.responders.flash_keys = [ :success, :failure ]
|
35
|
+
|
36
|
+
Or:
|
33
37
|
|
34
38
|
Responders::FlashResponder.flash_keys = [ :success, :failure ]
|
35
39
|
|
@@ -37,9 +41,29 @@ version.
|
|
37
41
|
allows clients to easily query the server if a resource changed and if the client tries
|
38
42
|
to retrieve a resource that has not been modified, it returns not_modified status.
|
39
43
|
|
40
|
-
==
|
44
|
+
== Configuring your own responder
|
45
|
+
|
46
|
+
The first step is instal responders gem and configure it in your application:
|
47
|
+
|
48
|
+
sudo gem install responders
|
49
|
+
|
50
|
+
Responders only provides a set of modules, to use them, you have to create your own
|
51
|
+
responder. This can be done in an initializer for example:
|
52
|
+
|
53
|
+
class AppResponder < ActionController::Responder
|
54
|
+
include Responders::FlashResponder
|
55
|
+
include Responders::HttpCacheResponder
|
56
|
+
end
|
57
|
+
|
58
|
+
Or, for your convenience, just do:
|
59
|
+
|
60
|
+
rails generate responders_install
|
61
|
+
|
62
|
+
== Generator
|
41
63
|
|
42
|
-
|
64
|
+
This gem also includes a responders controller generator, so your scaffold can be customized
|
65
|
+
to use respond_with instead of default respond_to blocks. Installing this gem automatically
|
66
|
+
sets the generator.
|
43
67
|
|
44
68
|
== Bugs and Feedback
|
45
69
|
|
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
|
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,11 @@
|
|
1
|
+
Description:
|
2
|
+
Stubs out a scaffolded controller and its views. Different from rails
|
3
|
+
scaffold_controller, it uses respond_with instead of respond_to blocks.
|
4
|
+
Pass the model name, either CamelCased or under_scored. The controller
|
5
|
+
name is retrieved as a pluralized version of the model name.
|
6
|
+
|
7
|
+
To create a controller within a module, specify the model name as a
|
8
|
+
path like 'parent_module/controller_name'.
|
9
|
+
|
10
|
+
This generates a controller class in app/controllers and invokes helper,
|
11
|
+
template engine and test framework generators.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'generators/rails/scaffold_controller/scaffold_controller_generator'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class RespondersControllerGenerator < ScaffoldControllerGenerator
|
6
|
+
def self.source_root
|
7
|
+
@_source_root ||= File.expand_path("templates", File.dirname(__FILE__))
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def flash?
|
13
|
+
!ApplicationController.responder.ancestors.include?(Responders::FlashResponder)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
2
|
+
<% unless options[:singleton] -%>
|
3
|
+
# GET /<%= table_name %>
|
4
|
+
# GET /<%= table_name %>.xml
|
5
|
+
def index
|
6
|
+
@<%= table_name %> = <%= orm_class.all(class_name) %>
|
7
|
+
respond_with(@<%= table_name %>)
|
8
|
+
end
|
9
|
+
<% end -%>
|
10
|
+
|
11
|
+
# GET /<%= table_name %>/1
|
12
|
+
# GET /<%= table_name %>/1.xml
|
13
|
+
def show
|
14
|
+
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
15
|
+
respond_with(@<%= file_name %>)
|
16
|
+
end
|
17
|
+
|
18
|
+
# GET /<%= table_name %>/new
|
19
|
+
# GET /<%= table_name %>/new.xml
|
20
|
+
def new
|
21
|
+
@<%= file_name %> = <%= orm_class.build(class_name) %>
|
22
|
+
respond_with(@<%= file_name %>)
|
23
|
+
end
|
24
|
+
|
25
|
+
# GET /<%= table_name %>/1/edit
|
26
|
+
def edit
|
27
|
+
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
28
|
+
end
|
29
|
+
|
30
|
+
# POST /<%= table_name %>
|
31
|
+
# POST /<%= table_name %>.xml
|
32
|
+
def create
|
33
|
+
@<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %>
|
34
|
+
<%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
|
35
|
+
respond_with(@<%= file_name %>)
|
36
|
+
end
|
37
|
+
|
38
|
+
# PUT /<%= table_name %>/1
|
39
|
+
# PUT /<%= table_name %>/1.xml
|
40
|
+
def update
|
41
|
+
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
42
|
+
<%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance.update_attributes("params[:#{file_name}]") %>
|
43
|
+
respond_with(@<%= file_name %>)
|
44
|
+
end
|
45
|
+
|
46
|
+
# DELETE /<%= table_name %>/1
|
47
|
+
# DELETE /<%= table_name %>/1.xml
|
48
|
+
def destroy
|
49
|
+
@<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
50
|
+
@<%= orm_instance.destroy %>
|
51
|
+
respond_with(@<%= file_name %>)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class RespondersInstallGenerator < Rails::Generators::Base
|
2
|
+
def self.source_root
|
3
|
+
@_source_root ||= File.expand_path("..", __FILE__)
|
4
|
+
end
|
5
|
+
|
6
|
+
desc "Creates an initializer with default responder configuration and copy locale file"
|
7
|
+
|
8
|
+
def create_responder_initializer
|
9
|
+
create_file "config/initializers/responders.rb", <<-FILE
|
10
|
+
class ApplicationResponder
|
11
|
+
include Responders::FlashResponder
|
12
|
+
include Responders::HttpCacheResponder
|
13
|
+
end
|
14
|
+
|
15
|
+
ApplicationController.respond_to :html
|
16
|
+
ApplicationController.responder = ApplicationResponder
|
17
|
+
FILE
|
18
|
+
end
|
19
|
+
|
20
|
+
def copy_locale
|
21
|
+
copy_file "../responders/locales/en.yml", "config/locales/responders.en.yml"
|
22
|
+
end
|
23
|
+
end
|
data/lib/responders.rb
CHANGED
@@ -1,4 +1,20 @@
|
|
1
1
|
module Responders
|
2
2
|
autoload :FlashResponder, 'responders/flash_responder'
|
3
3
|
autoload :HttpCacheResponder, 'responders/http_cache_responder'
|
4
|
+
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
railtie_name :responders
|
7
|
+
|
8
|
+
config.generators.scaffold_controller = :responders_controller
|
9
|
+
|
10
|
+
# Add load paths straight to I18n, so engines and application can overwrite it.
|
11
|
+
require 'active_support/i18n'
|
12
|
+
I18n.load_path << File.expand_path('../responders/locales/en.yml', __FILE__)
|
13
|
+
|
14
|
+
initializer "responders.flash_responder" do
|
15
|
+
if config.responders.flash_keys
|
16
|
+
Responders::FlashResponder.flash_keys = config.responders.flash_keys
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
4
20
|
end
|
@@ -64,8 +64,6 @@ module Responders
|
|
64
64
|
#
|
65
65
|
# respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
|
66
66
|
#
|
67
|
-
# * :flash_now - Sets the flash message using flash.now. Accepts true, :on_failure or :on_sucess.
|
68
|
-
#
|
69
67
|
# == Configure status keys
|
70
68
|
#
|
71
69
|
# As said previously, FlashResponder by default use :notice and :alert
|
@@ -82,30 +80,24 @@ module Responders
|
|
82
80
|
|
83
81
|
def initialize(controller, resources, options={})
|
84
82
|
super
|
85
|
-
@flash
|
86
|
-
@notice
|
87
|
-
@alert
|
88
|
-
@flash_now = options.delete(:flash_now)
|
83
|
+
@flash = options.delete(:flash)
|
84
|
+
@notice = options.delete(:notice)
|
85
|
+
@alert = options.delete(:alert)
|
89
86
|
end
|
90
87
|
|
91
88
|
def to_html
|
92
|
-
set_flash_message! if set_flash_message?
|
93
89
|
super
|
94
|
-
end
|
95
|
-
|
96
|
-
def to_js
|
97
90
|
set_flash_message! if set_flash_message?
|
98
|
-
to_format
|
99
91
|
end
|
100
92
|
|
101
93
|
protected
|
102
94
|
|
103
95
|
def set_flash_message!
|
104
96
|
if has_errors?
|
105
|
-
|
97
|
+
controller.flash[:alert] ||= @alert if @alert
|
106
98
|
status = Responders::FlashResponder.flash_keys.last
|
107
99
|
else
|
108
|
-
|
100
|
+
controller.flash[:notice] ||= @notice if @notice
|
109
101
|
status = Responders::FlashResponder.flash_keys.first
|
110
102
|
end
|
111
103
|
|
@@ -113,18 +105,7 @@ module Responders
|
|
113
105
|
|
114
106
|
options = mount_i18n_options(status)
|
115
107
|
message = ::I18n.t options[:default].shift, options
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
def set_flash(key, value)
|
120
|
-
return if value.blank?
|
121
|
-
flash = controller.flash
|
122
|
-
flash = flash.now if set_flash_now?
|
123
|
-
flash[key] ||= value
|
124
|
-
end
|
125
|
-
|
126
|
-
def set_flash_now?
|
127
|
-
(@flash_now == true) || (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success) || (format == :js)
|
108
|
+
controller.flash[status] = message unless message.blank?
|
128
109
|
end
|
129
110
|
|
130
111
|
def set_flash_message? #:nodoc:
|
@@ -132,8 +113,8 @@ module Responders
|
|
132
113
|
end
|
133
114
|
|
134
115
|
def mount_i18n_options(status) #:nodoc:
|
135
|
-
resource_name = if resource.class.respond_to?(:
|
136
|
-
resource.class.
|
116
|
+
resource_name = if resource.class.respond_to?(:model_name)
|
117
|
+
resource.class.model_name.human
|
137
118
|
else
|
138
119
|
resource.class.name.underscore.humanize
|
139
120
|
end
|
@@ -141,7 +122,7 @@ module Responders
|
|
141
122
|
options = {
|
142
123
|
:default => flash_defaults_by_namespace(status),
|
143
124
|
:resource_name => resource_name,
|
144
|
-
:
|
125
|
+
:downcase_resource_name => resource_name.downcase
|
145
126
|
}
|
146
127
|
|
147
128
|
if controller.respond_to?(:interpolation_options, true)
|
@@ -33,8 +33,7 @@ module Responders
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def do_http_cache?
|
36
|
-
get? && @http_cache != false &&
|
37
|
-
!new_record? && controller.response.last_modified.nil? && resource.respond_to?(:updated_at)
|
36
|
+
get? && @http_cache != false && !new_record? && controller.response.last_modified.nil?
|
38
37
|
end
|
39
38
|
|
40
39
|
def new_record?
|
@@ -0,0 +1,10 @@
|
|
1
|
+
en:
|
2
|
+
flash:
|
3
|
+
actions:
|
4
|
+
create:
|
5
|
+
notice: '{{resource_name}} was successfully created.'
|
6
|
+
update:
|
7
|
+
notice: '{{resource_name}} was successfully updated.'
|
8
|
+
destroy:
|
9
|
+
notice: '{{resource_name}} was successfully destroyed.'
|
10
|
+
alert: '{{resource_name}} could not be destroyed.'
|
data/lib/responders/version.rb
CHANGED
@@ -16,10 +16,9 @@ end
|
|
16
16
|
class AddressesController < ApplicationController
|
17
17
|
before_filter :set_resource
|
18
18
|
self.responder = FlashResponder
|
19
|
-
respond_to :js, :only => :create
|
20
19
|
|
21
20
|
def action
|
22
|
-
options = params.slice(:flash
|
21
|
+
options = params.slice(:flash)
|
23
22
|
flash[:success] = "Flash is set" if params[:set_flash]
|
24
23
|
respond_with(@resource, options)
|
25
24
|
end
|
@@ -108,32 +107,6 @@ class FlashResponderTest < ActionController::TestCase
|
|
108
107
|
assert_equal "Resource with block created with success", flash[:success]
|
109
108
|
end
|
110
109
|
|
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
|
-
|
118
|
-
def test_sets_flash_message_can_be_set_to_now
|
119
|
-
@now = {}
|
120
|
-
@controller.flash.expects(:now).returns(@now)
|
121
|
-
post :create, :flash_now => true
|
122
|
-
assert_equal "Resource created with success", @now[:success]
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_sets_flash_message_can_be_set_to_now_only_on_success
|
126
|
-
@now = {}
|
127
|
-
@controller.flash.expects(:now).returns(@now)
|
128
|
-
post :create, :flash_now => :on_success
|
129
|
-
assert_equal "Resource created with success", @now[:success]
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_sets_flash_message_can_be_set_to_now_only_on_failure
|
133
|
-
@controller.flash.expects(:now).never
|
134
|
-
post :create, :flash_now => :on_failure
|
135
|
-
end
|
136
|
-
|
137
110
|
def test_sets_message_based_on_notice_key
|
138
111
|
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
|
139
112
|
post :another
|
@@ -94,9 +94,9 @@ class HttpCacheResponderTest < ActionController::TestCase
|
|
94
94
|
assert_equal 200, @response.status
|
95
95
|
end
|
96
96
|
|
97
|
-
def
|
97
|
+
def test_collection_chooses_the_latest_timestamp
|
98
98
|
get :collection
|
99
|
-
assert_equal
|
99
|
+
assert_equal Time.utc(2009).httpdate, @response.headers["Last-Modified"]
|
100
100
|
assert_match /xml/, @response.body
|
101
101
|
assert_equal 200, @response.status
|
102
102
|
end
|
@@ -108,6 +108,11 @@ class HttpCacheResponderTest < ActionController::TestCase
|
|
108
108
|
assert_equal 200, @response.status
|
109
109
|
end
|
110
110
|
|
111
|
+
def test_it_does_not_set_body_etag
|
112
|
+
get :collection
|
113
|
+
assert_nil @response.headers["ETag"]
|
114
|
+
end
|
115
|
+
|
111
116
|
def test_does_not_set_cache_for_new_records
|
112
117
|
get :new_record
|
113
118
|
assert_nil @response.headers["Last-Modified"]
|
data/test/test_helper.rb
CHANGED
@@ -20,13 +20,14 @@ RAILS_ROOT = "anywhere"
|
|
20
20
|
|
21
21
|
require 'active_support'
|
22
22
|
require 'action_controller'
|
23
|
-
require '
|
23
|
+
require 'action_dispatch/middleware/flash'
|
24
|
+
|
25
|
+
require 'rails/railtie'
|
24
26
|
|
25
27
|
class ApplicationController < ActionController::Base
|
26
28
|
respond_to :html, :xml
|
27
29
|
end
|
28
30
|
|
29
|
-
# Add IR to load path and load the main file
|
30
31
|
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
31
32
|
require 'responders'
|
32
33
|
|
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: responders
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 4
|
9
|
-
- 8
|
10
|
-
version: 0.4.8
|
4
|
+
version: 0.5.0
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -15,7 +9,7 @@ autorequire:
|
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
11
|
|
18
|
-
date:
|
12
|
+
date: 2010-02-07 00:00:00 +01:00
|
19
13
|
default_executable:
|
20
14
|
dependencies: []
|
21
15
|
|
@@ -32,44 +26,40 @@ files:
|
|
32
26
|
- MIT-LICENSE
|
33
27
|
- README.rdoc
|
34
28
|
- Rakefile
|
29
|
+
- lib/generators/rails/USAGE
|
30
|
+
- lib/generators/rails/responders_controller_generator.rb
|
31
|
+
- lib/generators/rails/templates/controller.rb
|
32
|
+
- lib/generators/responders_install_generator.rb
|
35
33
|
- lib/responders.rb
|
36
34
|
- lib/responders/flash_responder.rb
|
37
35
|
- lib/responders/http_cache_responder.rb
|
36
|
+
- lib/responders/locales/en.yml
|
38
37
|
- lib/responders/version.rb
|
39
|
-
- test/flash_responder_test.rb
|
40
|
-
- test/http_cache_responder_test.rb
|
41
|
-
- test/test_helper.rb
|
42
38
|
has_rdoc: true
|
43
39
|
homepage: http://github.com/plataformatec/responders
|
44
40
|
licenses: []
|
45
41
|
|
46
42
|
post_install_message:
|
47
|
-
rdoc_options:
|
48
|
-
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
49
45
|
require_paths:
|
50
46
|
- lib
|
51
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
48
|
requirements:
|
54
49
|
- - ">="
|
55
50
|
- !ruby/object:Gem::Version
|
56
|
-
hash: 3
|
57
|
-
segments:
|
58
|
-
- 0
|
59
51
|
version: "0"
|
52
|
+
version:
|
60
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
54
|
requirements:
|
63
55
|
- - ">="
|
64
56
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
|
-
segments:
|
67
|
-
- 0
|
68
57
|
version: "0"
|
58
|
+
version:
|
69
59
|
requirements: []
|
70
60
|
|
71
61
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.5
|
62
|
+
rubygems_version: 1.3.5
|
73
63
|
signing_key:
|
74
64
|
specification_version: 3
|
75
65
|
summary: A set of Rails 3 responders to dry up your application
|