diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-25 15:45:15 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-25 15:45:15 +0200 |
commit | 0bb95968db3695467b63357aab66a9dddb62295c (patch) | |
tree | 408b2ff0c63ce874ca56a033915d47611925b358 | |
parent | d5bdf31d892136d59302adb14328db0417c70d76 (diff) | |
download | rails-0bb95968db3695467b63357aab66a9dddb62295c.tar.gz rails-0bb95968db3695467b63357aab66a9dddb62295c.tar.bz2 rails-0bb95968db3695467b63357aab66a9dddb62295c.zip |
More code refactoring.
7 files changed, 54 insertions, 36 deletions
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index c00ce9d342..e5727b8938 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -2,6 +2,11 @@ require 'generators/actions' module Rails module Generators + DEFAULTS = { + :test_framework => 'test_unit', + :template_engine => 'erb' + } + class Error < Thor::Error end @@ -113,39 +118,29 @@ module Rails end end - # Small macro to add test_framework option and invoke it. + # Invoke a generator based on the given name. If a class option does not + # exist for the current name, it's created. # - def self.add_and_invoke_test_framework_option! - class_option :test_framework, :type => :string, :aliases => "-t", :default => "test_unit", - :desc => "Test framework to be invoked by this generator", :banner => "NAME" - - define_method :invoke_test_framework do - return unless options[:test_framework] - name = "#{options[:test_framework]}:generators:#{self.class.generator_name}" - - begin - invoke name - rescue Thor::UndefinedTaskError - say "Could not find and invoke '#{name}'." + def self.invoke_for(*names) + names.each do |name| + unless class_options[name] + aliases = "-" + name.to_s.gsub(/_framework$/, '').split('_').last[0,1] + class_option name, :type => :string, :default => DEFAULTS[name], :banner => "NAME", :aliases => aliases, + :desc => "#{name.to_s.humanize} to be used" end - end - end - # Small macro to add template engine option and invoke it. - # - def self.add_and_invoke_template_engine_option! - class_option :template_engine, :type => :string, :aliases => "-e", :default => "erb", - :desc => "Template engine to be invoked by this generator", :banner => "NAME" - - define_method :invoke_template_engine do - return unless options[:template_engine] - name = "#{options[:template_engine]}:generators:#{self.class.generator_name}" - - begin - invoke name - rescue Thor::UndefinedTaskError - say "Could not find and invoke '#{name}'." - end + class_eval <<-METHOD, __FILE__, __LINE__ + def invoke_#{name} + return unless options[#{name.inspect}] + task = "\#{options[#{name.inspect}]}:generators:\#{self.class.generator_name}" + + begin + invoke task + rescue Thor::UndefinedTaskError + say "Could not find and invoke '\#{task}'." + end + end + METHOD end end diff --git a/railties/lib/generators/erb/mailer/templates/view.erb b/railties/lib/generators/erb/mailer/templates/view.erb index cda2cd24fe..fcce7bd805 100644 --- a/railties/lib/generators/erb/mailer/templates/view.erb +++ b/railties/lib/generators/erb/mailer/templates/view.erb @@ -1,3 +1,3 @@ <%= class_name %>#<%= @action %> -Find me in <%= @path %> +Find me in app/views/<%= @path %> diff --git a/railties/lib/generators/rails/mailer/mailer_generator.rb b/railties/lib/generators/rails/mailer/mailer_generator.rb index 4c77df89ab..15140f2127 100644 --- a/railties/lib/generators/rails/mailer/mailer_generator.rb +++ b/railties/lib/generators/rails/mailer/mailer_generator.rb @@ -11,8 +11,7 @@ module Rails template "mailer.rb", File.join('app/models', class_path, "#{file_name}.rb") end - add_and_invoke_template_engine_option! - add_and_invoke_test_framework_option! + invoke_for :template_engine, :test_framework end end end diff --git a/railties/lib/generators/rails/observer/observer_generator.rb b/railties/lib/generators/rails/observer/observer_generator.rb index e5e1be39dc..205ffc8064 100644 --- a/railties/lib/generators/rails/observer/observer_generator.rb +++ b/railties/lib/generators/rails/observer/observer_generator.rb @@ -9,7 +9,7 @@ module Rails template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb") end - add_and_invoke_test_framework_option! + invoke_for :test_framework end end end diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb index 4dbb3bfc0d..c3042afa94 100644 --- a/railties/lib/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/generators/rails/plugin/plugin_generator.rb @@ -27,7 +27,7 @@ module Rails directory 'lib' end - add_and_invoke_test_framework_option! + invoke_for :test_framework def create_tasks_files return unless options[:with_tasks] diff --git a/railties/lib/generators/test_unit/mailer/templates/fixture b/railties/lib/generators/test_unit/mailer/templates/fixture index cda2cd24fe..fcce7bd805 100644 --- a/railties/lib/generators/test_unit/mailer/templates/fixture +++ b/railties/lib/generators/test_unit/mailer/templates/fixture @@ -1,3 +1,3 @@ <%= class_name %>#<%= @action %> -Find me in <%= @path %> +Find me in app/views/<%= @path %> diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index c41f257e15..1e1c7cfb04 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -4,6 +4,16 @@ require 'generators/rails/app/app_generator' class AppGeneratorTest < GeneratorsTestCase + def setup + super + Rails::Generators::AppGenerator.instance_variable_set('@desc', nil) + end + + def teardown + super + Rails::Generators::AppGenerator.instance_variable_set('@desc', nil) + end + def test_application_skeleton_is_created run_generator @@ -120,6 +130,20 @@ class AppGeneratorTest < GeneratorsTestCase assert_match /It works!/, silence(:stdout){ generator.invoke(:all) } end + def test_usage_read_from_file + File.expects(:read).returns("USAGE FROM FILE") + assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc + end + + def test_default_usage + File.expects(:exist?).returns(false) + assert_match /Create rails files for app generator/, Rails::Generators::AppGenerator.desc + end + + def test_default_namespace + assert_match "rails:generators:app", Rails::Generators::AppGenerator.namespace + end + protected def run_generator(args=[]) |