aboutsummaryrefslogblamecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/generator_commands.txt
blob: 6ded0dde59c7b2d338273a59b9620428787fbc91 (plain) (tree)


















                                                                                                                                                                                                                                            

















                                                                                                  
                                                       




                                                     
                   






                                                                                                 
                                                          
     










                                                       


























                                                                             
                                                                                                                






                                   
                                                                 





















                                                                                         
                                                                         


















                                                           
               
                                                                                                                         
== Generator Commands ==

You may have noticed above that you can used one of the built-in rails migration commands `migration_template`.  If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.

This section describes how you you can create your own commands to add and remove a line of text from 'config/routes.rb'.

To start, add the following test method:

*vendor/plugins/yaffle/test/route_generator_test.rb*

[source, ruby]
-----------------------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
require 'rails_generator'
require 'rails_generator/scripts/generate'
require 'rails_generator/scripts/destroy'

class RouteGeneratorTest < Test::Unit::TestCase

  def setup
    FileUtils.mkdir_p(File.join(fake_rails_root, "config"))
  end
  
  def teardown
    FileUtils.rm_r(fake_rails_root)
  end
  
  def test_generates_route
    content = <<-END
      ActionController::Routing::Routes.draw do |map|
        map.connect ':controller/:action/:id'
        map.connect ':controller/:action/:id.:format'
      end
    END
    File.open(routes_path, 'wb') {|f| f.write(content) }
    
    Rails::Generator::Scripts::Generate.new.run(["yaffle_route"], :destination => fake_rails_root)
    assert_match /map\.yaffles/, File.read(routes_path)
  end

  def test_destroys_route
    content = <<-END
      ActionController::Routing::Routes.draw do |map|
        map.yaffles
        map.connect ':controller/:action/:id'
        map.connect ':controller/:action/:id.:format'
      end
    END
    File.open(routes_path, 'wb') {|f| f.write(content) }
    
    Rails::Generator::Scripts::Destroy.new.run(["yaffle_route"], :destination => fake_rails_root)
    assert_no_match /map\.yaffles/, File.read(routes_path)
  end
  
  private
  
    def fake_rails_root
      File.join(File.dirname(__FILE__), "rails_root")
    end
  
    def routes_path
      File.join(fake_rails_root, "config", "routes.rb")
    end
  
end
-----------------------------------------------------------

Run `rake` to watch the test fail, then make the test pass add the following:

*vendor/plugins/yaffle/lib/yaffle.rb*

[source, ruby]
-----------------------------------------------------------
require "yaffle/commands"
-----------------------------------------------------------

*vendor/plugins/yaffle/lib/yaffle/commands.rb*

[source, ruby]
-----------------------------------------------------------
require 'rails_generator'
require 'rails_generator/commands'

module Yaffle #:nodoc:
  module Generator #:nodoc:
    module Commands #:nodoc:
      module Create
        def yaffle_route
          logger.route "map.yaffle"
          look_for = 'ActionController::Routing::Routes.draw do |map|'
          unless options[:pretend]
            gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n  map.yaffles\n"}
          end
        end
      end

      module Destroy
        def yaffle_route
          logger.route "map.yaffle"
          gsub_file 'config/routes.rb', /\n.+?map\.yaffles/mi, ''
        end
      end

      module List
        def yaffle_route
        end
      end

      module Update
        def yaffle_route
        end
      end
    end
  end
end

Rails::Generator::Commands::Create.send   :include,  Yaffle::Generator::Commands::Create
Rails::Generator::Commands::Destroy.send  :include,  Yaffle::Generator::Commands::Destroy
Rails::Generator::Commands::List.send     :include,  Yaffle::Generator::Commands::List
Rails::Generator::Commands::Update.send   :include,  Yaffle::Generator::Commands::Update
-----------------------------------------------------------

*vendor/plugins/yaffle/generators/yaffle_route/yaffle_route_generator.rb*

[source, ruby]
-----------------------------------------------------------
class YaffleRouteGenerator < Rails::Generator::Base
  def manifest
    record do |m|
      m.yaffle_route
    end
  end
end
-----------------------------------------------------------

To see this work, type:

-----------------------------------------------------------
./script/generate yaffle_route
./script/destroy yaffle_route
-----------------------------------------------------------

.Editor's note:
NOTE: If you haven't set up the custom route from above, 'script/destroy' will fail and you'll have to remove it manually.