From 6b8500ce48f45f18696f6215b8a01f5cf0e328b5 Mon Sep 17 00:00:00 2001
From: Jeff Dean
Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. His home on the internet is his blog Sneaky Abstractions.
This section describes how to add a controller named woodpeckers to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.
You can test your plugin's controller as you would test any other controller:
vendor/plugins/yaffle/yaffle/woodpeckers_controller_test.rb:
vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:
vendor/plugins/yaffle/lib/yaffle.rb:
Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:
Once your plugin is stable, the tests pass on all database and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:
-Your name. -
--How to install. -
--How to add the functionality to the app (several examples of common use cases). -
--Warning, gotchas or tips that might help save users time. -
-Once your README is solid, go through and add rdoc comments to all of the methods that developers will use.
Before you generate your documentation, be sure to go through and add nodoc comments to those modules and methods that are not important to your users.
Once your comments are good to go, navigate to your plugin directory and run:
rake rdoc-
When you created the plugin with the built-in rails generator, it generated a rake file for you in vendor/plugins/yaffle/tasks/yaffle.rake. Any rake task you add here will be available to the app.
Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:
vendor/plugins/yaffle/tasks/yaffle.rake
When you run rake -T from your plugin you will see:
yaffle:squawk # Prints out the word 'Yaffle'+
... +yaffle:squawk # Prints out the word 'Yaffle' +...
You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.
You can store plugins wherever you want - you just have to add those plugins to the plugins path in environment.rb.
Since the plugin is only loaded after the plugin paths are defined, you can't redefine this in your plugins - but it may be good to now.
You can even store plugins inside of other plugins for complete plugin madness!
Note that tasks from vendor/plugins/yaffle/Rakefile are not available to the main app.
Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.
Historically rails plugins loaded the plugin's init.rb file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, init.rb was moved to rails/init.rb.
It's common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in Rakefile. A rake task that packages the gem might look like this:
vendor/plugins/yaffle/Rakefile:
config.plugin_paths << File.join(RAILS_ROOT,"vendor","plugins","yaffle","lib","plugins") +PKG_FILES = FileList[ + '[a-zA-Z]*', + 'generators/**/*', + 'lib/**/*', + 'rails/**/*', + 'tasks/**/*', + 'test/**/*' +] + +spec = Gem::Specification.new do |s| + s.name = "yaffle" + s.version = "0.0.1" + s.author = "Gleeful Yaffler" + s.email = "yaffle@example.com" + s.homepage = "http://yafflers.example.com/" + s.platform = Gem::Platform::RUBY + s.summary = "Sharing Yaffle Goodness" + s.files = PKG_FILES.to_a + s.require_path = "lib" + s.has_rdoc = false + s.extra_rdoc_files = ["README"] +end + +desc 'Turn this plugin into a gem.' +Rake::GemPackageTask.new(spec) do |pkg| + pkg.gem_spec = spec +end
If the built-in plugin behavior is inadequate, you can change almost every aspect of the location and loading process. You can write your own plugin locators and plugin loaders, but that's beyond the scope of this tutorial.
If you are an RSpec fan, you can install the rspec_plugin_generator gem, which will generate the spec folder and database for you. See http://github.com/pat-maddox/rspec-plugin-generator/tree/master.
To build and install the gem locally, run the following commands:
cd vendor/plugins/yaffle +rake gem +sudo gem install pkg/yaffle-0.0.1.gem+
To test this, create a new rails app, add config.gem "yaffle" to environment.rb and all of your plugin's functionality will be available to you.
Once your plugin is stable, the tests pass on all database and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:
+Your name +
++How to install +
++How to add the functionality to the app (several examples of common use cases) +
++Warning, gotchas or tips that might help save users time +
+Once your README is solid, go through and add rdoc comments to all of the methods that developers will use.
Before you generate your documentation, be sure to go through and add nodoc comments to those modules and methods that are not important to your users.
Once your comments are good to go, navigate to your plugin directory and run:
rake rdoc+
If you prefer to use RSpec instead of tets, you may be interested in the RSpec Plugin Generator.
@@ -1811,20 +1839,27 @@ http://www.gnu.org/software/src-highlite -->
The final plugin should have a directory structure that looks something like this:
vendor/plugins/yaffle/ -|-- MIT-LICENSE +|-- MIT-LICENSE |-- README |-- Rakefile |-- generators -| `-- yaffle +| |-- yaffle_definition +| | |-- USAGE +| | |-- templates +| | | `-- definition.txt +| | `-- yaffle_definition_generator.rb +| |-- yaffle_migration +| | |-- USAGE +| | |-- templates +| | `-- yaffle_migration_generator.rb +| `-- yaffle_route | |-- USAGE | |-- templates -| | `-- definition.txt -| `-- yaffle_generator.rb +| `-- yaffle_route_generator.rb |-- install.rb |-- lib | |-- app @@ -1834,11 +1869,16 @@ http://www.gnu.org/software/src-highlite --> | | | `-- woodpeckers_helper.rb | | `-- models | | `-- woodpecker.rb +| |-- db +| | `-- migrate +| | `-- 20081116181115_create_birdhouses.rb | |-- yaffle | | |-- acts_as_yaffle.rb | | |-- commands.rb | | `-- core_ext.rb | `-- yaffle.rb +|-- pkg +| `-- yaffle-0.0.1.gem |-- rails | `-- init.rb |-- tasks @@ -1848,7 +1888,9 @@ http://www.gnu.org/software/src-highlite --> | |-- core_ext_test.rb | |-- database.yml | |-- debug.log -| |-- generator_test.rb +| |-- definition_generator_test.rb +| |-- migration_generator_test.rb +| |-- route_generator_test.rb | |-- schema.rb | |-- test_helper.rb | |-- woodpecker_test.rb diff --git a/railties/doc/guides/html/routing_outside_in.html b/railties/doc/guides/html/routing_outside_in.html index 947d0836ce..1d313b619f 100644 --- a/railties/doc/guides/html/routing_outside_in.html +++ b/railties/doc/guides/html/routing_outside_in.html @@ -1454,7 +1454,7 @@ http://www.gnu.org/software/src-highlite -->- If your application has many RESTful routes, using :only and :accept to generate only the routes that you actually need can cut down on memory use and speed up the routing process. +If your application has many RESTful routes, using :only and :except to generate only the routes that you actually need can cut down on memory use and speed up the routing process.