X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Frailties%2Fdoc%2Fguides%2Fsource%2Fcreating_plugins%2Fgenerator_method.txt;fp=vendor%2Frails%2Frailties%2Fdoc%2Fguides%2Fsource%2Fcreating_plugins%2Fgenerator_method.txt;h=126692f2c4cb930e2ba706c5089cd57a9bdabf2d;hb=d115f2e23823271635bad69229a42cd8ac68debe;hp=0000000000000000000000000000000000000000;hpb=37cb670bf3ddde90b214e591f100ed4446469484;p=depot.git diff --git a/vendor/rails/railties/doc/guides/source/creating_plugins/generator_method.txt b/vendor/rails/railties/doc/guides/source/creating_plugins/generator_method.txt new file mode 100644 index 0000000..126692f --- /dev/null +++ b/vendor/rails/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 +-----------------------------------------------------------