aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/generator_method.txt
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-11-15 12:21:04 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2008-11-15 12:21:04 -0800
commiteeea1a26ec7bd5e11caa4630ff7820c1c7f762e3 (patch)
tree3b193dba9756b45e1a0a70d47faa4c3e2ff946a9 /railties/doc/guides/source/creating_plugins/generator_method.txt
parent1304b664924bfea54fd6dc0dc924ae3d126ff92d (diff)
parent4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d (diff)
downloadrails-eeea1a26ec7bd5e11caa4630ff7820c1c7f762e3.tar.gz
rails-eeea1a26ec7bd5e11caa4630ff7820c1c7f762e3.tar.bz2
rails-eeea1a26ec7bd5e11caa4630ff7820c1c7f762e3.zip
Merge branch 'master' into testing
Diffstat (limited to 'railties/doc/guides/source/creating_plugins/generator_method.txt')
-rw-r--r--railties/doc/guides/source/creating_plugins/generator_method.txt89
1 files changed, 89 insertions, 0 deletions
diff --git a/railties/doc/guides/source/creating_plugins/generator_method.txt b/railties/doc/guides/source/creating_plugins/generator_method.txt
new file mode 100644
index 0000000000..126692f2c4
--- /dev/null
+++ b/railties/doc/guides/source/creating_plugins/generator_method.txt
@@ -0,0 +1,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
+-----------------------------------------------------------