Changeset 5147
- Timestamp:
- 09/19/06 08:47:31 (3 years ago)
- Files:
-
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb (modified) (3 diffs)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/fixtures.yml (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_edit.rhtml (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_index.rhtml (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_new.rhtml (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_show.rhtml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb
r5132 r5147 1 1 class ScaffoldResourceGenerator < Rails::Generator::NamedBase 2 class ScaffoldAttribute 3 attr_accessor :name, :type, :column 4 5 def initialize(name, type) 6 @name, @type = name, type.to_sym 7 @column = ActiveRecord::ConnectionAdapters::Column.new(name, nil, @type) 8 end 9 10 def field_type 11 @field_type ||= case type 12 when :integer, :float, :decimal then :text_field 13 when :datetime, :timestamp, :time then :datetime_select 14 when :date then :date_select 15 when :string then :text_field 16 when :text then :text_area 17 when :boolean then :check_box 18 else 19 :text_field 20 end 21 end 22 23 def default 24 @default ||= case type 25 when :integer then 1 26 when :float then 1.5 27 when :decimal then "9.99" 28 when :datetime, :timestamp, :time then Time.now.to_s(:db) 29 when :date then Date.today.to_s(:db) 30 when :string then "MyString" 31 when :text then "MyText" 32 when :boolean then false 33 else 34 "" 35 end 36 end 37 end 38 2 39 attr_reader :controller_name, 3 40 :controller_class_path, … … 29 66 recorded_session = record do |m| 30 67 # Check for class naming collisions. 31 m.class_collisions controller_class_path, "#{controller_class_name}Controller", 32 "#{controller_class_name}Helper" 33 m.class_collisions class_path, "#{class_name}" 68 m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper") 69 m.class_collisions(class_path, "#{class_name}") 34 70 35 71 # Controller, helper, views, and test directories. 36 m.directory File.join('app/models', class_path)37 m.directory File.join('app/controllers', controller_class_path)38 m.directory File.join('app/helpers', controller_class_path)39 m.directory File.join('app/views', controller_class_path, controller_file_name)40 m.directory File.join('test/functional', controller_class_path)41 m.directory File.join('test/unit', class_path)72 m.directory(File.join('app/models', class_path)) 73 m.directory(File.join('app/controllers', controller_class_path)) 74 m.directory(File.join('app/helpers', controller_class_path)) 75 m.directory(File.join('app/views', controller_class_path, controller_file_name)) 76 m.directory(File.join('test/functional', controller_class_path)) 77 m.directory(File.join('test/unit', class_path)) 42 78 43 scaffold_views.each do |action| 44 m.template "view_#{action}.rhtml", 45 File.join('app/views', 46 controller_class_path, 47 controller_file_name, 48 "#{action}.rhtml"), 49 :assigns => { :action => action } 79 for action in scaffold_views 80 m.template( 81 "view_#{action}.rhtml", 82 File.join('app/views', controller_class_path, controller_file_name, "#{action}.rhtml") 83 ) 50 84 end 51 85 52 m.template 'model.rb', 53 File.join('app/models', 54 class_path, 55 "#{file_name}.rb") 86 m.template('model.rb', File.join('app/models', class_path, "#{file_name}.rb")) 56 87 57 m.template 'controller.rb', 58 File.join('app/controllers', 59 controller_class_path, 60 "#{controller_file_name}_controller.rb") 88 m.template( 89 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") 90 ) 61 91 62 m.template 'functional_test.rb', 63 File.join('test/functional', 64 controller_class_path, 65 "#{controller_file_name}_controller_test.rb") 66 67 m.template 'helper.rb', 68 File.join('app/helpers', 69 controller_class_path, 70 "#{controller_file_name}_helper.rb") 71 72 m.template 'unit_test.rb', 73 File.join('test/unit', 74 class_path, 75 "#{file_name}_test.rb") 76 77 m.template 'fixtures.yml', 78 File.join('test/fixtures', 79 "#{table_name}.yml") 92 m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")) 93 m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")) 94 m.template('unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")) 95 m.template('fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")) 80 96 81 97 unless options[:skip_migration] 82 m.migration_template 'migration.rb', 'db/migrate', :assigns => { 83 :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" 84 }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" 85 end 86 87 # View template for each action. 88 [:index, :new, :edit, :show].each do |action| 89 path = File.join('app/views', class_path, table_name, "#{action}.rhtml") 90 m.template 'view.rhtml', 91 path, 92 :assigns => { :action => action, :path => path } 98 m.migration_template( 99 'migration.rb', 'db/migrate', 100 :assigns => { 101 :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}", 102 :attributes => attributes 103 }, 104 :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" 105 ) 93 106 end 94 107 end … … 119 132 class_name.demodulize 120 133 end 134 135 def attributes 136 @attributes ||= args.collect do |attribute| 137 ScaffoldAttribute.new(*attribute.split(":")) 138 end 139 end 121 140 end trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/fixtures.yml
r5132 r5147 1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 first:2 one: 3 3 id: 1 4 another: 4 <% for attribute in attributes -%> 5 <%= attribute.name %>: <%= attribute.default %> 6 <% end -%> 7 two: 5 8 id: 2 9 <% for attribute in attributes -%> 10 <%= attribute.name %>: <%= attribute.default %> 11 <% end -%> trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb
r5132 r5147 2 2 def self.up 3 3 create_table :<%= table_name %> do |t| 4 t.column :name, :string 5 t.column : description, :text6 t.column :created_at, :datetime 4 <% for attribute in attributes -%> 5 t.column :<%= attribute.name %>, :<%= attribute.type %> 6 <% end -%> 7 7 end 8 8 end trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_edit.rhtml
r5132 r5147 1 1 <h1>Editing <%= singular_name %></h1> 2 2 3 <%%= form(@<%= singular_name %>) %> 3 <%% form_for(:<%= singular_name %>, :url => <%= singular_name %>_path(@<%= singular_name %>), :html => { :method => :put }) do |f| %> 4 <% for attribute in attributes -%> 5 <p> 6 <b><%= attribute.column.human_name %></b><br /> 7 <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %> 8 </p> 9 10 <% end -%> 11 <p> 12 <%%= submit_tag "Update" %> 13 </p> 14 <%% end %> 4 15 5 16 <%%= link_to 'Show', <%= singular_name %>_path(@<%= singular_name %>) %> | trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_index.rhtml
r5132 r5147 3 3 <table> 4 4 <tr> 5 <%% for column in <%= model_name %>.content_columns%>6 <th><% %=column.human_name %></th>7 <%% end%>5 <% for attribute in attributes -%> 6 <th><%= attribute.column.human_name %></th> 7 <% end -%> 8 8 </tr> 9 9 10 10 <%% for <%= singular_name %> in @<%= plural_name %> %> 11 11 <tr> 12 <%% for column in <%= model_name %>.content_columns%>13 <td><%%=h <%= singular_name %>. send(column.name)%></td>14 <%% end%>12 <% for attribute in attributes -%> 13 <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td> 14 <% end -%> 15 15 <td><%%= link_to 'Show', <%= singular_name %>_path(<%= singular_name %>) %></td> 16 16 <td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td> trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_new.rhtml
r5132 r5147 1 1 <h1>New <%= singular_name %></h1> 2 2 3 <%%= form(@<%= singular_name %>) %> 3 <%% form_for(:<%= singular_name %>, :url => <%= plural_name %>_path) do |f| %> 4 <% for attribute in attributes -%> 5 <p> 6 <b><%= attribute.column.human_name %></b><br /> 7 <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %> 8 </p> 9 10 <% end -%> 11 <p> 12 <%%= submit_tag "Create" %> 13 </p> 14 <%% end %> 4 15 5 16 <%%= link_to 'Back', <%= plural_name %>_path %> trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/view_show.rhtml
r5132 r5147 1 <% % for column in <%= model_name %>.content_columns%>1 <% for attribute in attributes -%> 2 2 <p> 3 <b><%%= column.human_name %>:</b> <%%=h @<%= singular_name %>.send(column.name) %> 3 <b><%= attribute.column.human_name %>:</b> 4 <%%=h @<%= singular_name %>.<%= attribute.name %> %> 4 5 </p> 5 <%% end %> 6 7 <% end -%> 6 8 7 9 <%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |