1 == Add a custom generator command ==
3 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.
5 This section describes how you you can create your own commands to add and remove a line of text from 'routes.rb'. This example creates a very simple method that adds or removes a text file.
7 To start, add the following test method:
9 *vendor/plugins/yaffle/test/generator_test.rb*
12 -----------------------------------------------------------
13 def test_generates_definition
14 Rails::Generator::Scripts::Generate.new.run(["yaffle", "bird"], :destination => fake_rails_root)
15 definition = File.read(File.join(fake_rails_root, "definition.txt"))
16 assert_match /Yaffle\:/, definition
18 -----------------------------------------------------------
20 Run `rake` to watch the test fail, then make the test pass add the following:
22 *vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*
24 -----------------------------------------------------------
26 -----------------------------------------------------------
28 *vendor/plugins/yaffle/lib/yaffle.rb*
31 -----------------------------------------------------------
32 require "yaffle/commands"
33 -----------------------------------------------------------
35 *vendor/plugins/yaffle/lib/commands.rb*
38 -----------------------------------------------------------
39 require 'rails_generator'
40 require 'rails_generator/commands'
42 module Yaffle #:nodoc:
43 module Generator #:nodoc:
44 module Commands #:nodoc:
47 file("definition.txt", "definition.txt")
53 file("definition.txt", "definition.txt")
59 file("definition.txt", "definition.txt")
65 file("definition.txt", "definition.txt")
72 Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
73 Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
74 Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
75 Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
76 -----------------------------------------------------------
78 Finally, call your new method in the manifest:
80 *vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
83 -----------------------------------------------------------
84 class YaffleGenerator < Rails::Generator::NamedBase
89 -----------------------------------------------------------