Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / creating_plugins / generator_method.txt
1 == Add a custom generator command ==
2
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.
4
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.
6
7 To start, add the following test method:
8
9 *vendor/plugins/yaffle/test/generator_test.rb*
10
11 [source, ruby]
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
17 end
18 -----------------------------------------------------------
19
20 Run `rake` to watch the test fail, then make the test pass add the following:
21
22 *vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*
23
24 -----------------------------------------------------------
25 Yaffle: A bird
26 -----------------------------------------------------------
27
28 *vendor/plugins/yaffle/lib/yaffle.rb*
29
30 [source, ruby]
31 -----------------------------------------------------------
32 require "yaffle/commands"
33 -----------------------------------------------------------
34
35 *vendor/plugins/yaffle/lib/commands.rb*
36
37 [source, ruby]
38 -----------------------------------------------------------
39 require 'rails_generator'
40 require 'rails_generator/commands'
41
42 module Yaffle #:nodoc:
43 module Generator #:nodoc:
44 module Commands #:nodoc:
45 module Create
46 def yaffle_definition
47 file("definition.txt", "definition.txt")
48 end
49 end
50
51 module Destroy
52 def yaffle_definition
53 file("definition.txt", "definition.txt")
54 end
55 end
56
57 module List
58 def yaffle_definition
59 file("definition.txt", "definition.txt")
60 end
61 end
62
63 module Update
64 def yaffle_definition
65 file("definition.txt", "definition.txt")
66 end
67 end
68 end
69 end
70 end
71
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 -----------------------------------------------------------
77
78 Finally, call your new method in the manifest:
79
80 *vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
81
82 [source, ruby]
83 -----------------------------------------------------------
84 class YaffleGenerator < Rails::Generator::NamedBase
85 def manifest
86 m.yaffle_definition
87 end
88 end
89 -----------------------------------------------------------