Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
141 lines (112 sloc) 4.23 KB
== 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 fake_rails_root
File.join(File.dirname(__FILE__), "rails_root")
end
def routes_path
File.join(fake_rails_root, "config", "routes.rb")
end
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\.yaffle/, File.read(routes_path)
end
def test_destroys_route
content = <<-END
ActionController::Routing::Routes.draw do |map|
map.yaffle
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\.yaffle/, File.read(routes_path)
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.yaffle\n"}
end
end
end
module Destroy
def yaffle_route
logger.route "map.yaffle"
gsub_file 'config/routes.rb', /\n.+?map\.yaffle/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/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.
Jump to Line
Something went wrong with that request. Please try again.