Allow scaffold controller to load action_orm files on demand.

This commit is contained in:
José Valim
2009-07-04 18:13:27 +02:00
parent 35925a8995
commit b277cf28e8
3 changed files with 14 additions and 4 deletions

View File

@@ -12,7 +12,6 @@ require 'rails/version' unless defined?(Rails::VERSION)
require 'generators/base'
require 'generators/named_base'
require 'generators/active_record' # We will need ActionORM from ActiveRecord, but just it.
module Rails
module Generators

View File

@@ -129,13 +129,24 @@ module Rails
#
def orm_class
@orm_class ||= begin
# Raise an error if the class_option :orm was not defined.
unless self.class.class_options[:orm]
raise "You need to have :orm as class option to invoke orm_class and orm_instance"
end
action_orm = "#{options[:orm].to_s.classify}::Generators::ActionORM"
action_orm.constantize
rescue NameError => e
# If the orm was not loaded, try to load it at "generators/orm",
# for example "generators/active_record" or "generators/sequel".
begin
klass = action_orm.constantize
rescue NameError
require "generators/#{options[:orm]}"
end
# Try once again after loading the file with success.
klass ||= action_orm.constantize
rescue Exception => e
raise Error, "Could not load #{action_orm}, skipping controller. Error: #{e.message}."
end
end

View File

@@ -100,7 +100,7 @@ class ScaffoldControllerGeneratorTest < GeneratorsTestCase
def test_error_is_shown_if_orm_does_not_provide_interface
error = capture(:stderr){ run_generator ["User", "--orm=unknown"] }
assert_equal "Could not load Unknown::Generators::ActionORM, skipping controller. " <<
"Error: uninitialized constant Unknown.\n", error
"Error: no such file to load -- generators/unknown.\n", error
end
protected