diff options
-rw-r--r-- | actionpack/lib/action_controller/metal/mime_responds.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/responder.rb | 10 | ||||
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 27 | ||||
-rw-r--r-- | actionpack/test/lib/controller/fake_models.rb | 12 | ||||
-rw-r--r-- | railties/lib/generators.rb | 90 | ||||
-rw-r--r-- | railties/lib/generators/active_record.rb | 1 | ||||
-rw-r--r-- | railties/test/fixtures/vendor/another_gem_path/xspec/lib/generators/xspec_generator.rb | 2 | ||||
-rw-r--r-- | railties/test/fixtures/vendor/plugins/mspec/lib/generators/mspec_generator.rb (renamed from railties/test/fixtures/vendor/gems/gems/mspec/lib/generators/mspec_generator.rb) | 0 | ||||
-rw-r--r-- | railties/test/generators/generators_test_helper.rb | 7 | ||||
-rw-r--r-- | railties/test/generators/session_migration_generator_test.rb | 13 | ||||
-rw-r--r-- | railties/test/generators_test.rb | 19 |
11 files changed, 100 insertions, 86 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index c8d042acb5..950105e63f 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -226,10 +226,11 @@ module ActionController #:nodoc: # is quite simple (it just needs to respond to call), you can even give # a proc to it. # - def respond_with(resource, options={}, &block) + def respond_with(*resources, &block) respond_to(&block) rescue ActionView::MissingTemplate - (options.delete(:responder) || responder).call(self, resource, options) + options = resources.extract_options! + (options.delete(:responder) || responder).call(self, resources, options) end def responder diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index 9ed99ca623..fc01a0924a 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -64,7 +64,7 @@ module ActionController #:nodoc: # @project = Project.find(params[:project_id]) # @task = @project.comments.build(params[:task]) # flash[:notice] = 'Task was successfully created.' if @task.save - # respond_with([@project, @task]) + # respond_with(@project, @task) # end # # Giving an array of resources, you ensure that the responder will redirect to @@ -74,19 +74,19 @@ module ActionController #:nodoc: # polymorphic urls. If a project has one manager which has many tasks, it # should be invoked as: # - # respond_with([@project, :manager, @task]) + # respond_with(@project, :manager, @task) # # Check polymorphic_url documentation for more examples. # class Responder attr_reader :controller, :request, :format, :resource, :resource_location, :options - def initialize(controller, resource, options={}) + def initialize(controller, resources, options={}) @controller = controller @request = controller.request @format = controller.formats.first - @resource = resource.is_a?(Array) ? resource.last : resource - @resource_location = options[:location] || resource + @resource = resources.is_a?(Array) ? resources.last : resources + @resource_location = options[:location] || resources @options = options end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 8319b5c573..2e2dba5aae 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -497,8 +497,12 @@ class RespondWithController < ActionController::Base respond_with(Customer.new("david", 13)) end + def using_resource_with_collection + respond_with([Customer.new("david", 13), Customer.new("jamis", 9)]) + end + def using_resource_with_parent - respond_with([Quiz::Store.new("developer?", 11), Customer.new("david", 13)]) + respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13)) end def using_resource_with_status_and_location @@ -506,7 +510,7 @@ class RespondWithController < ActionController::Base end def using_resource_with_responder - responder = proc { |c, r, o| c.render :text => "Resource name is #{r.name}" } + responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" } respond_with(Customer.new("david", 13), :responder => responder) end @@ -592,7 +596,7 @@ class RespondWithControllerTest < ActionController::TestCase @request.accept = "application/xml" get :using_resource assert_equal "application/xml", @response.content_type - assert_equal "XML", @response.body + assert_equal "<name>david</name>", @response.body @request.accept = "application/json" assert_raise ActionView::MissingTemplate do @@ -622,7 +626,7 @@ class RespondWithControllerTest < ActionController::TestCase post :using_resource assert_equal "application/xml", @response.content_type assert_equal 201, @response.status - assert_equal "XML", @response.body + assert_equal "<name>david</name>", @response.body assert_equal "http://www.example.com/customers/13", @response.location errors = { :name => :invalid } @@ -689,7 +693,7 @@ class RespondWithControllerTest < ActionController::TestCase get :using_resource_with_parent assert_equal "application/xml", @response.content_type assert_equal 200, @response.status - assert_equal "XML", @response.body + assert_equal "<name>david</name>", @response.body end def test_using_resource_with_parent_for_post @@ -698,7 +702,7 @@ class RespondWithControllerTest < ActionController::TestCase post :using_resource_with_parent assert_equal "application/xml", @response.content_type assert_equal 201, @response.status - assert_equal "XML", @response.body + assert_equal "<name>david</name>", @response.body assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location errors = { :name => :invalid } @@ -710,6 +714,15 @@ class RespondWithControllerTest < ActionController::TestCase assert_nil @response.location end + def test_using_resource_with_collection + @request.accept = "application/xml" + get :using_resource_with_collection + assert_equal "application/xml", @response.content_type + assert_equal 200, @response.status + assert_match /<name>david<\/name>/, @response.body + assert_match /<name>jamis<\/name>/, @response.body + end + def test_clear_respond_to @controller = InheritedRespondWithController.new @request.accept = "text/html" @@ -722,7 +735,7 @@ class RespondWithControllerTest < ActionController::TestCase @request.accept = "*/*" get :index assert_equal "application/xml", @response.content_type - assert_equal "XML", @response.body + assert_equal "<name>david</name>", @response.body end def test_no_double_render_is_raised diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index 0faf8f3f9a..18eff7516b 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -10,12 +10,16 @@ class Customer < Struct.new(:name, :id) id.to_s end - def to_xml - "XML" + def to_xml(options={}) + if options[:builder] + options[:builder].name name + else + "<name>#{name}</name>" + end end - def to_js - "JS" + def to_js(options={}) + "name: #{name.inspect}" end def errors diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb index c97c61507a..a2f462ae81 100644 --- a/railties/lib/generators.rb +++ b/railties/lib/generators.rb @@ -76,39 +76,37 @@ module Rails } def self.aliases #:nodoc: - @@aliases ||= DEFAULT_ALIASES.dup + @aliases ||= DEFAULT_ALIASES.dup end def self.options #:nodoc: - @@options ||= DEFAULT_OPTIONS.dup + @options ||= DEFAULT_OPTIONS.dup end - # Get paths only from loaded rubygems. In other words, to use rspec - # generators, you first have to ensure that rspec gem was already loaded. + # We have two scenarios here: when rubygems is loaded and when bundler is + # being used. If rubygems is loaded, we get all generators paths from loaded + # specs. Otherwise we just have to look into vendor/gems/gems. # - def self.rubygems_generators_paths + def self.gems_generators_paths paths = [] - return paths unless defined?(Gem) - Gem.loaded_specs.each do |name, spec| - generator_path = File.join(spec.full_gem_path, "lib/generators") - paths << generator_path if File.exist?(generator_path) + if defined?(Gem) && Gem.respond_to?(:loaded_specs) + Gem.loaded_specs.each do |name, spec| + generator_path = File.join(spec.full_gem_path, "lib/generators") + paths << generator_path if File.exist?(generator_path) + end + elsif defined?(RAILS_ROOT) + paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")] end paths end - # If RAILS_ROOT is defined, add vendor/gems, vendor/plugins and lib/generators - # paths. + # Load paths from plugin. # - def self.rails_root_generators_paths - paths = [] - if defined?(RAILS_ROOT) - paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")] - paths += Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")] - paths << File.join(RAILS_ROOT, "lib", "generators") - end - paths + def self.plugins_generators_paths + return [] unless defined?(RAILS_ROOT) + Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")] end # Hold configured generators fallbacks. If a plugin developer wants a @@ -125,7 +123,7 @@ module Rails # Rails::Generators.fallbacks[:shoulda] = :test_unit # def self.fallbacks - @@fallbacks ||= {} + @fallbacks ||= {} end # Remove the color from output. @@ -143,14 +141,17 @@ module Rails # 5) rubygems generators # 6) builtin generators # - # TODO Remove hardcoded paths for all, except (1). + # TODO Remove hardcoded paths for all, except (6). # def self.load_paths - @@load_paths ||= begin - paths = self.rails_root_generators_paths + @load_paths ||= begin + paths = [] + paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT) paths << File.join(Thor::Util.user_home, ".rails", "generators") - paths += self.rubygems_generators_paths + paths += self.plugins_generators_paths + paths += self.gems_generators_paths paths << File.expand_path(File.join(File.dirname(__FILE__), "generators")) + paths.uniq! paths end end @@ -279,39 +280,18 @@ module Rails end # Receives namespaces in an array and tries to find matching generators - # in the load path. Each path is traversed into directory lookups. For - # example: - # - # rails:generators:model - # - # Becomes: - # - # generators/rails/model/model_generator.rb - # generators/rails/model_generator.rb - # generators/model_generator.rb + # in the load path. # def self.lookup(attempts) #:nodoc: - attempts.each do |attempt| - generators_path = ['.'] - - paths = attempt.gsub(':generators:', ':').split(':') - name = "#{paths.last}_generator.rb" - - until paths.empty? - generators_path.unshift File.join(*paths) - paths.pop - end - - generators_path.uniq! - generators_path = "{#{generators_path.join(',')}}" - - self.load_paths.each do |path| - Dir[File.join(path, generators_path, name)].each do |file| - begin - require file - rescue Exception => e - warn "[WARNING] Could not load generator at #{file.inspect}. Error: #{e.message}" - end + attempts = attempts.map { |a| "#{a.split(":").last}_generator" }.uniq + attempts = "{#{attempts.join(',')}}.rb" + + self.load_paths.each do |path| + Dir[File.join(path, '**', attempts)].each do |file| + begin + require file + rescue Exception => e + warn "[WARNING] Could not load generator at #{file.inspect}. Error: #{e.message}" end end end diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 924b70881a..ff3093f356 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,7 +1,6 @@ require 'generators/named_base' require 'generators/migration' require 'generators/active_model' -require 'active_record' module ActiveRecord module Generators diff --git a/railties/test/fixtures/vendor/another_gem_path/xspec/lib/generators/xspec_generator.rb b/railties/test/fixtures/vendor/another_gem_path/xspec/lib/generators/xspec_generator.rb new file mode 100644 index 0000000000..cd477eb4c9 --- /dev/null +++ b/railties/test/fixtures/vendor/another_gem_path/xspec/lib/generators/xspec_generator.rb @@ -0,0 +1,2 @@ +class XspecGenerator < Rails::Generators::NamedBase +end diff --git a/railties/test/fixtures/vendor/gems/gems/mspec/lib/generators/mspec_generator.rb b/railties/test/fixtures/vendor/plugins/mspec/lib/generators/mspec_generator.rb index 191bdbf2fc..191bdbf2fc 100644 --- a/railties/test/fixtures/vendor/gems/gems/mspec/lib/generators/mspec_generator.rb +++ b/railties/test/fixtures/vendor/plugins/mspec/lib/generators/mspec_generator.rb diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 9444a9ed4b..a258574dce 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -8,9 +8,12 @@ else RAILS_ROOT = fixtures end -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../activerecord/lib" $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../activerecord/lib" +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../actionpack/lib" require 'generators' +require 'activerecord' +require 'action_dispatch' CURRENT_PATH = File.expand_path(Dir.pwd) Rails::Generators.no_color! @@ -19,7 +22,7 @@ class GeneratorsTestCase < Test::Unit::TestCase include FileUtils def destination_root - @destination_root ||= File.expand_path(File.join(File.dirname(__FILE__), + @destination_root ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'tmp')) end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb index 57bd755a9a..293b903b87 100644 --- a/railties/test/generators/session_migration_generator_test.rb +++ b/railties/test/generators/session_migration_generator_test.rb @@ -2,16 +2,6 @@ require 'abstract_unit' require 'generators/generators_test_helper' require 'generators/rails/session_migration/session_migration_generator' -module ActiveRecord - module SessionStore - class Session - class << self - attr_accessor :table_name - end - end - end -end - class SessionMigrationGeneratorTest < GeneratorsTestCase def test_session_migration_with_default_name @@ -31,7 +21,10 @@ class SessionMigrationGeneratorTest < GeneratorsTestCase assert_match /class AddSessionsTable < ActiveRecord::Migration/, migration assert_match /create_table :custom_table_name/, migration end + ensure + ActiveRecord::SessionStore::Session.table_name = "sessions" end + protected def run_generator(args=[]) diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 89d52dd170..4cc0b33521 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -4,6 +4,11 @@ require 'generators/test_unit/model/model_generator' require 'mocha' class GeneratorsTest < GeneratorsTestCase + def setup + Rails::Generators.instance_variable_set(:@load_paths, nil) + Gem.stubs(:respond_to?).with(:loaded_specs).returns(false) + end + def test_invoke_when_generator_is_not_found output = capture(:stdout){ Rails::Generators.invoke :unknown } assert_equal "Could not find generator unknown.\n", output @@ -70,6 +75,20 @@ class GeneratorsTest < GeneratorsTestCase assert_equal "mspec", klass.namespace end + def test_find_by_namespace_lookup_with_gem_specification + assert_nil Rails::Generators.find_by_namespace(:xspec) + Rails::Generators.instance_variable_set(:@load_paths, nil) + + spec = Gem::Specification.new + spec.expects(:full_gem_path).returns(File.join(RAILS_ROOT, 'vendor', 'another_gem_path', 'xspec')) + Gem.expects(:respond_to?).with(:loaded_specs).returns(true) + Gem.expects(:loaded_specs).returns(:spec => spec) + + klass = Rails::Generators.find_by_namespace(:xspec) + assert klass + assert_equal "xspec", klass.namespace + end + def test_builtin_generators assert Rails::Generators.builtin.include? %w(rails model) end |