aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/generators
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/generators')
-rw-r--r--railties/lib/generators/actions.rb12
-rw-r--r--railties/lib/generators/base.rb52
-rw-r--r--railties/lib/generators/erb/scaffold/scaffold_generator.rb5
-rw-r--r--railties/lib/generators/erb/scaffold/templates/_form.html.erb17
-rw-r--r--railties/lib/generators/erb/scaffold/templates/edit.html.erb16
-rw-r--r--railties/lib/generators/erb/scaffold/templates/layout.html.erb2
-rw-r--r--railties/lib/generators/erb/scaffold/templates/new.html.erb16
-rw-r--r--railties/lib/generators/named_base.rb62
-rw-r--r--railties/lib/generators/rails/generator/generator_generator.rb2
-rw-r--r--railties/lib/generators/rails/resource/resource_generator.rb16
-rw-r--r--railties/lib/generators/rails/scaffold/scaffold_generator.rb3
-rw-r--r--railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb5
-rw-r--r--railties/lib/generators/rails/stylesheets/templates/scaffold.css8
-rw-r--r--railties/lib/generators/resource_helpers.rb74
-rw-r--r--railties/lib/generators/test_unit/scaffold/scaffold_generator.rb3
15 files changed, 173 insertions, 120 deletions
diff --git a/railties/lib/generators/actions.rb b/railties/lib/generators/actions.rb
index 03d0d11a07..c4552dd399 100644
--- a/railties/lib/generators/actions.rb
+++ b/railties/lib/generators/actions.rb
@@ -89,13 +89,11 @@ module Rails
# git :add => "onefile.rb", :rm => "badfile.cxx"
#
def git(command={})
- in_root do
- if command.is_a?(Symbol)
- run "git #{command}"
- else
- command.each do |command, options|
- run "git #{command} #{options}"
- end
+ if command.is_a?(Symbol)
+ run "git #{command}"
+ else
+ command.each do |command, options|
+ run "git #{command} #{options}"
end
end
end
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb
index cbe9c0a49d..c5d769b6ed 100644
--- a/railties/lib/generators/base.rb
+++ b/railties/lib/generators/base.rb
@@ -9,6 +9,8 @@ module Rails
include Thor::Actions
include Rails::Generators::Actions
+ add_runtime_options!
+
# Automatically sets the source root based on the class name.
#
def self.source_root
@@ -45,8 +47,10 @@ module Rails
#
# ==== Examples
#
- # class ControllerGenerator < Rails::Generators::Base
- # hook_for :test_framework, :aliases => "-t"
+ # module Rails::Generators
+ # class ControllerGenerator < Base
+ # hook_for :test_framework, :aliases => "-t"
+ # end
# end
#
# The example above will create a test framework option and will invoke
@@ -64,7 +68,49 @@ module Rails
# invoked. This allows any test framework to hook into Rails as long as it
# provides any of the hooks above.
#
- # Finally, if the user don't want to use any test framework, he can do:
+ # ==== Options
+ #
+ # This lookup can be customized with two options: :base and :as. The first
+ # is the root module value and in the example above defaults to "rails".
+ # The later defaults to the generator name, without the "Generator" ending.
+ #
+ # Let's suppose you are creating a generator that needs to invoke the
+ # controller generator from test unit. Your first attempt is:
+ #
+ # class AwesomeGenerator < Rails::Generators::Base
+ # hook_for :test_framework
+ # end
+ #
+ # The lookup in this case for test_unit as input is:
+ #
+ # "test_unit:generators:awesome", "test_unit"
+ #
+ # Which is not the desired the lookup. You can change it by providing the
+ # :as option:
+ #
+ # class AwesomeGenerator < Rails::Generators::Base
+ # hook_for :test_framework, :as => :controller
+ # end
+ #
+ # And now it will lookup at:
+ #
+ # "test_unit:generators:awesome", "test_unit"
+ #
+ # Similarly, if you want it to also lookup in the rails namespace, you just
+ # need to provide the :base value:
+ #
+ # class AwesomeGenerator < Rails::Generators::Base
+ # hook_for :test_framework, :base => :rails, :as => :controller
+ # end
+ #
+ # And the lookup is exactly the same as previously:
+ #
+ # "rails:generators:test_unit", "test_unit:generators:controller", "test_unit"
+ #
+ # ==== Switches
+ #
+ # All hooks come with switches for user interface. If the user don't want
+ # to use any test framework, he can do:
#
# ruby script/generate controller Account --skip-test-framework
#
diff --git a/railties/lib/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/generators/erb/scaffold/scaffold_generator.rb
index 955f22192a..d51dc7d725 100644
--- a/railties/lib/generators/erb/scaffold/scaffold_generator.rb
+++ b/railties/lib/generators/erb/scaffold/scaffold_generator.rb
@@ -1,13 +1,13 @@
require 'generators/erb'
+require 'generators/resource_helpers'
module Erb
module Generators
class ScaffoldGenerator < Base
- include Rails::Generators::ScaffoldBase
+ include Rails::Generators::ResourceHelpers
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
- class_option :form, :type => :boolean
class_option :layout, :type => :boolean
class_option :singleton, :type => :boolean, :desc => "Supply to skip index view"
@@ -33,7 +33,6 @@ module Erb
end
def copy_form_file
- return unless options[:form]
copy_view :_form
end
diff --git a/railties/lib/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/generators/erb/scaffold/templates/_form.html.erb
new file mode 100644
index 0000000000..d02028d983
--- /dev/null
+++ b/railties/lib/generators/erb/scaffold/templates/_form.html.erb
@@ -0,0 +1,17 @@
+<%% form_for(@<%= singular_name %>) do |f| %>
+ <%%= f.error_messages %>
+
+<% for attribute in attributes -%>
+ <div class="field">
+ <%%= f.label :<%= attribute.name %> %><br />
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
+ </div>
+<% end -%>
+ <div class="actions">
+ <%% if @<%= singular_name %>.new_record? %>
+ <%%= f.submit 'Create' %>
+ <%% else %>
+ <%%= f.submit 'Update' %>
+ <%% end %>
+ </div>
+<%% end %>
diff --git a/railties/lib/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/generators/erb/scaffold/templates/edit.html.erb
index cca1d61c68..5bc507ffc8 100644
--- a/railties/lib/generators/erb/scaffold/templates/edit.html.erb
+++ b/railties/lib/generators/erb/scaffold/templates/edit.html.erb
@@ -1,18 +1,6 @@
<h1>Editing <%= singular_name %></h1>
-<%% form_for(@<%= singular_name %>) do |f| %>
- <%%= f.error_messages %>
-
-<% for attribute in attributes -%>
- <p>
- <%%= f.label :<%= attribute.name %> %><br />
- <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
- </p>
-<% end -%>
- <p>
- <%%= f.submit 'Update' %>
- </p>
-<%% end %>
+<%%= render 'form' %>
<%%= link_to 'Show', @<%= singular_name %> %> |
-<%%= link_to 'Back', <%= plural_name %>_path %> \ No newline at end of file
+<%%= link_to 'Back', <%= plural_name %>_path %>
diff --git a/railties/lib/generators/erb/scaffold/templates/layout.html.erb b/railties/lib/generators/erb/scaffold/templates/layout.html.erb
index aacfbe4a8f..6460e5b599 100644
--- a/railties/lib/generators/erb/scaffold/templates/layout.html.erb
+++ b/railties/lib/generators/erb/scaffold/templates/layout.html.erb
@@ -7,7 +7,7 @@
</head>
<body>
-<p style="color: green"><%%= flash[:notice] %></p>
+<p class="notice"><%%= flash[:notice] %></p>
<%%= yield %>
diff --git a/railties/lib/generators/erb/scaffold/templates/new.html.erb b/railties/lib/generators/erb/scaffold/templates/new.html.erb
index 96c89fc50e..9a1c489331 100644
--- a/railties/lib/generators/erb/scaffold/templates/new.html.erb
+++ b/railties/lib/generators/erb/scaffold/templates/new.html.erb
@@ -1,17 +1,5 @@
<h1>New <%= singular_name %></h1>
-<%% form_for(@<%= singular_name %>) do |f| %>
- <%%= f.error_messages %>
+<%%= render 'form' %>
-<% for attribute in attributes -%>
- <p>
- <%%= f.label :<%= attribute.name %> %><br />
- <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
- </p>
-<% end -%>
- <p>
- <%%= f.submit 'Create' %>
- </p>
-<%% end %>
-
-<%%= link_to 'Back', <%= plural_name %>_path %> \ No newline at end of file
+<%%= link_to 'Back', <%= plural_name %>_path %>
diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb
index cd7aa61b50..b6ac05f482 100644
--- a/railties/lib/generators/named_base.rb
+++ b/railties/lib/generators/named_base.rb
@@ -97,67 +97,5 @@ module Rails
end
end
end
-
- # Deal with controller names on scaffold. Also provide helpers to deal with
- # ActionORM.
- #
- module ScaffoldBase
- def self.included(base) #:nodoc:
- base.send :attr_reader, :controller_name, :controller_class_name, :controller_file_name,
- :controller_class_path, :controller_file_path
- end
-
- # Set controller variables on initialization.
- #
- def initialize(*args) #:nodoc:
- super
- @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
-
- # 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.
- 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::ActiveModel"
-
- # 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
-
- # Initialize ORM::Generators::ActiveModel to access instance methods.
- #
- def orm_instance(name=file_name)
- @orm_instance ||= @orm_class.new(name)
- end
- end
end
end
diff --git a/railties/lib/generators/rails/generator/generator_generator.rb b/railties/lib/generators/rails/generator/generator_generator.rb
index 2fc97b20d3..5b5d1884bc 100644
--- a/railties/lib/generators/rails/generator/generator_generator.rb
+++ b/railties/lib/generators/rails/generator/generator_generator.rb
@@ -6,7 +6,7 @@ module Rails
class_option :namespace, :type => :boolean, :default => true,
:desc => "Namespace generator under lib/generators/name"
- def craete_generator_files
+ def create_generator_files
directory '.', generator_dir
end
diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb
index 70babc0550..9abb8bbeaf 100644
--- a/railties/lib/generators/rails/resource/resource_generator.rb
+++ b/railties/lib/generators/rails/resource/resource_generator.rb
@@ -1,25 +1,19 @@
require 'generators/rails/model/model_generator'
+require 'generators/resource_helpers'
module Rails
module Generators
class ResourceGenerator < ModelGenerator #metagenerator
+ include ResourceHelpers
+
hook_for :resource_controller, :required => true do |base, controller|
- base.invoke controller, [ base.name.pluralize, base.options[:actions] ]
+ base.invoke controller, [ base.controller_name, base.options[:actions] ]
end
class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [],
:desc => "Actions for the resource controller"
- class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
- class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
-
- def initialize(*args)
- super
- if name == name.pluralize && !options[:force_plural]
- say "Plural version of the model detected, using singularized version. Override with --force-plural."
- name.replace name.singularize
- end
- end
+ class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
def add_resource_route
route "map.resource#{:s unless options[:singleton]} :#{pluralize?(file_name)}"
diff --git a/railties/lib/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/generators/rails/scaffold/scaffold_generator.rb
index af44c8ba65..fdea5bf52b 100644
--- a/railties/lib/generators/rails/scaffold/scaffold_generator.rb
+++ b/railties/lib/generators/rails/scaffold/scaffold_generator.rb
@@ -3,7 +3,8 @@ require 'generators/rails/resource/resource_generator'
module Rails
module Generators
class ScaffoldGenerator < ResourceGenerator #metagenerator
- remove_hook_for :actions, :resource_controller
+ remove_hook_for :resource_controller
+ remove_class_option :actions
hook_for :scaffold_controller, :required => true
hook_for :stylesheets
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 972be5a33b..228cdecb14 100644
--- a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
+++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -1,8 +1,9 @@
+require 'generators/resource_helpers'
+
module Rails
module Generators
class ScaffoldControllerGenerator < NamedBase
- # Add controller methods and ActionORM settings.
- include ScaffoldBase
+ include ResourceHelpers
check_class_collision :suffix => "Controller"
diff --git a/railties/lib/generators/rails/stylesheets/templates/scaffold.css b/railties/lib/generators/rails/stylesheets/templates/scaffold.css
index 093c20994d..d9fa2cf2dc 100644
--- a/railties/lib/generators/rails/stylesheets/templates/scaffold.css
+++ b/railties/lib/generators/rails/stylesheets/templates/scaffold.css
@@ -16,6 +16,14 @@ a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
+div.field, div.actions {
+ margin-bottom: 10px;
+}
+
+.notice {
+ color: green;
+}
+
.fieldWithErrors {
padding: 2px;
background-color: red;
diff --git a/railties/lib/generators/resource_helpers.rb b/railties/lib/generators/resource_helpers.rb
new file mode 100644
index 0000000000..ba1444652d
--- /dev/null
+++ b/railties/lib/generators/resource_helpers.rb
@@ -0,0 +1,74 @@
+module Rails
+ module Generators
+ # Deal with controller names on scaffold and add some helpers to deal with
+ # ActiveModel.
+ #
+ module ResourceHelpers
+ def self.included(base) #:nodoc:
+ base.send :attr_reader, :controller_name, :controller_class_name, :controller_file_name,
+ :controller_class_path, :controller_file_path
+
+ base.send :class_option, :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName"
+ end
+
+ # Set controller variables on initialization.
+ #
+ def initialize(*args) #:nodoc:
+ super
+
+ if name == name.pluralize && !options[:force_plural]
+ say "Plural version of the model detected, using singularized version. Override with --force-plural."
+ name.replace name.singularize
+ assign_names!(self.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
+
+ # 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.
+ unless self.class.class_options[:orm]
+ raise "You need to have :orm as class option to invoke orm_class and orm_instance"
+ end
+
+ active_model = "#{options[:orm].to_s.classify}::Generators::ActiveModel"
+
+ # If the orm was not loaded, try to load it at "generators/orm",
+ # for example "generators/active_record" or "generators/sequel".
+ begin
+ klass = active_model.constantize
+ rescue NameError
+ require "generators/#{options[:orm]}"
+ end
+
+ # Try once again after loading the file with success.
+ klass ||= active_model.constantize
+ rescue Exception => e
+ raise Error, "Could not load #{active_model}, skipping controller. Error: #{e.message}."
+ end
+ end
+
+ # Initialize ORM::Generators::ActiveModel to access instance methods.
+ #
+ def orm_instance(name=file_name)
+ @orm_instance ||= @orm_class.new(name)
+ end
+ end
+ end
+end
diff --git a/railties/lib/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/generators/test_unit/scaffold/scaffold_generator.rb
index 78fcea1e9c..a8f9c8bd79 100644
--- a/railties/lib/generators/test_unit/scaffold/scaffold_generator.rb
+++ b/railties/lib/generators/test_unit/scaffold/scaffold_generator.rb
@@ -1,9 +1,10 @@
require 'generators/test_unit'
+require 'generators/resource_helpers'
module TestUnit
module Generators
class ScaffoldGenerator < Base
- include Rails::Generators::ScaffoldBase
+ include Rails::Generators::ResourceHelpers
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
check_class_collision :suffix => "ControllerTest"