aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/generator_method.txt
blob: 126692f2c4cb930e2ba706c5089cd57a9bdabf2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
== Add a custom generator command ==

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 'routes.rb'.  This example creates a very simple method that adds or removes a text file.

To start, add the following test method:

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

[source, ruby]
-----------------------------------------------------------
def test_generates_definition
  Rails::Generator::Scripts::Generate.new.run(["yaffle", "bird"], :destination => fake_rails_root)
  definition = File.read(File.join(fake_rails_root, "definition.txt"))
  assert_match /Yaffle\:/, definition
end
-----------------------------------------------------------

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

*vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*

-----------------------------------------------------------
Yaffle: A bird
-----------------------------------------------------------

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

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

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

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

module Yaffle #:nodoc:
  module Generator #:nodoc:
    module Commands #:nodoc:
      module Create
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end

      module Destroy
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end

      module List
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end

      module Update
        def yaffle_definition
          file("definition.txt", "definition.txt")
        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
-----------------------------------------------------------

Finally, call your new method in the manifest:

*vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*

[source, ruby]
-----------------------------------------------------------
class YaffleGenerator < Rails::Generator::NamedBase
  def manifest
    m.yaffle_definition
  end
end
-----------------------------------------------------------