From 8a9bd56ca0d43c703f330e378474ddbdca2acd8e Mon Sep 17 00:00:00 2001 From: Jeff Dean Date: Fri, 14 Nov 2008 00:38:52 -0500 Subject: Plugin Guide: updated test setup and generator sections --- railties/doc/guides/html/creating_plugins.html | 65 ++++++++-------- .../source/creating_plugins/custom_generator.txt | 69 ----------------- .../source/creating_plugins/generator_method.txt | 89 ++++++++++++++++++++++ .../doc/guides/source/creating_plugins/index.txt | 2 +- .../guides/source/creating_plugins/test_setup.txt | 2 - 5 files changed, 125 insertions(+), 102 deletions(-) delete mode 100644 railties/doc/guides/source/creating_plugins/custom_generator.txt create mode 100644 railties/doc/guides/source/creating_plugins/generator_method.txt (limited to 'railties') diff --git a/railties/doc/guides/html/creating_plugins.html b/railties/doc/guides/html/creating_plugins.html index 5589971ec5..3e67c34b4f 100644 --- a/railties/doc/guides/html/creating_plugins.html +++ b/railties/doc/guides/html/creating_plugins.html @@ -1178,35 +1178,34 @@ Example:

6. Add a custom generator command

-

You may have noticed above that you can used one of the built-in rails migration commands m.migration_template. You can create your own commands for these, using the following steps:

-
    -
  1. -

    -Add the require and hook statements to init.rb. -

    -
  2. -
  3. -

    -Create the commands - creating 3 sets, Create, Destroy, List. -

    -
  4. -
  5. -

    -Add the method to your generator. -

    -
  6. -
-

Working with the internals of generators is beyond the scope of this tutorial, but here is a basic example:

-

vendor/plugins/yaffle/init.rb

+

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

-
require "commands"
-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
+
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

+
+
+
require "yaffle/commands"
 

vendor/plugins/yaffle/lib/commands.rb

@@ -1237,15 +1236,22 @@ http://www.gnu.org/software/src-highlite --> 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
-

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

-
-
-
Yaffle is a bird
-
+

Finally, call your new method in the manifest:

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

end end
-

This example just uses the built-in "file" method, but you could do anything that Ruby allows.

7. Add a Custom Route

diff --git a/railties/doc/guides/source/creating_plugins/custom_generator.txt b/railties/doc/guides/source/creating_plugins/custom_generator.txt deleted file mode 100644 index a8cf1b48ce..0000000000 --- a/railties/doc/guides/source/creating_plugins/custom_generator.txt +++ /dev/null @@ -1,69 +0,0 @@ -== Add a custom generator command == - -You may have noticed above that you can used one of the built-in rails migration commands `m.migration_template`. You can create your own commands for these, using the following steps: - - 1. Add the require and hook statements to init.rb. - 2. Create the commands - creating 3 sets, Create, Destroy, List. - 3. Add the method to your generator. - -Working with the internals of generators is beyond the scope of this tutorial, but here is a basic example: - -*vendor/plugins/yaffle/init.rb* - -[source, ruby] ------------------------------------------------------------ -require "commands" -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 ------------------------------------------------------------ - -*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 - end - end -end ------------------------------------------------------------ - -*vendor/plugins/yaffle/generators/yaffle/templates/definition.txt* ------------------------------------------------------------ -Yaffle is a bird ------------------------------------------------------------ - -*vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb* - -[source, ruby] ------------------------------------------------------------ -class YaffleGenerator < Rails::Generator::NamedBase - def manifest - m.yaffle_definition - end -end ------------------------------------------------------------ - -This example just uses the built-in "file" method, but you could do anything that Ruby allows. 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 +----------------------------------------------------------- diff --git a/railties/doc/guides/source/creating_plugins/index.txt b/railties/doc/guides/source/creating_plugins/index.txt index 91d7027323..bd7dfe65c3 100644 --- a/railties/doc/guides/source/creating_plugins/index.txt +++ b/railties/doc/guides/source/creating_plugins/index.txt @@ -39,7 +39,7 @@ include::view_helper.txt[] include::migration_generator.txt[] -include::custom_generator.txt[] +include::generator_method.txt[] include::custom_route.txt[] diff --git a/railties/doc/guides/source/creating_plugins/test_setup.txt b/railties/doc/guides/source/creating_plugins/test_setup.txt index 4bcb2d5c2b..9e6763bc30 100644 --- a/railties/doc/guides/source/creating_plugins/test_setup.txt +++ b/railties/doc/guides/source/creating_plugins/test_setup.txt @@ -153,9 +153,7 @@ def load_schema end ActiveRecord::Base.establish_connection(config[db_adapter]) - load(File.dirname(__FILE__) + "/schema.rb") - require File.dirname(__FILE__) + '/../init.rb' end ---------------------------------------------- -- cgit v1.2.3