aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/generators.txt
blob: 8ef46561d1bc915c6dafb13d789519e38308ea28 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
== Generators ==

Many plugins ship with generators.  When you created the plugin above, you specified the --with-generator option, so you already have the generator stubs in 'vendor/plugins/yaffle/generators/yaffle'.

Building generators is a complex topic unto itself and this section will cover one small aspect of generators:  creating a generator that adds a time-stamped migration.

To add a generator to a plugin:

 * Write a test
 * Add your instructions to the 'manifest' method of the generator
 * Add any necessary template files to the templates directory
 * Update the USAGE file to add helpful documentation for your generator

=== Testing generators ===

Many rails plugin authors do not test their generators, however testing generators is quite simple.  A typical generator test does the following:

 * Creates a new fake rails root directory that will serve as destination
 * Runs the generator
 * Asserts that the correct files were generated
 * Removes the fake rails root

This section will describe how to create a simple generator that adds a file.  For the generator in this section, the test could look something like this:

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

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

class DefinitionGeneratorTest < Test::Unit::TestCase

  def setup
    FileUtils.mkdir_p(fake_rails_root)
    @original_files = file_list
  end

  def teardown
    FileUtils.rm_r(fake_rails_root)
  end
  
  def test_generates_correct_file_name
    Rails::Generator::Scripts::Generate.new.run(["yaffle_definition"], :destination => fake_rails_root)
    new_file = (file_list - @original_files).first
    assert_equal "definition.txt", File.basename(new_file)
  end

  private
  
    def fake_rails_root
      File.join(File.dirname(__FILE__), 'rails_root')
    end
  
    def file_list
      Dir.glob(File.join(fake_rails_root, "*"))
    end
  
end
------------------------------------------------------------------

You can run 'rake' from the plugin directory to see this fail.  Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.

To make it pass, create the generator:

*vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb*

[source, ruby]
------------------------------------------------------------------
class YaffleDefinitionGenerator < Rails::Generator::Base
  def manifest
    record do |m|
      m.file "definition.txt", "definition.txt"
    end
  end
end
------------------------------------------------------------------

=== The USAGE file ===

If you plan to distribute your plugin, developers will expect at least a minimum of documentation.  You can add simple documentation to the generator by updating the USAGE file.

Rails ships with several built-in generators.  You can see all of the generators available to you by typing the following at the command line:

------------------------------------------------------------------
./script/generate
------------------------------------------------------------------

You should see something like this:

------------------------------------------------------------------
Installed Generators
  Plugins (vendor/plugins): yaffle_definition
  Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration
------------------------------------------------------------------

When you run `script/generate yaffle_definition -h` you should see the contents of your 'vendor/plugins/yaffle/generators/yaffle_definition/USAGE'.  

For this plugin, update the USAGE file could look like this:

------------------------------------------------------------------
Description:
    Adds a file with the definition of a Yaffle to the app's main directory
------------------------------------------------------------------