Created
May 10, 2011 22:01
-
-
Save malclocke/965471 to your computer and use it in GitHub Desktop.
A minimal Rails 3 generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/generators/test_generator/templates/model.rb | |
| class <%= class_name %> | |
| <%- fields.each do |field| -%> | |
| def <%= field %> | |
| puts "<%= field %>" | |
| end | |
| <%- end -%> | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/generators/test_generator/test_generator_generator.rb | |
| require 'rails/generators' | |
| require 'rails/generators/named_base' | |
| class TestGeneratorGenerator < Rails::Generators::NamedBase | |
| source_root File.expand_path('../templates', __FILE__) | |
| argument :fields, :type => :array | |
| check_class_collision | |
| def create_model_file | |
| template 'model.rb', File.join('app','models', class_path, "#{file_name}.rb" | |
| end | |
| # All public methods get called when the generator is invoked | |
| protected | |
| def shouldnt_get_called | |
| puts "Shouldnt get called" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # test/unit/test_generator_test.rb | |
| require File.join(File.dirname(__FILE__), '..', 'test_helper') | |
| require 'generators/test_generator/test_generator_generator' | |
| class TestGeneratorTest < Rails::Generators::TestCase | |
| # Name of the generator class that this test exercises | |
| tests TestGeneratorGenerator | |
| # Generate test files in this directory | |
| destination File.join(Rails.root, 'tmp') | |
| # Set up the destination directory, removing previously generated | |
| # files if necessary | |
| setup :prepare_destination | |
| test "it should do its stuff" do | |
| # Run the generator with the given arguments, which will output | |
| # the results in the destination above. | |
| run_generator %w(Flum poop gloop) | |
| # Check the model files class name | |
| assert_file 'app/models/flum.rb', /class Flum/ | |
| # Should create a method for each of the extra arguments passed | |
| assert_file 'app/models/flum.rb', /def poop/ | |
| assert_file 'app/models/flum.rb', /def gloop/ | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/generators/test_generator/USAGE | |
| Description: | |
| A minimal rails 3 generator example | |
| Example: | |
| rails generate NewClass method1 method2 | |
| This will create: | |
| app/models/new_class.rb | |
| Which will contain the following methods: | |
| def method1 | |
| puts "method1" | |
| end | |
| def method2 | |
| puts "method2" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment