diff options
Diffstat (limited to 'railties/lib/rails_generator/generators/components/model')
5 files changed, 56 insertions, 0 deletions
diff --git a/railties/lib/rails_generator/generators/components/model/USAGE b/railties/lib/rails_generator/generators/components/model/USAGE new file mode 100644 index 0000000000..f0669104fe --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/USAGE @@ -0,0 +1,17 @@ +Description: + The model generator creates stubs for a new model. + + The generator takes a model name as its argument. The model name may be + given in CamelCase or under_score and should not be suffixed with 'Model'. + + The generator creates a model class in app/models, a test suite in + test/unit, and test fixtures in test/fixtures/model_name.yml. + +Example: + ./script/generate model Account + + This will create an Account model: + Model: app/models/account.rb + Test: test/unit/account_test.rb + Fixtures: test/fixtures/accounts.yml + diff --git a/railties/lib/rails_generator/generators/components/model/model_generator.rb b/railties/lib/rails_generator/generators/components/model/model_generator.rb new file mode 100644 index 0000000000..32577d08a3 --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/model_generator.rb @@ -0,0 +1,13 @@ +class ModelGenerator < Rails::Generator::NamedBase + def manifest + record do |m| + # Check for class naming collisions. + m.class_collisions class_name, "#{class_name}Test" + + # Model class, unit test, and fixtures. + m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") + m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb") + m.template 'fixtures.yml', File.join('test/fixtures', class_path, "#{table_name}.yml") + end + end +end diff --git a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml new file mode 100644 index 0000000000..fc3185dc46 --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml @@ -0,0 +1,10 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +# Set the $base_id variable in the setup method of your tests. +# It's used to ensure that ids don't clash in some databases. +<%% $base_id ||= 100000 %> + +first_<%= singular_name %>: + id: <%%= $base_id %> + +another_<%= singular_name %>: + id: <%%= $base_id + 1 %> diff --git a/railties/lib/rails_generator/generators/components/model/templates/model.rb b/railties/lib/rails_generator/generators/components/model/templates/model.rb new file mode 100644 index 0000000000..8d4c89e912 --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/templates/model.rb @@ -0,0 +1,2 @@ +class <%= class_name %> < ActiveRecord::Base +end diff --git a/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb new file mode 100644 index 0000000000..db0fbf5d33 --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb @@ -0,0 +1,14 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class <%= class_name %>Test < Test::Unit::TestCase + fixtures :<%= table_name %> + + def setup + $base_id = 1000001 + end + + # Replace this with your real tests. + def test_truth + assert true + end +end |