responders 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  * :js now sets the flash.now by default, instead of flash
4
4
  * Renamed responders_install generator to responders:install
5
- * Added a fix for Rails 3.0.0.beta3
5
+ * Added CollectionResponder which allows you to always redirect to the collection path (index action) after POST/PUT/DELETE
6
6
 
7
7
  == 0.5
8
8
 
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "3.0.0.beta3"
3
+ gem "rails", :path => "../rails"
4
4
  gem "mocha"
5
5
 
6
6
  if RUBY_VERSION < "1.9"
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: /Users/jose/Work/github/rails
3
+ specs:
4
+ actionmailer (3.0.0.beta4)
5
+ actionpack (= 3.0.0.beta4)
6
+ mail (~> 2.2.5)
7
+ actionpack (3.0.0.beta4)
8
+ activemodel (= 3.0.0.beta4)
9
+ activesupport (= 3.0.0.beta4)
10
+ builder (~> 2.1.2)
11
+ erubis (~> 2.6.6)
12
+ i18n (~> 0.4.1)
13
+ rack (~> 1.2.1)
14
+ rack-mount (~> 0.6.9)
15
+ rack-test (~> 0.5.4)
16
+ tzinfo (~> 0.3.22)
17
+ activemodel (3.0.0.beta4)
18
+ activesupport (= 3.0.0.beta4)
19
+ builder (~> 2.1.2)
20
+ i18n (~> 0.4.1)
21
+ activerecord (3.0.0.beta4)
22
+ activemodel (= 3.0.0.beta4)
23
+ activesupport (= 3.0.0.beta4)
24
+ arel (~> 0.4.0)
25
+ tzinfo (~> 0.3.22)
26
+ activeresource (3.0.0.beta4)
27
+ activemodel (= 3.0.0.beta4)
28
+ activesupport (= 3.0.0.beta4)
29
+ activesupport (3.0.0.beta4)
30
+ rails (3.0.0.beta4)
31
+ actionmailer (= 3.0.0.beta4)
32
+ actionpack (= 3.0.0.beta4)
33
+ activerecord (= 3.0.0.beta4)
34
+ activeresource (= 3.0.0.beta4)
35
+ activesupport (= 3.0.0.beta4)
36
+ bundler (>= 1.0.0.beta.10)
37
+ railties (= 3.0.0.beta4)
38
+ railties (3.0.0.beta4)
39
+ actionpack (= 3.0.0.beta4)
40
+ activesupport (= 3.0.0.beta4)
41
+ rake (>= 0.8.3)
42
+ thor (~> 0.14.0)
43
+
44
+ GEM
45
+ remote: http://rubygems.org/
46
+ specs:
47
+ abstract (1.0.0)
48
+ arel (0.4.0)
49
+ activesupport (>= 3.0.0.beta)
50
+ builder (2.1.2)
51
+ columnize (0.3.1)
52
+ erubis (2.6.6)
53
+ abstract (>= 1.0.0)
54
+ i18n (0.4.1)
55
+ linecache (0.43)
56
+ mail (2.2.5)
57
+ activesupport (>= 2.3.6)
58
+ mime-types
59
+ treetop (>= 1.4.5)
60
+ mime-types (1.16)
61
+ mocha (0.9.8)
62
+ rake
63
+ polyglot (0.3.1)
64
+ rack (1.2.1)
65
+ rack-mount (0.6.9)
66
+ rack (>= 1.0.0)
67
+ rack-test (0.5.4)
68
+ rack (>= 1.0)
69
+ rake (0.8.7)
70
+ ruby-debug (0.10.3)
71
+ columnize (>= 0.1)
72
+ ruby-debug-base (~> 0.10.3.0)
73
+ ruby-debug-base (0.10.3)
74
+ linecache (>= 0.3)
75
+ thor (0.14.0)
76
+ treetop (1.4.8)
77
+ polyglot (>= 0.3.1)
78
+ tzinfo (0.3.22)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ mocha
85
+ rails!
86
+ ruby-debug
data/README.rdoc CHANGED
@@ -13,12 +13,12 @@ to be used with Rails 2.3 together with Inherited Resources, please check this b
13
13
  flash:
14
14
  actions:
15
15
  create:
16
- notice: "{{resource_name}} was successfully created."
16
+ notice: "%{resource_name} was successfully created."
17
17
  update:
18
- notice: "{{resource_name}} was successfully updated."
18
+ notice: "%{resource_name} was successfully updated."
19
19
  destroy:
20
- notice: "{{resource_name}} was successfully destroyed."
21
- alert: "{{resource_name}} could not be destroyed."
20
+ notice: "%{resource_name} was successfully destroyed."
21
+ alert: "%{resource_name} could not be destroyed."
22
22
 
23
23
  In case the resource contains errors, you should use the failure key on I18n. This is
24
24
  useful to dry up flash messages from your controllers. If you need a specific message
@@ -42,6 +42,8 @@ to be used with Rails 2.3 together with Inherited Resources, please check this b
42
42
  allows clients to easily query the server if a resource changed and if the client tries
43
43
  to retrieve a resource that has not been modified, it returns not_modified status.
44
44
 
45
+ * CollectionResponder - Makes your create and update action redirect to the collection on success.
46
+
45
47
  == Configuring your own responder
46
48
 
47
49
  The first step is instal responders gem and configure it in your application:
@@ -49,13 +51,24 @@ The first step is instal responders gem and configure it in your application:
49
51
  gem install responders
50
52
 
51
53
  Responders only provides a set of modules, to use them, you have to create your own
52
- responder. This can be done in an initializer for example:
54
+ responder. This can be done inside the lib folder for example:
53
55
 
56
+ # lib/app_responder.rb
54
57
  class AppResponder < ActionController::Responder
55
58
  include Responders::FlashResponder
56
59
  include Responders::HttpCacheResponder
57
60
  end
58
61
 
62
+ And then you need to configure your application to use it:
63
+
64
+ # app/controllers/application_controller.rb
65
+ require "app_responder"
66
+
67
+ class ApplicationController < ActionController::Base
68
+ self.responder = AppResponder
69
+ respond_to :html
70
+ end
71
+
59
72
  Or, for your convenience, just do:
60
73
 
61
74
  rails generate responders:install
@@ -3,9 +3,7 @@ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generato
3
3
  module Rails
4
4
  module Generators
5
5
  class RespondersControllerGenerator < ScaffoldControllerGenerator
6
- def self.source_root
7
- @_source_root ||= File.expand_path("templates", File.dirname(__FILE__))
8
- end
6
+ source_root File.expand_path("../templates", __FILE__)
9
7
 
10
8
  protected
11
9
 
@@ -1,9 +1,7 @@
1
1
  module Responders
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- def self.source_root
5
- @_source_root ||= File.expand_path("..", __FILE__)
6
- end
4
+ source_root File.expand_path("..", __FILE__)
7
5
 
8
6
  desc "Creates an initializer with default responder configuration and copy locale file"
9
7
 
@@ -12,6 +10,10 @@ module Responders
12
10
  class ApplicationResponder < ActionController::Responder
13
11
  include Responders::FlashResponder
14
12
  include Responders::HttpCacheResponder
13
+
14
+ # Uncomment this responder if you want your resources to redirect to the collection
15
+ # path (index action) instead of the resource path for POST/PUT/DELETE requests.
16
+ # include Responders::CollectionResponder
15
17
  end
16
18
  RUBY
17
19
  end
data/lib/responders.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'action_controller/base'
2
2
 
3
3
  module Responders
4
- autoload :FlashResponder, 'responders/flash_responder'
5
- autoload :HttpCacheResponder, 'responders/http_cache_responder'
4
+ autoload :FlashResponder, 'responders/flash_responder'
5
+ autoload :HttpCacheResponder, 'responders/http_cache_responder'
6
+ autoload :CollectionResponder, 'responders/collection_responder'
6
7
 
7
8
  require 'responders/controller_method'
8
9
 
@@ -0,0 +1,30 @@
1
+ module Responders
2
+ # This responder modifies your current responder to redirect
3
+ # to the collection page on POST/PUT/DELETE.
4
+ module CollectionResponder
5
+ protected
6
+
7
+ # Returns the collection location for redirecting after POST/PUT/DELETE.
8
+ # This method, converts the following resources array to the following:
9
+ #
10
+ # [:admin, @post] #=> [:admin, :posts]
11
+ # [@user, @post] #=> [@user, :posts]
12
+ #
13
+ # When these new arrays are given to redirect_to, it will generate the
14
+ # proper URL pointing to the index action.
15
+ #
16
+ # [:admin, @post] #=> admin_posts_url
17
+ # [@user, @post] #=> user_posts_url(@user.to_param)
18
+ #
19
+ def navigation_location
20
+ return options[:location] if options[:location]
21
+ klass = resources.last.class
22
+
23
+ if klass.respond_to?(:model_name)
24
+ resources[0...-1] << klass.model_name.plural.to_sym
25
+ else
26
+ resources
27
+ end
28
+ end
29
+ end
30
+ end
@@ -22,7 +22,7 @@ module Responders
22
22
  when Module
23
23
  responder
24
24
  when String, Symbol
25
- "#{responder.to_s.classify}Responder".constantize
25
+ Responders.const_get("#{responder.to_s.classify}Responder")
26
26
  else
27
27
  raise "responder has to be a string, a symbol or a module"
28
28
  end
@@ -34,13 +34,4 @@ module Responders
34
34
  end
35
35
  end
36
36
 
37
- # Fix for Rails <= 3.0.0.beta3
38
- require "action_controller/metal/responder"
39
-
40
- class ActionController::Responder
41
- def default_action
42
- @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol]
43
- end
44
- end
45
-
46
37
  ActionController::Base.extend Responders::ControllerMethod
@@ -21,7 +21,7 @@ module Responders
21
21
  # flash:
22
22
  # actions:
23
23
  # create:
24
- # notice: "Hooray! {{resource_name}} was successfully created!"
24
+ # notice: "Hooray! %{resource_name} was successfully created!"
25
25
  #
26
26
  # But sometimes, flash messages are not that simple. Going back
27
27
  # to cars example, you might want to say the brand of the car when it's
@@ -30,7 +30,7 @@ module Responders
30
30
  # flash:
31
31
  # cars:
32
32
  # update:
33
- # notice: "Hooray! You just tuned your {{car_brand}}!"
33
+ # notice: "Hooray! You just tuned your %{car_brand}!"
34
34
  #
35
35
  # Since :car_name is not available for interpolation by default, you have
36
36
  # to overwrite interpolation_options in your controller.
@@ -23,8 +23,7 @@ module Responders
23
23
 
24
24
  def do_http_cache!
25
25
  timestamp = resources.flatten.map do |resource|
26
- next unless resource.respond_to?(:updated_at)
27
- resource.updated_at.utc
26
+ resource.updated_at.try(:utc) if resource.respond_to?(:updated_at)
28
27
  end.compact.max
29
28
 
30
29
  controller.response.last_modified ||= timestamp if timestamp
@@ -1,3 +1,3 @@
1
1
  module Responders
2
- VERSION = "0.6.1".freeze
2
+ VERSION = "0.6.2".freeze
3
3
  end
@@ -0,0 +1,62 @@
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
+ end
30
+
31
+ class CollectionResponderTest < ActionController::TestCase
32
+ tests CollectionController
33
+
34
+ def test_collection_with_single_resource
35
+ @controller.expects(:addresses_url).returns("addresses_url")
36
+ post :single
37
+ assert_redirected_to "addresses_url"
38
+ end
39
+
40
+ def test_collection_with_namespaced_resource
41
+ @controller.expects(:admin_addresses_url).returns("admin_addresses_url")
42
+ put :namespaced
43
+ assert_redirected_to "admin_addresses_url"
44
+ end
45
+
46
+ def test_collection_with_nested_resource
47
+ @controller.expects(:user_addresses_url).returns("user_addresses_url")
48
+ delete :nested
49
+ assert_redirected_to "user_addresses_url"
50
+ end
51
+
52
+ def test_collection_respects_location_option
53
+ delete :with_location
54
+ assert_redirected_to "given_location"
55
+ end
56
+
57
+ def test_collection_respects_only_symbols
58
+ @controller.expects(:admin_addresses_url).returns("admin_addresses_url")
59
+ post :only_symbols
60
+ assert_redirected_to "admin_addresses_url"
61
+ end
62
+ end
@@ -1,14 +1,5 @@
1
1
  require 'test_helper'
2
2
 
3
- class Address
4
- attr_accessor :errors
5
- def self.human_name; 'Address'; end
6
-
7
- def initialize
8
- @errors = {}
9
- end
10
- end
11
-
12
3
  class FlashResponder < ActionController::Responder
13
4
  include Responders::FlashResponder
14
5
  end
@@ -16,6 +7,7 @@ end
16
7
  class AddressesController < ApplicationController
17
8
  before_filter :set_resource
18
9
  self.responder = FlashResponder
10
+
19
11
  respond_to :js, :only => :create
20
12
 
21
13
  def action
@@ -38,7 +30,7 @@ class AddressesController < ApplicationController
38
30
  respond_with(@resource, :notice => "Yes, notice this!", :alert => "Warning, warning!")
39
31
  end
40
32
 
41
- protected
33
+ protected
42
34
 
43
35
  def interpolation_options
44
36
  { :reference => 'Ocean Avenue' }
@@ -46,7 +38,7 @@ protected
46
38
 
47
39
  def set_resource
48
40
  @resource = Address.new
49
- @resource.errors[:fail] = true if params[:fail]
41
+ @resource.errors[:fail] << "FAIL" if params[:fail]
50
42
  end
51
43
  end
52
44
 
@@ -109,29 +101,26 @@ class FlashResponderTest < ActionController::TestCase
109
101
  end
110
102
 
111
103
  def test_sets_now_flash_message_on_javascript_requests
112
- @now = {}
113
- @controller.flash.expects(:now).returns(@now)
114
104
  post :create, :format => :js
115
- assert_equal "Resource created with success", @now[:success]
105
+ assert_equal "Resource created with success", flash[:success]
106
+ assert_flash_now :success
116
107
  end
117
108
 
118
109
  def test_sets_flash_message_can_be_set_to_now
119
- @now = {}
120
- @controller.flash.expects(:now).returns(@now)
121
110
  post :create, :flash_now => true
122
- assert_equal "Resource created with success", @now[:success]
111
+ assert_equal "Resource created with success", @controller.flash.now[:success]
112
+ assert_flash_now :success
123
113
  end
124
114
 
125
115
  def test_sets_flash_message_can_be_set_to_now_only_on_success
126
- @now = {}
127
- @controller.flash.expects(:now).returns(@now)
128
116
  post :create, :flash_now => :on_success
129
- assert_equal "Resource created with success", @now[:success]
117
+ assert_equal "Resource created with success", @controller.flash.now[:success]
118
+ assert_flash_now :success
130
119
  end
131
120
 
132
121
  def test_sets_flash_message_can_be_set_to_now_only_on_failure
133
- @controller.flash.expects(:now).never
134
122
  post :create, :flash_now => :on_failure
123
+ assert_not_flash_now :success
135
124
  end
136
125
 
137
126
  def test_sets_message_based_on_notice_key
@@ -145,6 +134,17 @@ class FlashResponderTest < ActionController::TestCase
145
134
  post :another, :fail => true
146
135
  assert_equal "Warning, warning!", flash[:alert]
147
136
  end
137
+
138
+ # If we have flash.now, it's always marked as used.
139
+ def assert_flash_now(k)
140
+ assert flash.instance_variable_get(:@used).include?(k.to_sym),
141
+ "Expected #{k} to be in flash.now, but it's not."
142
+ end
143
+
144
+ def assert_not_flash_now(k)
145
+ assert !flash.instance_variable_get(:@used).include?(k.to_sym),
146
+ "Expected #{k} to not be in flash.now, but it is."
147
+ end
148
148
  end
149
149
 
150
150
  class NamespacedFlashResponderTest < ActionController::TestCase
@@ -10,15 +10,15 @@ class HttpCacheController < ApplicationController
10
10
  def single
11
11
  options = params.slice(:http_cache)
12
12
  response.last_modified = Time.utc(2008) if params[:last_modified]
13
- respond_with(Model.new(Time.utc(2009)), options)
13
+ respond_with(Address.new(Time.utc(2009)), options)
14
14
  end
15
15
 
16
16
  def collection
17
- respond_with [Model.new(Time.utc(2009)), Model.new(Time.utc(2008))]
17
+ respond_with [Address.new(Time.utc(2009)), Address.new(Time.utc(2008))]
18
18
  end
19
19
 
20
20
  def not_persisted
21
- model = Model.new(Time.utc(2009))
21
+ model = Address.new(Time.utc(2009))
22
22
  model.persisted = false
23
23
  respond_with(model)
24
24
  end
data/test/test_helper.rb CHANGED
@@ -2,7 +2,6 @@ require 'rubygems'
2
2
  require 'bundler'
3
3
 
4
4
  Bundler.setup
5
-
6
5
  require 'test/unit'
7
6
  require 'mocha'
8
7
 
@@ -11,6 +10,7 @@ ENV["RAILS_ENV"] = "test"
11
10
 
12
11
  require 'active_support'
13
12
  require 'action_controller'
13
+ require 'active_model'
14
14
  require 'rails/railtie'
15
15
 
16
16
  class ApplicationController < ActionController::Base
@@ -26,26 +26,42 @@ I18n.reload!
26
26
  ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
27
27
 
28
28
  Responders::Routes = ActionDispatch::Routing::RouteSet.new
29
- Responders::Routes.draw do |map|
30
- map.connect 'admin/:action', :controller => "admin/addresses"
31
- map.connect ':controller/:action/:id'
32
- map.connect ':controller/:action'
29
+ Responders::Routes.draw do
30
+ match '/admin/:action', :controller => "admin/addresses"
31
+ match '/:controller(/:action(/:id))'
33
32
  end
34
33
 
34
+ ActionController::Base.send :include, Responders::Routes.url_helpers
35
+
35
36
  class ActiveSupport::TestCase
36
37
  setup do
37
38
  @routes = Responders::Routes
38
39
  end
39
40
  end
40
41
 
41
- class Model < Struct.new(:updated_at)
42
- attr_writer :persisted
42
+ class Model
43
+ include ActiveModel::Conversion
44
+ include ActiveModel::Validations
45
+
46
+ attr_accessor :persisted, :updated_at
47
+ alias :persisted? :persisted
43
48
 
44
49
  def persisted?
45
- defined?(@persisted) ? @persisted : true
50
+ @persisted
46
51
  end
47
52
 
48
53
  def to_xml(*args)
49
54
  "<xml />"
50
55
  end
56
+
57
+ def initialize(updated_at=nil)
58
+ @persisted = true
59
+ self.updated_at = updated_at
60
+ end
61
+ end
62
+
63
+ class Address < Model
64
+ end
65
+
66
+ class User < Model
51
67
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responders
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 3
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 6
8
- - 1
9
- version: 0.6.1
9
+ - 2
10
+ version: 0.6.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - "Jos\xC3\xA9 Valim"
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-24 00:00:00 +02:00
18
+ date: 2010-07-26 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies: []
20
21
 
@@ -29,6 +30,7 @@ extra_rdoc_files:
29
30
  files:
30
31
  - CHANGELOG.rdoc
31
32
  - Gemfile
33
+ - Gemfile.lock
32
34
  - MIT-LICENSE
33
35
  - README.rdoc
34
36
  - Rakefile
@@ -37,11 +39,17 @@ files:
37
39
  - lib/generators/rails/templates/controller.rb
38
40
  - lib/generators/responders/install_generator.rb
39
41
  - lib/responders.rb
42
+ - lib/responders/collection_responder.rb
40
43
  - lib/responders/controller_method.rb
41
44
  - lib/responders/flash_responder.rb
42
45
  - lib/responders/http_cache_responder.rb
43
46
  - lib/responders/locales/en.yml
44
47
  - lib/responders/version.rb
48
+ - test/collection_responder_test.rb
49
+ - test/controller_method_test.rb
50
+ - test/flash_responder_test.rb
51
+ - test/http_cache_responder_test.rb
52
+ - test/test_helper.rb
45
53
  has_rdoc: true
46
54
  homepage: http://github.com/plataformatec/responders
47
55
  licenses: []
@@ -52,27 +60,32 @@ rdoc_options:
52
60
  require_paths:
53
61
  - lib
54
62
  required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
55
64
  requirements:
56
65
  - - ">="
57
66
  - !ruby/object:Gem::Version
67
+ hash: 3
58
68
  segments:
59
69
  - 0
60
70
  version: "0"
61
71
  required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
62
73
  requirements:
63
74
  - - ">="
64
75
  - !ruby/object:Gem::Version
76
+ hash: 3
65
77
  segments:
66
78
  - 0
67
79
  version: "0"
68
80
  requirements: []
69
81
 
70
82
  rubyforge_project:
71
- rubygems_version: 1.3.6
83
+ rubygems_version: 1.3.7
72
84
  signing_key:
73
85
  specification_version: 3
74
86
  summary: A set of Rails 3 responders to dry up your application
75
87
  test_files:
88
+ - test/collection_responder_test.rb
76
89
  - test/controller_method_test.rb
77
90
  - test/flash_responder_test.rb
78
91
  - test/http_cache_responder_test.rb