Added HelperGenerator.

This commit is contained in:
José Valim
2009-06-26 09:53:53 +02:00
parent da1baeab4a
commit e084313640
9 changed files with 100 additions and 6 deletions

View File

@@ -13,15 +13,15 @@ module Rails
def initialize(*args)
super
assign_names!
assign_names!(self.name)
parse_attributes! if respond_to?(:attributes)
end
protected
def assign_names!
base_name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(name)
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
def assign_names!(given_name)
self.name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(given_name)
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(self.name)
@table_name = if !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
plural_name

View File

@@ -0,0 +1,24 @@
Description:
Stubs out a new helper. Pass the helper name, either
CamelCased or under_scored.
To create a helper within a module, specify the helper name as a
path like 'parent_module/helper_name'.
This generates a helper class in app/helpers and invokes your test
framework.
Example:
`./script/generate helper CreditCard`
Credit card helper.
Helper: app/helpers/credit_card_helper.rb
Test: test/unit/helpers/credit_card_helper_test.rb
Modules Example:
`./script/generate helper 'admin/credit_card'`
Credit card admin helper.
Helper: app/helpers/admin/credit_card_helper.rb
Test: test/unit/helpers/admin/credit_card_helper_test.rb

View File

@@ -0,0 +1,15 @@
module Rails
module Generators
class HelperGenerator < NamedBase
def check_class_collisions
class_collisions "#{class_name}Helper"
end
def create_helper_files
template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb")
end
invoke_for :test_framework
end
end
end

View File

@@ -0,0 +1,2 @@
module <%= class_name %>Helper
end

View File

@@ -0,0 +1,13 @@
module TestUnit
module Generators
class HelperGenerator < Base
def check_class_collisions
class_collisions "#{class_name}Helper"
end
def create_helper_files
template 'helper_test.rb', File.join('test/unit/helpers', class_path, "#{file_name}_helper_test.rb")
end
end
end
end

View File

@@ -0,0 +1,4 @@
require 'test_helper'
class <%= class_name %>HelperTest < ActionView::TestCase
end

View File

@@ -0,0 +1,36 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/rails/helper/helper_generator'
require 'generators/test_unit/helper/helper_generator'
ObjectHelper = Class.new
class HelperGeneratorTest < GeneratorsTestCase
def test_helper_skeleton_is_created
run_generator
assert_file "app/helpers/admin_helper.rb", /module AdminHelper/
end
def test_invokes_default_test_framework
run_generator
assert_file "test/unit/helpers/admin_helper_test.rb"
end
def test_logs_if_the_test_framework_cannot_be_found
content = run_generator ["admin", "--test-framework=unknown"]
assert_match /Could not find and invoke 'unknown:generators:helper'/, content
end
def test_check_class_collision
content = capture(:stderr){ run_generator ["object"] }
assert_match /The name 'ObjectHelper' is either already used in your application or reserved/, content
end
protected
def run_generator(args=["admin"])
silence(:stdout) { Rails::Generators::HelperGenerator.start args, :root => destination_root }
end
end

View File

@@ -6,7 +6,7 @@ class IntegrationTestGeneratorTest < GeneratorsTestCase
def test_integration_test_skeleton_is_created
run_generator
assert_file "test/integration/integration_test.rb"
assert_file "test/integration/integration_test.rb", /class IntegrationTest < ActionController::IntegrationTest/
end
protected

View File

@@ -6,7 +6,7 @@ class PerformanceTestGeneratorTest < GeneratorsTestCase
def test_performance_test_skeleton_is_created
run_generator
assert_file "test/performance/performance_test.rb"
assert_file "test/performance/performance_test.rb", /class PerformanceTest < ActionController::PerformanceTest/
end
protected