aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-25 13:13:29 +0100
committerJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-25 13:20:32 +0100
commit9cb3ca1d29eb770c1a7adac3798666847fceee2b (patch)
tree70aeb2f12e00d98c13d7f55bdd9d445d4223fcfd
parentad8f5d4106c77ed1d054bf3b29bec94df557a7cd (diff)
downloadrails-9cb3ca1d29eb770c1a7adac3798666847fceee2b.tar.gz
rails-9cb3ca1d29eb770c1a7adac3798666847fceee2b.tar.bz2
rails-9cb3ca1d29eb770c1a7adac3798666847fceee2b.zip
Change mailer generator templates and refactor the whole naming schema.
-rw-r--r--railties/lib/generators/erb/mailer/templates/view.erb2
-rw-r--r--railties/lib/generators/rails/mailer/templates/mailer.rb19
-rw-r--r--railties/lib/generators/rails/resource/resource_generator.rb4
-rw-r--r--railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb6
-rw-r--r--railties/lib/generators/test_unit/mailer/templates/fixture2
-rw-r--r--railties/lib/generators/test_unit/mailer/templates/functional_test.rb12
-rw-r--r--railties/lib/rails/generators/named_base.rb79
-rw-r--r--railties/lib/rails/generators/resource_helpers.rb43
-rw-r--r--railties/test/generators/mailer_generator_test.rb40
-rw-r--r--railties/test/generators/named_base_test.rb62
10 files changed, 165 insertions, 104 deletions
diff --git a/railties/lib/generators/erb/mailer/templates/view.erb b/railties/lib/generators/erb/mailer/templates/view.erb
index fcce7bd805..6d597256a6 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 app/views/<%= @path %>
+<%%= @greeting %>, find me in app/views/<%= @path %>
diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb
index 90e0b712d6..5e7ef42370 100644
--- a/railties/lib/generators/rails/mailer/templates/mailer.rb
+++ b/railties/lib/generators/rails/mailer/templates/mailer.rb
@@ -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 \ No newline at end of file
diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb
index 43c7cc85f4..5acb839f39 100644
--- a/railties/lib/generators/rails/resource/resource_generator.rb
+++ b/railties/lib/generators/rails/resource/resource_generator.rb
@@ -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 => [],
diff --git a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
index e544e29892..49af2974cd 100644
--- a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
+++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -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
diff --git a/railties/lib/generators/test_unit/mailer/templates/fixture b/railties/lib/generators/test_unit/mailer/templates/fixture
index fcce7bd805..171648d6fd 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 app/views/<%= @path %>
+Hi, find me in app/views/<%= @path %>
diff --git a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb
index d7366fea5f..2f694e431c 100644
--- a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb
+++ b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb
@@ -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 -%>
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb
index 3e851bf888..12e918731e 100644
--- a/railties/lib/rails/generators/named_base.rb
+++ b/railties/lib/rails/generators/named_base.rb
@@ -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
+ def file_path
+ @file_path ||= (class_path + [file_name]).join('/')
+ end
+
+ 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
- if class_nesting.empty?
- @class_name = class_name_without_nesting
+ # 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
- @table_name = class_nesting.underscore << "_" << @table_name
- @class_name = "#{class_nesting}::#{class_name_without_nesting}"
+ "application"
end
+ end
- @table_name.gsub!('/', '_')
+ 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 hash into an array with GeneratedAttribute objects.
- #
+ # 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
diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb
index 7e00a222ed..3a98a8f9c1 100644
--- a/railties/lib/rails/generators/resource_helpers.rb
+++ b/railties/lib/rails/generators/resource_helpers.rb
@@ -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
+ end
- 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)
+ protected
+
+ attr_reader :controller_name
- @controller_class_name = if class_nesting.empty?
- class_name_without_nesting
- else
- "#{class_nesting}::#{class_name_without_nesting}"
+ def controller_class_path
+ @class_path
end
- end
- protected
+ 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
diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb
index dfc3130f77..0203eb314c 100644
--- a/railties/test/generators/mailer_generator_test.rb
+++ b/railties/test/generators/mailer_generator_test.rb
@@ -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
diff --git a/railties/test/generators/named_base_test.rb b/railties/test/generators/named_base_test.rb
index 99eb431a49..7514bc32bd 100644
--- a/railties/test/generators/named_base_test.rb
+++ b/railties/test/generators/named_base_test.rb
@@ -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