From 0ad511fcdca08c4cae646a633cf6329f0dca1dc4 Mon Sep 17 00:00:00 2001 From: fabian Date: Fri, 28 Jan 2011 18:24:42 +0800 Subject: [PATCH 01/12] fixed reloading of ApplicationHelper in development mode --- lib/inherited_resources/base.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/inherited_resources/base.rb b/lib/inherited_resources/base.rb index 003efa0b..96cc0083 100644 --- a/lib/inherited_resources/base.rb +++ b/lib/inherited_resources/base.rb @@ -39,6 +39,11 @@ def self.inherit_resources(base) :resource_class?, :parents_symbols?, :resources_configuration? end end + + def self.inherited(klass) + super + klass.helper :all if klass.superclass == InheritedResources::Base + end inherit_resources(self) end From cbf12d4f881edc9df59c7f26814952cd804bb607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 17 Apr 2011 18:05:53 +0200 Subject: [PATCH 02/12] Release v1.2.2 --- CHANGELOG | 5 +++++ README.rdoc | 10 ++++------ lib/inherited_resources/version.rb | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3b9e74dd..a03db39f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +# Version 1.2.2 + +* Fix a bug in params parsing +* Call .scoped only if it is available + # Version 1.2.1 * Fix a bug with namespaces diff --git a/README.rdoc b/README.rdoc index bf41f0f5..d94f8e37 100644 --- a/README.rdoc +++ b/README.rdoc @@ -15,11 +15,9 @@ http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with === Rails 3 -Inherited Resources master branch is now supports Rails 3 and is NOT backward compatible. +You can let bundler install Inherited Resources by adding this line to your application's Gemfile: -You can let bundler to install Inherited Resources by adding this line to your application's Gemfile: - - gem 'inherited_resources', '~> 1.2.1' + gem 'inherited_resources' And then execute: @@ -27,13 +25,13 @@ And then execute: Or install it yourself as: - gem install inherited_resources --version=1.2.1 + gem install inherited_resources === Rails 2.3.x If you want to use the Rails 2.3.x version, you should install: - sudo gem install inherited_resources --version=1.0.6 + gem install inherited_resources --version=1.0.6 Or checkout from the v1.0 branch: diff --git a/lib/inherited_resources/version.rb b/lib/inherited_resources/version.rb index 1bbf2046..736305e2 100644 --- a/lib/inherited_resources/version.rb +++ b/lib/inherited_resources/version.rb @@ -1,3 +1,3 @@ module InheritedResources - VERSION = '1.2.1'.freeze + VERSION = '1.2.2'.freeze end From 12571db2d9524d52fe8785c3023221cf1ceb1cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 18 Apr 2011 09:17:11 +0200 Subject: [PATCH 03/12] Rails 3.1 forward compability. --- lib/inherited_resources/base.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/inherited_resources/base.rb b/lib/inherited_resources/base.rb index 96cc0083..0f2c2fbe 100644 --- a/lib/inherited_resources/base.rb +++ b/lib/inherited_resources/base.rb @@ -42,7 +42,8 @@ def self.inherit_resources(base) def self.inherited(klass) super - klass.helper :all if klass.superclass == InheritedResources::Base + include_all = !respond_to?(:include_all_helpers) || include_all_helpers + klass.helper :all if klass.superclass == InheritedResources::Base && include_all end inherit_resources(self) From 30b078eb61201e5e70d50d83f7ed6fc4f7fa4463 Mon Sep 17 00:00:00 2001 From: Jarrod Carlson Date: Sun, 1 May 2011 14:48:16 -0400 Subject: [PATCH 04/12] Resolves https://github.com/josevalim/inherited_resources/issues/139 --- lib/inherited_resources/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inherited_resources/base.rb b/lib/inherited_resources/base.rb index 0f2c2fbe..adfdef1c 100644 --- a/lib/inherited_resources/base.rb +++ b/lib/inherited_resources/base.rb @@ -21,7 +21,7 @@ def self.inherit_resources(base) extend InheritedResources::UrlHelpers # Add at least :html mime type - respond_to :html + respond_to :html if self.mimes_for_respond_to.empty? self.responder = InheritedResources::Responder helper_method :resource, :collection, :resource_class, :association_chain, From e5334f01898024894470545dd5cef015d8d55517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 5 May 2011 22:19:24 -0700 Subject: [PATCH 05/12] Edited README.rdoc via GitHub --- README.rdoc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.rdoc b/README.rdoc index d94f8e37..814a5af6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -8,8 +8,8 @@ Plus, making your controllers follow a pattern, it helps you to write better code by following fat models and skinny controllers convention. There are two screencasts available besides this README: -http://railscasts.com/episodes/230-inherited-resources -http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources +* http://railscasts.com/episodes/230-inherited-resources +* http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources == Installation @@ -514,6 +514,8 @@ check this Wiki page: https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance +Notice that Rails 3.1 ships with view inheritance built-in. + == Some DSL For those DSL lovers, InheritedResources won't leave you alone. You can overwrite @@ -534,9 +536,9 @@ And then you can rewrite the last example as: == Bugs and Feedback -If you discover any bugs or want to drop a line, join us in the mailing list: +If you discover any bugs, please describe it in the issues tracker, including Rails and Inherited Resources versions. -http://groups.google.com/group/inherited_resources +Questions are better handled on StackOverflow. -Copyright (c) 2009 José Valim http://blog.plataformatec.com.br +Copyright (c) 2011 José Valim http://blog.plataformatec.com.br See the attached MIT License. From 9886f20de211a550ab0d81a98fa11e38676b35c3 Mon Sep 17 00:00:00 2001 From: thedarkone Date: Wed, 18 May 2011 15:53:54 +0200 Subject: [PATCH 06/12] Turn InheritedResources::Base into a reloadable constant. --- {lib => app/controllers}/inherited_resources/base.rb | 11 +---------- lib/inherited_resources.rb | 5 +++-- 2 files changed, 4 insertions(+), 12 deletions(-) rename {lib => app/controllers}/inherited_resources/base.rb (85%) diff --git a/lib/inherited_resources/base.rb b/app/controllers/inherited_resources/base.rb similarity index 85% rename from lib/inherited_resources/base.rb rename to app/controllers/inherited_resources/base.rb index adfdef1c..03f46ef1 100644 --- a/lib/inherited_resources/base.rb +++ b/app/controllers/inherited_resources/base.rb @@ -1,6 +1,3 @@ -require 'inherited_resources/blank_slate' -require 'inherited_resources/responder' - module InheritedResources # = Base # @@ -39,13 +36,7 @@ def self.inherit_resources(base) :resource_class?, :parents_symbols?, :resources_configuration? end end - - def self.inherited(klass) - super - include_all = !respond_to?(:include_all_helpers) || include_all_helpers - klass.helper :all if klass.superclass == InheritedResources::Base && include_all - end inherit_resources(self) end -end +end \ No newline at end of file diff --git a/lib/inherited_resources.rb b/lib/inherited_resources.rb index 91178040..f7b64cca 100644 --- a/lib/inherited_resources.rb +++ b/lib/inherited_resources.rb @@ -1,10 +1,11 @@ require 'responders' +require 'inherited_resources/blank_slate' +require 'inherited_resources/responder' module InheritedResources ACTIONS = [ :index, :show, :new, :edit, :create, :update, :destroy ] unless self.const_defined?(:ACTIONS) autoload :Actions, 'inherited_resources/actions' - autoload :Base, 'inherited_resources/base' autoload :BaseHelpers, 'inherited_resources/base_helpers' autoload :ShallowHelpers, 'inherited_resources/shallow_helpers' autoload :BelongsToHelpers, 'inherited_resources/belongs_to_helpers' @@ -20,7 +21,7 @@ def self.flash_keys=(array) Responders::FlashResponder.flash_keys = array end - class Railtie < ::Rails::Railtie + class Railtie < ::Rails::Engine config.inherited_resources = InheritedResources if config.respond_to?(:app_generators) config.app_generators.scaffold_controller = :inherited_resources_controller From 57b2f290d6a5f28c421a81d1b69d9e7279715fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 20 Jun 2011 09:00:17 -0300 Subject: [PATCH 07/12] Make test suite run again. --- lib/inherited_resources.rb | 1 + test/test_helper.rb | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/inherited_resources.rb b/lib/inherited_resources.rb index f7b64cca..96e42883 100644 --- a/lib/inherited_resources.rb +++ b/lib/inherited_resources.rb @@ -1,3 +1,4 @@ +require 'rails/engine' require 'responders' require 'inherited_resources/blank_slate' require 'inherited_resources/responder' diff --git a/test/test_helper.rb b/test/test_helper.rb index 7e33eef9..33a7cabf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,7 +12,6 @@ require "active_support" require "active_model" require "action_controller" -require "rails/railtie" I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml') I18n.reload! @@ -23,6 +22,10 @@ class ApplicationController < ActionController::Base; end $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'inherited_resources' +# Add app base to load path +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../app/controllers') +require 'inherited_resources/base' + ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views') InheritedResources::Routes = ActionDispatch::Routing::RouteSet.new From 8320335837101f75046c5657f6f58371be8b88ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 20 Jun 2011 10:03:32 -0300 Subject: [PATCH 08/12] Update to 3.1.rc4 and fix polymorphic tests. --- Gemfile | 2 +- Gemfile.lock | 116 ++++++++++++++++++++++----------------- test/test_helper.rb | 8 +-- test/url_helpers_test.rb | 95 ++++++++++++++++---------------- 4 files changed, 120 insertions(+), 101 deletions(-) diff --git a/Gemfile b/Gemfile index 9eb7739f..6e25dbce 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "http://rubygems.org" -gem "rails", "3.0.3" +gem "rails", "3.1.0.rc4" gem "responders", "~> 0.6.0" gem "has_scope", "~> 0.5.0" gem "mocha" diff --git a/Gemfile.lock b/Gemfile.lock index 4b2eeb03..0b6280c6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,79 +1,95 @@ GEM remote: http://rubygems.org/ specs: - abstract (1.0.0) - actionmailer (3.0.3) - actionpack (= 3.0.3) - mail (~> 2.2.9) - actionpack (3.0.3) - activemodel (= 3.0.3) - activesupport (= 3.0.3) - builder (~> 2.1.2) - erubis (~> 2.6.6) - i18n (~> 0.4) - rack (~> 1.2.1) - rack-mount (~> 0.6.13) - rack-test (~> 0.5.6) - tzinfo (~> 0.3.23) - activemodel (3.0.3) - activesupport (= 3.0.3) - builder (~> 2.1.2) - i18n (~> 0.4) - activerecord (3.0.3) - activemodel (= 3.0.3) - activesupport (= 3.0.3) - arel (~> 2.0.2) - tzinfo (~> 0.3.23) - activeresource (3.0.3) - activemodel (= 3.0.3) - activesupport (= 3.0.3) - activesupport (3.0.3) - arel (2.0.6) - builder (2.1.2) + actionmailer (3.1.0.rc4) + actionpack (= 3.1.0.rc4) + mail (~> 2.3.0) + actionpack (3.1.0.rc4) + activemodel (= 3.1.0.rc4) + activesupport (= 3.1.0.rc4) + builder (~> 3.0.0) + erubis (~> 2.7.0) + i18n (~> 0.6) + rack (~> 1.3.0) + rack-cache (~> 1.0.1) + rack-mount (~> 0.8.1) + rack-test (~> 0.6.0) + sprockets (~> 2.0.0.beta.10) + tzinfo (~> 0.3.27) + activemodel (3.1.0.rc4) + activesupport (= 3.1.0.rc4) + bcrypt-ruby (~> 2.1.4) + builder (~> 3.0.0) + i18n (~> 0.6) + activerecord (3.1.0.rc4) + activemodel (= 3.1.0.rc4) + activesupport (= 3.1.0.rc4) + arel (~> 2.1.1) + tzinfo (~> 0.3.27) + activeresource (3.1.0.rc4) + activemodel (= 3.1.0.rc4) + activesupport (= 3.1.0.rc4) + activesupport (3.1.0.rc4) + multi_json (~> 1.0) + arel (2.1.1) + bcrypt-ruby (2.1.4) + builder (3.0.0) columnize (0.3.1) - erubis (2.6.6) - abstract (>= 1.0.0) + erubis (2.7.0) has_scope (0.5.0) - i18n (0.5.0) + hike (1.1.0) + i18n (0.6.0) linecache (0.43) - mail (2.2.14) - activesupport (>= 2.3.6) + mail (2.3.0) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.16) mocha (0.9.8) rake + multi_json (1.0.3) polyglot (0.3.1) - rack (1.2.1) - rack-mount (0.6.13) + rack (1.3.0) + rack-cache (1.0.2) + rack (>= 0.4) + rack-mount (0.8.1) rack (>= 1.0.0) - rack-test (0.5.7) + rack-ssl (1.3.2) + rack + rack-test (0.6.0) rack (>= 1.0) - rails (3.0.3) - actionmailer (= 3.0.3) - actionpack (= 3.0.3) - activerecord (= 3.0.3) - activeresource (= 3.0.3) - activesupport (= 3.0.3) + rails (3.1.0.rc4) + actionmailer (= 3.1.0.rc4) + actionpack (= 3.1.0.rc4) + activerecord (= 3.1.0.rc4) + activeresource (= 3.1.0.rc4) + activesupport (= 3.1.0.rc4) bundler (~> 1.0) - railties (= 3.0.3) - railties (3.0.3) - actionpack (= 3.0.3) - activesupport (= 3.0.3) + railties (= 3.1.0.rc4) + railties (3.1.0.rc4) + actionpack (= 3.1.0.rc4) + activesupport (= 3.1.0.rc4) + rack-ssl (~> 1.3.2) rake (>= 0.8.7) - thor (~> 0.14.4) + rdoc (~> 3.4) + thor (~> 0.14.6) rake (0.8.7) + rdoc (3.6.1) responders (0.6.2) ruby-debug (0.10.3) columnize (>= 0.1) ruby-debug-base (~> 0.10.3.0) ruby-debug-base (0.10.3) linecache (>= 0.3) + sprockets (2.0.0.beta.10) + hike (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) thor (0.14.6) + tilt (1.3.2) treetop (1.4.9) polyglot (>= 0.3.1) - tzinfo (0.3.24) + tzinfo (0.3.28) PLATFORMS ruby @@ -81,6 +97,6 @@ PLATFORMS DEPENDENCIES has_scope (~> 0.5.0) mocha - rails (= 3.0.3) + rails (= 3.1.0.rc4) responders (~> 0.6.0) ruby-debug diff --git a/test/test_helper.rb b/test/test_helper.rb index 33a7cabf..761820e6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -22,10 +22,6 @@ class ApplicationController < ActionController::Base; end $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'inherited_resources' -# Add app base to load path -$:.unshift File.expand_path(File.dirname(__FILE__) + '/../app/controllers') -require 'inherited_resources/base' - ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views') InheritedResources::Routes = ActionDispatch::Routing::RouteSet.new @@ -37,6 +33,10 @@ class ApplicationController < ActionController::Base; end ActionController::Base.send :include, InheritedResources::Routes.url_helpers +# Add app base to load path +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../app/controllers') +require 'inherited_resources/base' + class ActiveSupport::TestCase setup do @routes = InheritedResources::Routes diff --git a/test/url_helpers_test.rb b/test/url_helpers_test.rb index 486f97a4..39ecc38a 100644 --- a/test/url_helpers_test.rb +++ b/test/url_helpers_test.rb @@ -125,6 +125,10 @@ class ImageButtonsController < ButtonsController # Create a TestHelper module with some helpers class UrlHelpersTest < ActiveSupport::TestCase + def mock_polymorphic(controller, route) + controller.expects(:url_for).with(:no=>"no") + controller._routes.url_helpers.expects("hash_for_#{route}").returns({:no=>"no"}) + end def test_url_helpers_on_simple_inherited_resource controller = HousesController.new @@ -408,30 +412,30 @@ def test_url_helpers_on_polymorphic_belongs_to controller.instance_variable_set('@bed', bed) [:url, :path].each do |path_or_url| - controller.expects("house_beds_#{path_or_url}").with(house).once + mock_polymorphic(controller, "house_beds_#{path_or_url}").with(house).once controller.send("collection_#{path_or_url}") - controller.expects("house_bed_#{path_or_url}").with(house, bed).once + mock_polymorphic(controller, "house_bed_#{path_or_url}").with(house, bed).once controller.send("resource_#{path_or_url}") - controller.expects("new_house_bed_#{path_or_url}").with(house).once + mock_polymorphic(controller, "new_house_bed_#{path_or_url}").with(house).once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_house_bed_#{path_or_url}").with(house, bed).once + mock_polymorphic(controller, "edit_house_bed_#{path_or_url}").with(house, bed).once controller.send("edit_resource_#{path_or_url}") - controller.expects("house_#{path_or_url}").with(house).once + mock_polymorphic(controller, "house_#{path_or_url}").with(house).once controller.send("parent_#{path_or_url}") - controller.expects("edit_house_#{path_or_url}").with(house).once + mock_polymorphic(controller, "edit_house_#{path_or_url}").with(house).once controller.send("edit_parent_#{path_or_url}") end # With options - controller.expects("house_bed_url").with(house, bed, :page => 1).once + mock_polymorphic(controller, "house_bed_url").with(house, bed, :page => 1).once controller.send("resource_url", :page => 1) - controller.expects("house_url").with(house, :page => 1).once + mock_polymorphic(controller, "house_url").with(house, :page => 1).once controller.send("parent_url", :page => 1) # With args @@ -462,30 +466,30 @@ def test_url_helpers_on_polymorphic_belongs_to_using_uncountable controller.instance_variable_set('@sheep', sheep) [:url, :path].each do |path_or_url| - controller.expects("news_sheep_index_#{path_or_url}").with(news).once + mock_polymorphic(controller, "news_sheep_index_#{path_or_url}").with(news).once controller.send("collection_#{path_or_url}") - controller.expects("news_sheep_#{path_or_url}").with(news, sheep).once + mock_polymorphic(controller, "news_sheep_#{path_or_url}").with(news, sheep).once controller.send("resource_#{path_or_url}") - controller.expects("new_news_sheep_#{path_or_url}").with(news).once + mock_polymorphic(controller, "new_news_sheep_#{path_or_url}").with(news).once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_news_sheep_#{path_or_url}").with(news, sheep).once + mock_polymorphic(controller, "edit_news_sheep_#{path_or_url}").with(news, sheep).once controller.send("edit_resource_#{path_or_url}") - controller.expects("news_#{path_or_url}").with(news).once + mock_polymorphic(controller, "news_#{path_or_url}").with(news).once controller.send("parent_#{path_or_url}") - controller.expects("edit_news_#{path_or_url}").with(news).once + mock_polymorphic(controller, "edit_news_#{path_or_url}").with(news).once controller.send("edit_parent_#{path_or_url}") end # With options - controller.expects("news_sheep_url").with(news, sheep, :page => 1).once + mock_polymorphic(controller, "news_sheep_url").with(news, sheep, :page => 1).once controller.send("resource_url", :page => 1) - controller.expects("news_url").with(news, :page => 1).once + mock_polymorphic(controller, "news_url").with(news, :page => 1).once controller.send("parent_url", :page => 1) # With args @@ -516,30 +520,30 @@ def test_url_helpers_on_namespaced_polymorphic_belongs_to controller.instance_variable_set('@desk', desk) [:url, :path].each do |path_or_url| - controller.expects("admin_house_desks_#{path_or_url}").with(house).once + mock_polymorphic(controller, "admin_house_desks_#{path_or_url}").with(house).once controller.send("collection_#{path_or_url}") - controller.expects("admin_house_desk_#{path_or_url}").with(house, desk).once + mock_polymorphic(controller, "admin_house_desk_#{path_or_url}").with(house, desk).once controller.send("resource_#{path_or_url}") - controller.expects("new_admin_house_desk_#{path_or_url}").with(house).once + mock_polymorphic(controller, "new_admin_house_desk_#{path_or_url}").with(house).once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_admin_house_desk_#{path_or_url}").with(house, desk).once + mock_polymorphic(controller, "edit_admin_house_desk_#{path_or_url}").with(house, desk).once controller.send("edit_resource_#{path_or_url}") - controller.expects("admin_house_#{path_or_url}").with(house).once + mock_polymorphic(controller, "admin_house_#{path_or_url}").with(house).once controller.send("parent_#{path_or_url}") - controller.expects("edit_admin_house_#{path_or_url}").with(house).once + mock_polymorphic(controller, "edit_admin_house_#{path_or_url}").with(house).once controller.send("edit_parent_#{path_or_url}") end # With options - controller.expects("admin_house_desk_url").with(house, desk, :page => 1).once + mock_polymorphic(controller, "admin_house_desk_url").with(house, desk, :page => 1).once controller.send("resource_url", :page => 1) - controller.expects("admin_house_url").with(house, :page => 1).once + mock_polymorphic(controller, "admin_house_url").with(house, :page => 1).once controller.send("parent_url", :page => 1) # With args @@ -572,30 +576,30 @@ def test_url_helpers_on_nested_polymorphic_belongs_to controller.instance_variable_set('@dish', dish) [:url, :path].each do |path_or_url| - controller.expects("house_table_dishes_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "house_table_dishes_#{path_or_url}").with(house, table).once controller.send("collection_#{path_or_url}") - controller.expects("house_table_dish_#{path_or_url}").with(house, table, dish).once + mock_polymorphic(controller, "house_table_dish_#{path_or_url}").with(house, table, dish).once controller.send("resource_#{path_or_url}") - controller.expects("new_house_table_dish_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "new_house_table_dish_#{path_or_url}").with(house, table).once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_house_table_dish_#{path_or_url}").with(house, table, dish).once + mock_polymorphic(controller, "edit_house_table_dish_#{path_or_url}").with(house, table, dish).once controller.send("edit_resource_#{path_or_url}") - controller.expects("house_table_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "house_table_#{path_or_url}").with(house, table).once controller.send("parent_#{path_or_url}") - controller.expects("edit_house_table_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "edit_house_table_#{path_or_url}").with(house, table).once controller.send("edit_parent_#{path_or_url}") end # With options - controller.expects("house_table_dish_url").with(house, table, dish, :page => 1).once + mock_polymorphic(controller, "house_table_dish_url").with(house, table, dish, :page => 1).once controller.send("resource_url", :page => 1) - controller.expects("house_table_url").with(house, table, :page => 1).once + mock_polymorphic(controller, "house_table_url").with(house, table, :page => 1).once controller.send("parent_url", :page => 1) # With args @@ -624,30 +628,30 @@ def test_url_helpers_on_singleton_nested_polymorphic_belongs_to # controller.instance_variable_set('@center', :center) [:url, :path].each do |path_or_url| - controller.expects("house_table_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "house_table_#{path_or_url}").with(house, table).once controller.send("collection_#{path_or_url}") - controller.expects("house_table_center_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "house_table_center_#{path_or_url}").with(house, table).once controller.send("resource_#{path_or_url}") - controller.expects("new_house_table_center_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "new_house_table_center_#{path_or_url}").with(house, table).once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_house_table_center_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "edit_house_table_center_#{path_or_url}").with(house, table).once controller.send("edit_resource_#{path_or_url}") - controller.expects("house_table_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "house_table_#{path_or_url}").with(house, table).once controller.send("parent_#{path_or_url}") - controller.expects("edit_house_table_#{path_or_url}").with(house, table).once + mock_polymorphic(controller, "edit_house_table_#{path_or_url}").with(house, table).once controller.send("edit_parent_#{path_or_url}") end # With options - controller.expects("house_table_center_url").with(house, table, :page => 1) + mock_polymorphic(controller, "house_table_center_url").with(house, table, :page => 1) controller.send("resource_url", :page => 1) - controller.expects("house_table_url").with(house, table, :page => 1) + mock_polymorphic(controller, "house_table_url").with(house, table, :page => 1) controller.send("parent_url", :page => 1) # With args @@ -669,21 +673,21 @@ def test_url_helpers_on_optional_polymorphic_belongs_to controller.instance_variable_set('@bed', bed) [:url, :path].each do |path_or_url| - controller.expects("beds_#{path_or_url}").with().once + mock_polymorphic(controller, "beds_#{path_or_url}").with().once controller.send("collection_#{path_or_url}") - controller.expects("bed_#{path_or_url}").with(bed).once + mock_polymorphic(controller, "bed_#{path_or_url}").with(bed).once controller.send("resource_#{path_or_url}") - controller.expects("new_bed_#{path_or_url}").with().once + mock_polymorphic(controller, "new_bed_#{path_or_url}").with().once controller.send("new_resource_#{path_or_url}") - controller.expects("edit_bed_#{path_or_url}").with(bed).once + mock_polymorphic(controller, "edit_bed_#{path_or_url}").with(bed).once controller.send("edit_resource_#{path_or_url}") end # With options - controller.expects("bed_url").with(bed, :page => 1).once + mock_polymorphic(controller, "bed_url").with(bed, :page => 1).once controller.send("resource_url", :page => 1) # With args @@ -701,7 +705,6 @@ def test_url_helpers_on_belongs_to_with_shallowed_route controller.instance_variable_set('@mirror', :mirror) [:url, :path].each do |path_or_url| - controller.expects("house_mirrors_#{path_or_url}").with(:house, {}).once controller.send("collection_#{path_or_url}") From 7870f027e3b55991b3f9a474dcaa7d9bcfe2b79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 20 Jun 2011 11:15:48 -0300 Subject: [PATCH 09/12] Fix remaining tests. --- Gemfile.lock | 2 +- test/aliases_test.rb | 11 ++++++++++- test/base_test.rb | 18 +++++++++++++++++- test/test_helper.rb | 1 + 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0b6280c6..71151962 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -75,7 +75,7 @@ GEM thor (~> 0.14.6) rake (0.8.7) rdoc (3.6.1) - responders (0.6.2) + responders (0.6.4) ruby-debug (0.10.3) columnize (>= 0.1) ruby-debug-base (~> 0.10.3.0) diff --git a/test/aliases_test.rb b/test/aliases_test.rb index 82264666..bccedb59 100644 --- a/test/aliases_test.rb +++ b/test/aliases_test.rb @@ -126,7 +126,16 @@ def test_block_is_called_when_student_is_destroyed def test_options_are_used_in_respond_with @request.accept = "application/xml" - Student.stubs(:new).returns(mock_student(:save => true, :to_xml => "XML")) + mock_student = mock_student(:save => true, :to_xml => "XML") + Student.stubs(:new).returns(mock_student) + + # Bug in mocha does not accept strings on respond_to + mock_student.singleton_class.class_eval do + def respond_to?(method, *) + method == "to_xml" || super + end + end + post :create assert_equal "http://test.host/", @response.location end diff --git a/test/base_test.rb b/test/base_test.rb index 73aa5c5b..f22c6136 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -104,6 +104,14 @@ def test_render_exposed_user_as_xml_when_mime_type_is_xml @request.accept = 'application/xml' User.expects(:find).with('42').returns(mock_user) mock_user.expects(:to_xml).returns("Generated XML") + + # Bug in mocha does not accept strings on respond_to + mock_user.singleton_class.class_eval do + def respond_to?(method, *) + method == "to_xml" || super + end + end + get :show, :id => '42' assert_response :success assert_equal 'Generated XML', @response.body @@ -130,6 +138,14 @@ def test_render_exposed_a_new_user_as_xml_when_mime_type_is_xml @request.accept = 'application/xml' User.expects(:new).returns(mock_user) mock_user.expects(:to_xml).returns("Generated XML") + + # Bug in mocha does not accept strings on respond_to + mock_user.singleton_class.class_eval do + def respond_to?(method, *) + method == "to_xml" || super + end + end + get :new assert_response :success assert_equal 'Generated XML', @response.body @@ -206,7 +222,7 @@ def test_update_the_requested_object assert_equal mock_user, assigns(:user) end - def test_redirect_to_the_created_user + def test_redirect_to_the_updated_user User.stubs(:find).returns(mock_user(:update_attributes => true)) @controller.expects(:resource_url).returns('http://test.host/') put :update diff --git a/test/test_helper.rb b/test/test_helper.rb index 761820e6..31903b44 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -29,6 +29,7 @@ class ApplicationController < ActionController::Base; end match ':controller(/:action(/:id))' match ':controller(/:action)' resources 'posts' + root :to => 'posts#index' end ActionController::Base.send :include, InheritedResources::Routes.url_helpers From b21aa27b218aa831592a449e554a2361bfc39d6f Mon Sep 17 00:00:00 2001 From: Tomasz Bak Date: Fri, 24 Jun 2011 13:57:17 +0200 Subject: [PATCH 10/12] do not overwrite class_attribute :resource_class defined in controller --- app/controllers/inherited_resources/base.rb | 3 ++- lib/inherited_resources/class_methods.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/inherited_resources/base.rb b/app/controllers/inherited_resources/base.rb index 03f46ef1..db41fc4f 100644 --- a/app/controllers/inherited_resources/base.rb +++ b/app/controllers/inherited_resources/base.rb @@ -30,7 +30,8 @@ def self.inherit_resources(base) :parent_url, :parent_path, :smart_resource_url, :smart_collection_url - self.class_attribute :resource_class, :parents_symbols, :resources_configuration, :instance_writer => false + self.class_attribute :resource_class unless self.respond_to? :resource_class + self.class_attribute :parents_symbols, :resources_configuration, :instance_writer => false protected :resource_class, :parents_symbols, :resources_configuration, :resource_class?, :parents_symbols?, :resources_configuration? diff --git a/lib/inherited_resources/class_methods.rb b/lib/inherited_resources/class_methods.rb index 6446c5ff..c26274be 100644 --- a/lib/inherited_resources/class_methods.rb +++ b/lib/inherited_resources/class_methods.rb @@ -280,7 +280,7 @@ def acts_as_shallow! #:nodoc: # def initialize_resources_class_accessors! #:nodoc: # First priority is the namespaced modek, e.g. User::Group - self.resource_class = begin + self.resource_class ||= begin namespaced_class = self.name.sub(/Controller/, '').singularize namespaced_class.constantize rescue NameError From f4b2fca2993a0f6b6fc481376a24d36f875ea0d9 Mon Sep 17 00:00:00 2001 From: Tomasz Bak Date: Wed, 29 Jun 2011 18:02:46 +0200 Subject: [PATCH 11/12] fix mountable apps namespace issue --- lib/inherited_resources/class_methods.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/inherited_resources/class_methods.rb b/lib/inherited_resources/class_methods.rb index c26274be..64136119 100644 --- a/lib/inherited_resources/class_methods.rb +++ b/lib/inherited_resources/class_methods.rb @@ -279,7 +279,7 @@ def acts_as_shallow! #:nodoc: # Initialize resources class accessors and set their default values. # def initialize_resources_class_accessors! #:nodoc: - # First priority is the namespaced modek, e.g. User::Group + # First priority is the namespaced model, e.g. User::Group self.resource_class ||= begin namespaced_class = self.name.sub(/Controller/, '').singularize namespaced_class.constantize @@ -287,7 +287,16 @@ def initialize_resources_class_accessors! #:nodoc: nil end - # Second priority the camelcased c, i.e. UserGroup + # Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController + self.resource_class ||= begin + namespaced_classes = self.name.sub(/Controller/, '').split('::') + namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize + namespaced_class.constantize + rescue NameError + nil + end + + # Third priority the camelcased c, i.e. UserGroup self.resource_class ||= begin camelcased_class = self.name.sub(/Controller/, '').gsub('::', '').singularize camelcased_class.constantize From 79d4c1e00f890bed3807c1fa50fc179eec97c810 Mon Sep 17 00:00:00 2001 From: Ilya Zayats Date: Fri, 8 Jul 2011 20:19:36 +0400 Subject: [PATCH 12/12] added with_role method to support Rails 3.1 mass assignment --- lib/inherited_resources/base_helpers.rb | 18 +++++++++++++++--- lib/inherited_resources/class_methods.rb | 6 ++++++ test/base_test.rb | 17 +++++++++++++++++ test/customized_base_test.rb | 2 +- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/inherited_resources/base_helpers.rb b/lib/inherited_resources/base_helpers.rb index 5b8df76f..d0db37e6 100644 --- a/lib/inherited_resources/base_helpers.rb +++ b/lib/inherited_resources/base_helpers.rb @@ -49,7 +49,7 @@ def resource # instance variable. # def build_resource - get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, resource_params)) + get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, *resource_params)) end # Responsible for saving the resource on :create method. Overwriting this @@ -75,7 +75,7 @@ def create_resource(object) # end # def update_resource(object, attributes) - object.update_attributes(attributes) + object.update_attributes(*attributes) end # Handle the :destroy method for the resource. Overwrite it to call your @@ -300,7 +300,19 @@ def smart_collection_url # extract attributes from params def resource_params - params[resource_request_name] || params[resource_instance_name] || {} + rparams = [params[resource_request_name] || params[resource_instance_name] || {}] + rparams << as_role if role_given? + rparams + end + + # checking if role given + def role_given? + self.resources_configuration[:self][:role].present? + end + + # getting role for mass-asignment + def as_role + { :as => self.resources_configuration[:self][:role] } end end end diff --git a/lib/inherited_resources/class_methods.rb b/lib/inherited_resources/class_methods.rb index 6446c5ff..be914211 100644 --- a/lib/inherited_resources/class_methods.rb +++ b/lib/inherited_resources/class_methods.rb @@ -255,6 +255,12 @@ def custom_actions(options) end end + # Defines the role to use when creating or updating resource. + # Makes sense when using rails 3.1 mass assignment conventions + def with_role(role) + self.resources_configuration[:self][:role] = role.try(:to_sym) + end + private def acts_as_singleton! #:nodoc: diff --git a/test/base_test.rb b/test/base_test.rb index f22c6136..685a46e8 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -178,6 +178,14 @@ def test_expose_a_newly_create_user_when_saved_with_success post :create, :user => {:these => 'params'} assert_equal mock_user, assigns(:user) end + + def test_expose_a_newly_create_user_when_saved_with_success_and_role_setted + @controller.class.send(:with_role, :admin) + User.expects(:new).with({'these' => 'params'}, {:as => :admin}).returns(mock_user(:save => true)) + post :create, :user => {:these => 'params'} + assert_equal mock_user, assigns(:user) + @controller.class.send(:with_role, nil) + end def test_redirect_to_the_created_user User.stubs(:new).returns(mock_user(:save => true)) @@ -221,6 +229,15 @@ def test_update_the_requested_object put :update, :id => '42', :user => {:these => 'params'} assert_equal mock_user, assigns(:user) end + + def test_update_the_requested_object_when_setted_role + @controller.class.send(:with_role, :admin) + User.expects(:find).with('42').returns(mock_user) + mock_user.expects(:update_attributes).with({'these' => 'params'}, {:as => :admin}).returns(true) + put :update, :id => '42', :user => {:these => 'params'} + assert_equal mock_user, assigns(:user) + @controller.class.send(:with_role, nil) + end def test_redirect_to_the_updated_user User.stubs(:find).returns(mock_user(:update_attributes => true)) diff --git a/test/customized_base_test.rb b/test/customized_base_test.rb index a64e30ab..ac1dee26 100644 --- a/test/customized_base_test.rb +++ b/test/customized_base_test.rb @@ -26,7 +26,7 @@ def create_resource(resource) end def update_resource(resource, attributes) - resource.update_successfully(attributes) + resource.update_successfully(*attributes) end def destroy_resource(resource)