Implemented ./script/generate scaffold_resource comment body:text created_at:datetime active:boolean price:decimal

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5147 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2006-09-19 08:47:31 +00:00
parent 0da2357527
commit a292cd3222
7 changed files with 119 additions and 70 deletions

View File

@@ -1,4 +1,41 @@
class ScaffoldResourceGenerator < Rails::Generator::NamedBase
class ScaffoldAttribute
attr_accessor :name, :type, :column
def initialize(name, type)
@name, @type = name, type.to_sym
@column = ActiveRecord::ConnectionAdapters::Column.new(name, nil, @type)
end
def field_type
@field_type ||= case type
when :integer, :float, :decimal then :text_field
when :datetime, :timestamp, :time then :datetime_select
when :date then :date_select
when :string then :text_field
when :text then :text_area
when :boolean then :check_box
else
:text_field
end
end
def default
@default ||= case type
when :integer then 1
when :float then 1.5
when :decimal then "9.99"
when :datetime, :timestamp, :time then Time.now.to_s(:db)
when :date then Date.today.to_s(:db)
when :string then "MyString"
when :text then "MyText"
when :boolean then false
else
""
end
end
end
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
@@ -28,68 +65,44 @@ class ScaffoldResourceGenerator < Rails::Generator::NamedBase
def manifest
recorded_session = record do |m|
# Check for class naming collisions.
m.class_collisions controller_class_path, "#{controller_class_name}Controller",
"#{controller_class_name}Helper"
m.class_collisions class_path, "#{class_name}"
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
m.class_collisions(class_path, "#{class_name}")
# Controller, helper, views, and test directories.
m.directory File.join('app/models', class_path)
m.directory File.join('app/controllers', controller_class_path)
m.directory File.join('app/helpers', controller_class_path)
m.directory File.join('app/views', controller_class_path, controller_file_name)
m.directory File.join('test/functional', controller_class_path)
m.directory File.join('test/unit', class_path)
m.directory(File.join('app/models', class_path))
m.directory(File.join('app/controllers', controller_class_path))
m.directory(File.join('app/helpers', controller_class_path))
m.directory(File.join('app/views', controller_class_path, controller_file_name))
m.directory(File.join('test/functional', controller_class_path))
m.directory(File.join('test/unit', class_path))
scaffold_views.each do |action|
m.template "view_#{action}.rhtml",
File.join('app/views',
controller_class_path,
controller_file_name,
"#{action}.rhtml"),
:assigns => { :action => action }
for action in scaffold_views
m.template(
"view_#{action}.rhtml",
File.join('app/views', controller_class_path, controller_file_name, "#{action}.rhtml")
)
end
m.template 'model.rb',
File.join('app/models',
class_path,
"#{file_name}.rb")
m.template('model.rb', File.join('app/models', class_path, "#{file_name}.rb"))
m.template 'controller.rb',
File.join('app/controllers',
controller_class_path,
"#{controller_file_name}_controller.rb")
m.template(
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
)
m.template 'functional_test.rb',
File.join('test/functional',
controller_class_path,
"#{controller_file_name}_controller_test.rb")
m.template 'helper.rb',
File.join('app/helpers',
controller_class_path,
"#{controller_file_name}_helper.rb")
m.template 'unit_test.rb',
File.join('test/unit',
class_path,
"#{file_name}_test.rb")
m.template 'fixtures.yml',
File.join('test/fixtures',
"#{table_name}.yml")
m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
m.template('unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb"))
m.template('fixtures.yml', File.join('test/fixtures', "#{table_name}.yml"))
unless options[:skip_migration]
m.migration_template 'migration.rb', 'db/migrate', :assigns => {
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
end
# View template for each action.
[:index, :new, :edit, :show].each do |action|
path = File.join('app/views', class_path, table_name, "#{action}.rhtml")
m.template 'view.rhtml',
path,
:assigns => { :action => action, :path => path }
m.migration_template(
'migration.rb', 'db/migrate',
:assigns => {
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
:attributes => attributes
},
:migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
)
end
end
@@ -118,4 +131,10 @@ class ScaffoldResourceGenerator < Rails::Generator::NamedBase
def model_name
class_name.demodulize
end
end
def attributes
@attributes ||= args.collect do |attribute|
ScaffoldAttribute.new(*attribute.split(":"))
end
end
end

View File

@@ -1,5 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
first:
one:
id: 1
another:
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>
two:
id: 2
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>

View File

@@ -1,9 +1,9 @@
class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
t.column :name, :string
t.column :description, :text
t.column :created_at, :datetime
<% for attribute in attributes -%>
t.column :<%= attribute.name %>, :<%= attribute.type %>
<% end -%>
end
end

View File

@@ -1,6 +1,17 @@
<h1>Editing <%= singular_name %></h1>
<%%= form(@<%= singular_name %>) %>
<%% form_for(:<%= singular_name %>, :url => <%= singular_name %>_path(@<%= singular_name %>), :html => { :method => :put }) do |f| %>
<% for attribute in attributes -%>
<p>
<b><%= attribute.column.human_name %></b><br />
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
</p>
<% end -%>
<p>
<%%= submit_tag "Update" %>
</p>
<%% end %>
<%%= link_to 'Show', <%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Back', <%= plural_name %>_path %>

View File

@@ -2,16 +2,16 @@
<table>
<tr>
<%% for column in <%= model_name %>.content_columns %>
<th><%%= column.human_name %></th>
<%% end %>
<% for attribute in attributes -%>
<th><%= attribute.column.human_name %></th>
<% end -%>
</tr>
<%% for <%= singular_name %> in @<%= plural_name %> %>
<tr>
<%% for column in <%= model_name %>.content_columns %>
<td><%%=h <%= singular_name %>.send(column.name) %></td>
<%% end %>
<% for attribute in attributes -%>
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
<% end -%>
<td><%%= link_to 'Show', <%= singular_name %>_path(<%= singular_name %>) %></td>
<td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_name %>_path(<%= singular_name %>), :confirm => 'Are you sure?', :method => :delete %></td>

View File

@@ -1,5 +1,16 @@
<h1>New <%= singular_name %></h1>
<%%= form(@<%= singular_name %>) %>
<%% form_for(:<%= singular_name %>, :url => <%= plural_name %>_path) do |f| %>
<% for attribute in attributes -%>
<p>
<b><%= attribute.column.human_name %></b><br />
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
</p>
<% end -%>
<p>
<%%= submit_tag "Create" %>
</p>
<%% end %>
<%%= link_to 'Back', <%= plural_name %>_path %>

View File

@@ -1,8 +1,10 @@
<%% for column in <%= model_name %>.content_columns %>
<% for attribute in attributes -%>
<p>
<b><%%= column.human_name %>:</b> <%%=h @<%= singular_name %>.send(column.name) %>
<b><%= attribute.column.human_name %>:</b>
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
</p>
<%% end %>
<% end -%>
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Back', <%= plural_name %>_path %>