Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7269: refactor_resource_generator_to_use_model_generator.patch

File refactor_resource_generator_to_use_model_generator.patch, 3.2 kB (added by bscofield, 2 years ago)

Refactor resource generator; remove unneeded templates

  • resource_generator.rb

    old new  
    3939      m.directory(File.join('test/functional', controller_class_path)) 
    4040      m.directory(File.join('test/unit', class_path)) 
    4141 
    42       m.template('model.rb', File.join('app/models', class_path, "#{file_name}.rb")) 
     42      m.dependency 'model', [singular_name] + @args, :collision => :skip 
    4343 
    4444      m.template( 
    4545        'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") 
     
    4747 
    4848      m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")) 
    4949      m.template('helper.rb',          File.join('app/helpers',     controller_class_path, "#{controller_file_name}_helper.rb")) 
    50       m.template('unit_test.rb',       File.join('test/unit',       class_path, "#{file_name}_test.rb")) 
    51       m.template('fixtures.yml',       File.join('test/fixtures', "#{table_name}.yml")) 
    5250 
    53       unless options[:skip_migration] 
    54         m.migration_template( 
    55           'migration.rb', 'db/migrate',  
    56           :assigns => { 
    57             :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}", 
    58             :attributes     => attributes 
    59           },  
    60           :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" 
    61         ) 
    62       end 
    63  
    6451      m.route_resources controller_file_name 
    6552    end 
    6653  end 
  • templates/unit_test.rb

    old new  
    1 require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper' 
    2  
    3 class <%= class_name %>Test < Test::Unit::TestCase 
    4   fixtures :<%= table_name %> 
    5  
    6   # Replace this with your real tests. 
    7   def test_truth 
    8     assert true 
    9   end 
    10 end 
  • templates/model.rb

    old new  
    1 class <%= class_name %> < ActiveRecord::Base 
    2 end 
  • templates/fixtures.yml

    old new  
    1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 
    2 one: 
    3   id: 1 
    4 <% for attribute in attributes -%> 
    5   <%= attribute.name %>: <%= attribute.default %> 
    6 <% end -%> 
    7 two: 
    8   id: 2 
    9 <% for attribute in attributes -%> 
    10   <%= attribute.name %>: <%= attribute.default %> 
    11 <% end -%> 
  • templates/migration.rb

    old new  
    1 class <%= migration_name %> < ActiveRecord::Migration 
    2   def self.up 
    3     create_table :<%= table_name %> do |t| 
    4 <% for attribute in attributes -%> 
    5       t.column :<%= attribute.name %>, :<%= attribute.type %> 
    6 <% end -%> 
    7     end 
    8   end 
    9  
    10   def self.down 
    11     drop_table :<%= table_name %> 
    12   end 
    13 end