Change mailer generator templates and refactor the whole naming schema.

This commit is contained in:
José Valim and Mikel Lindsaar
2010-01-25 13:13:29 +01:00
parent ad8f5d4106
commit 9cb3ca1d29
10 changed files with 171 additions and 110 deletions

View File

@@ -1,3 +1,3 @@
<%= class_name %>#<%= @action %>
Find me in app/views/<%= @path %>
<%%= @greeting %>, find me in app/views/<%= @path %>

View File

@@ -1,14 +1,15 @@
class <%= class_name %> < ActionMailer::Base
delivers_from "mail@<%= application_name %>.com"
<% for action in actions -%>
def <%= action %>(sent_at = Time.now)
subject '<%= class_name %>#<%= action %>'
recipients ''
from ''
sent_on sent_at
body :greeting => 'Hi,'
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.actionmailer.<%= file_name %>.<%= action %>.subject
#
def <%= action %>
@greeting = "Hi"
mail(:to => "")
end
<% end -%>
end
end

View File

@@ -6,8 +6,8 @@ module Rails
class ResourceGenerator < ModelGenerator #metagenerator
include ResourceHelpers
hook_for :resource_controller, :required => true do |base, controller|
base.invoke controller, [ base.controller_name, base.options[:actions] ]
hook_for :resource_controller, :required => true do |controller|
invoke controller, [ controller_name, options[:actions] ]
end
class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [],

View File

@@ -18,9 +18,9 @@ module Rails
hook_for :template_engine, :test_framework, :as => :scaffold
# Invoke the helper using the controller (pluralized) name.
hook_for :helper, :as => :scaffold do |base, invoked|
base.invoke invoked, [ base.controller_name ]
# Invoke the helper using the controller name (pluralized)
hook_for :helper, :as => :scaffold do |invoked|
invoke invoked, [ controller_name ]
end
end
end

View File

@@ -1,3 +1,3 @@
<%= class_name %>#<%= @action %>
Find me in app/views/<%= @path %>
Hi, find me in app/views/<%= @path %>

View File

@@ -3,11 +3,17 @@ require 'test_helper'
class <%= class_name %>Test < ActionMailer::TestCase
<% for action in actions -%>
test "<%= action %>" do
@expected.subject = '<%= class_name %>#<%= action %>'
@expected.body = read_fixture('<%= action %>')
@actual = <%= class_name %>.<%= action %>
@expected.subject = <%= action.to_s.humanize.inspect %>
@expected.body = read_fixture("<%= action %>")
@expected.date = Time.now
assert_equal @expected, <%= class_name %>.create_<%= action %>(@expected.date)
assert_difference "<%= class_name %>.deliveries.size" do
@actual.deliver
end
assert_equal @expected.encoded, @actual.encoded
end
<% end -%>

View File

@@ -6,17 +6,9 @@ module Rails
class NamedBase < Base
argument :name, :type => :string
no_tasks {
attr_reader :class_name, :singular_name, :plural_name, :table_name,
:class_path, :file_path, :class_nesting_depth
alias :file_name :singular_name
}
def initialize(args, *options) #:nodoc:
# Unfreeze name in case it's given as a frozen string
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
super
assign_names!(self.name)
parse_attributes! if respond_to?(:attributes)
@@ -24,28 +16,48 @@ module Rails
protected
def assign_names!(given_name) #:nodoc:
base_name, @class_path, @file_path, class_nesting, @class_nesting_depth = extract_modules(given_name)
class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
attr_reader :class_path, :file_name
alias :singular_name :file_name
@table_name = if pluralize_table_names?
plural_name
else
singular_name
end
if class_nesting.empty?
@class_name = class_name_without_nesting
else
@table_name = class_nesting.underscore << "_" << @table_name
@class_name = "#{class_nesting}::#{class_name_without_nesting}"
end
@table_name.gsub!('/', '_')
def file_path
@file_path ||= (class_path + [file_name]).join('/')
end
# Convert attributes hash into an array with GeneratedAttribute objects.
#
def class_name
@class_name ||= (class_path + [file_name]).map!{ |m| m.camelize }.join('::')
end
def plural_name
@plural_name ||= singular_name.pluralize
end
def i18n_scope
@i18n_scope ||= file_path.gsub('/', '.')
end
def table_name
@table_name ||= begin
base = pluralize_table_names? ? plural_name : singular_name
(class_path + [base]).join('_')
end
end
# Tries to retrieve the application name or simple return application.
def application_name
if defined?(Rails) && Rails.application
Rails.application.class.name.split('::').first.underscore
else
"application"
end
end
def assign_names!(name) #:nodoc:
@class_path = name.include?('/') ? name.split('/') : name.split('::')
@class_path.map! { |m| m.underscore }
@file_name = @class_path.pop
end
# Convert attributes array into GeneratedAttribute objects.
def parse_attributes! #:nodoc:
self.attributes = (attributes || []).map do |key_value|
name, type = key_value.split(':')
@@ -53,29 +65,6 @@ module Rails
end
end
# Extract modules from filesystem-style or ruby-style path. Both
# good/fun/stuff and Good::Fun::Stuff produce the same results.
#
def extract_modules(name) #:nodoc:
modules = name.include?('/') ? name.split('/') : name.split('::')
name = modules.pop
path = modules.map { |m| m.underscore }
file_path = (path + [name.underscore]).join('/')
nesting = modules.map { |m| m.camelize }.join('::')
[name, path, file_path, nesting, modules.size]
end
# Receives name and return camelized, underscored and pluralized names.
#
def inflect_names(name) #:nodoc:
camel = name.camelize
under = camel.underscore
plural = under.pluralize
[camel, under, plural]
end
def pluralize_table_names?
!defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
end

View File

@@ -9,14 +9,7 @@ module Rails
mattr_accessor :skip_warn
def self.included(base) #:nodoc:
base.class_eval do
class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
no_tasks {
attr_reader :controller_name, :controller_class_name, :controller_file_name,
:controller_class_path, :controller_file_path
}
end
base.class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
end
# Set controller variables on initialization.
@@ -29,29 +22,40 @@ module Rails
say "Plural version of the model detected, using singularized version. Override with --force-plural."
ResourceHelpers.skip_warn = true
end
name.replace name.singularize
assign_names!(self.name)
assign_names!(name)
end
@controller_name = name.pluralize
base_name, @controller_class_path, @controller_file_path, class_nesting, class_nesting_depth = extract_modules(@controller_name)
class_name_without_nesting, @controller_file_name, controller_plural_name = inflect_names(base_name)
@controller_class_name = if class_nesting.empty?
class_name_without_nesting
else
"#{class_nesting}::#{class_name_without_nesting}"
end
end
protected
attr_reader :controller_name
def controller_class_path
@class_path
end
def controller_file_name
@controller_file_name ||= file_name.pluralize
end
def controller_file_path
@controller_file_path ||= (controller_class_path + [controller_file_name]).join('/')
end
def controller_class_name
@controller_class_name ||= (controller_class_path + [controller_file_name]).map!{ |m| m.camelize }.join('::')
end
def controller_i18n_scope
@controller_i18n_scope ||= controller_file_path.gsub('/', '.')
end
# Loads the ORM::Generators::ActiveModel class. This class is responsable
# to tell scaffold entities how to generate an specific method for the
# ORM. Check Rails::Generators::ActiveModel for more information.
#
def orm_class
@orm_class ||= begin
# Raise an error if the class_option :orm was not defined.
@@ -68,7 +72,6 @@ module Rails
end
# Initialize ORM::Generators::ActiveModel to access instance methods.
#
def orm_instance(name=file_name)
@orm_instance ||= @orm_class.new(name)
end

View File

@@ -6,8 +6,20 @@ class MailerGeneratorTest < Rails::Generators::TestCase
arguments %w(notifier foo bar)
def test_mailer_skeleton_is_created
Rails.stubs(:application).returns(Object.new)
run_generator
assert_file "app/mailers/notifier.rb", /class Notifier < ActionMailer::Base/
assert_file "app/mailers/notifier.rb" do |mailer|
assert_match /class Notifier < ActionMailer::Base/, mailer
assert_match /delivers_from "mail@object.com"/, mailer
end
end
def test_mailer_with_i18n_helper
run_generator
assert_file "app/mailers/notifier.rb" do |mailer|
assert_match /en\.actionmailer\.notifier\.foo\.subject/, mailer
assert_match /en\.actionmailer\.notifier\.bar\.subject/, mailer
end
end
def test_check_class_collision
@@ -24,8 +36,15 @@ class MailerGeneratorTest < Rails::Generators::TestCase
def test_invokes_default_template_engine
run_generator
assert_file "app/views/notifier/foo.erb", /app\/views\/notifier\/foo/
assert_file "app/views/notifier/bar.erb", /app\/views\/notifier\/bar/
assert_file "app/views/notifier/foo.erb" do |view|
assert_match /app\/views\/notifier\/foo/, view
assert_match /<%= @greeting %>/, view
end
assert_file "app/views/notifier/bar.erb" do |view|
assert_match /app\/views\/notifier\/bar/, view
assert_match /<%= @greeting %>/, view
end
end
def test_invokes_default_template_engine_even_with_no_action
@@ -40,7 +59,18 @@ class MailerGeneratorTest < Rails::Generators::TestCase
def test_actions_are_turned_into_methods
run_generator
assert_file "app/mailers/notifier.rb", /def foo/
assert_file "app/mailers/notifier.rb", /def bar/
assert_file "app/mailers/notifier.rb" do |mailer|
assert_instance_method :foo, mailer do |foo|
assert_match /mail\(:to => ""\)/, foo
assert_match /@greeting = "Hi"/, foo
end
assert_instance_method :bar, mailer do |bar|
assert_match /mail\(:to => ""\)/, bar
assert_match /@greeting = "Hi"/, bar
end
end
end
end

View File

@@ -16,28 +16,60 @@ class NamedBaseTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldControllerGenerator
def test_named_generator_attributes
g = generator ["admin/foo"]
assert_equal 'admin/foo', g.name
assert_equal %w(admin), g.class_path
assert_equal 1, g.class_nesting_depth
assert_equal 'Admin::Foo', g.class_name
assert_equal 'foo', g.singular_name
assert_equal 'foos', g.plural_name
assert_equal g.singular_name, g.file_name
assert_equal "admin_#{g.plural_name}", g.table_name
g = generator ['admin/foo']
assert_name g, 'admin/foo', :name
assert_name g, %w(admin), :class_path
assert_name g, 'Admin::Foo', :class_name
assert_name g, 'admin/foo', :file_path
assert_name g, 'foo', :file_name
assert_name g, 'foo', :singular_name
assert_name g, 'foos', :plural_name
assert_name g, 'admin.foo', :i18n_scope
assert_name g, 'admin_foos', :table_name
end
def test_named_generator_attributes_as_ruby
g = generator ['Admin::Foo']
assert_name g, 'Admin::Foo', :name
assert_name g, %w(admin), :class_path
assert_name g, 'Admin::Foo', :class_name
assert_name g, 'admin/foo', :file_path
assert_name g, 'foo', :file_name
assert_name g, 'foo', :singular_name
assert_name g, 'foos', :plural_name
assert_name g, 'admin.foo', :i18n_scope
assert_name g, 'admin_foos', :table_name
end
def test_named_generator_attributes_without_pluralized
ActiveRecord::Base.pluralize_table_names = false
g = generator ["admin/foo"]
assert_equal "admin_#{g.singular_name}", g.table_name
g = generator ['admin/foo']
assert_name g, 'admin_foo', :table_name
end
def test_scaffold_plural_names
g = generator ["ProductLine"]
assert_equal "ProductLines", g.controller_name
assert_equal "ProductLines", g.controller_class_name
assert_equal "product_lines", g.controller_file_name
g = generator ['admin/foo']
assert_name g, 'admin/foos', :controller_name
assert_name g, %w(admin), :controller_class_path
assert_name g, 'Admin::Foos', :controller_class_name
assert_name g, 'admin/foos', :controller_file_path
assert_name g, 'foos', :controller_file_name
assert_name g, 'admin.foos', :controller_i18n_scope
end
def test_scaffold_plural_names_as_ruby
g = generator ['Admin::Foo']
assert_name g, 'Admin::Foos', :controller_name
assert_name g, %w(admin), :controller_class_path
assert_name g, 'Admin::Foos', :controller_class_name
assert_name g, 'admin/foos', :controller_file_path
assert_name g, 'foos', :controller_file_name
assert_name g, 'admin.foos', :controller_i18n_scope
end
protected
def assert_name(generator, value, method)
assert_equal value, generator.send(method)
end
end