2008-10-27 07:47:01 +01:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" lang = "en" xml:lang = "en" >
< head >
2009-02-04 21:26:08 +01:00
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" / >
< title > The Basics of Creating Rails Plugins< / title >
<!-- [if lt IE 8]>
< script src = "http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type = "text/javascript" > < / script >
<![endif]-->
< link href = "stylesheets/base.css" media = "screen" rel = "Stylesheet" type = "text/css" / >
< link href = "stylesheets/forms.css" media = "screen" rel = "Stylesheet" type = "text/css" / >
< link href = "stylesheets/more.css" media = "screen" rel = "Stylesheet" type = "text/css" / >
2008-10-27 07:47:01 +01:00
< / head >
< body >
2009-02-04 21:26:08 +01:00
< div id = "header" >
< div id = "logo" >
< a href = "index.html" title = "Ruby on Rails" > < img src = "images/rails_logo_remix.gif" alt = "Rails" height = "140" width = "110" / > < / a >
< / div >
< h1 id = "site_title" > < span > Ruby on Rails< / span > < / h1 >
< h2 id = "site_title_tagline" > Sustainable productivity for web-application development< / h2 >
2008-10-27 07:47:01 +01:00
2009-02-04 21:26:08 +01:00
< ul id = "navMain" >
< li class = "first-child" > < a href = "http://www.rubyonrails.org/" title = "Ruby on Rails" class = "ruby_on_rails" > Ruby on Rails< / a > < / li >
< li > < a class = "manuals" href = "index.html" title = "Manuals Index" > Guides Index< / a > < / li >
< / ul >
< / div >
2008-10-27 07:47:01 +01:00
2009-02-04 21:26:08 +01:00
< div id = "container" >
< div id = "sidebar" >
< h2 > Chapters< / h2 >
< ol >
< li >
< a href = "#_setup" > Setup< / a >
< ul >
< li > < a href = "#_create_the_basic_app" > Create the basic app< / a > < / li >
< li > < a href = "#_generate_the_plugin_skeleton" > Generate the plugin skeleton< / a > < / li >
< li > < a href = "#_organize_your_files" > Organize your files< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_tests" > Tests< / a >
< ul >
< li > < a href = "#_test_setup" > Test Setup< / a > < / li >
< li > < a href = "#_run_the_plugin_tests" > Run the plugin tests< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_extending_core_classes" > Extending core classes< / a >
< ul >
< li > < a href = "#_working_with_init_rb" > Working with init.rb< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_add_an_em_acts_as_em_method_to_active_record" > Add an < em > acts_as< / em > method to Active Record< / a >
< ul >
< li > < a href = "#_add_a_class_method" > Add a class method< / a > < / li >
< li > < a href = "#_add_an_instance_method" > Add an instance method< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_models" > Models< / a >
< / li >
< li >
< a href = "#_controllers" > Controllers< / a >
< / li >
< li >
< a href = "#_helpers" > Helpers< / a >
< / li >
< li >
< a href = "#_routes" > Routes< / a >
< / li >
< li >
< a href = "#_generators" > Generators< / a >
< ul >
< li > < a href = "#_testing_generators" > Testing generators< / a > < / li >
< li > < a href = "#_the_usage_file" > The USAGE file< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_generator_commands" > Generator Commands< / a >
< / li >
< li >
< a href = "#_migrations" > Migrations< / a >
< ul >
< li > < a href = "#_create_a_custom_rake_task" > Create a custom rake task< / a > < / li >
< li > < a href = "#_call_migrations_directly" > Call migrations directly< / a > < / li >
< li > < a href = "#_generate_migrations" > Generate migrations< / a > < / li >
< / ul >
< / li >
< li >
< a href = "#_rake_tasks" > Rake tasks< / a >
< / li >
< li >
< a href = "#_plugingems" > PluginGems< / a >
< / li >
< li >
< a href = "#_rdoc_documentation" > RDoc Documentation< / a >
< / li >
< li >
< a href = "#_appendix" > Appendix< / a >
< ul >
< li > < a href = "#_references" > References< / a > < / li >
< li > < a href = "#_contents_of_em_lib_yaffle_rb_em" > Contents of < em > lib/yaffle.rb< / em > < / a > < / li >
< li > < a href = "#_final_plugin_directory_structure" > Final plugin directory structure< / a > < / li >
< / ul >
< / li >
< / ol >
< / div >
< div id = "content" >
< h1 > The Basics of Creating Rails Plugins< / h1 >
< div id = "preamble" >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > A Rails plugin is either an extension or a modification of the core framework. Plugins provide:< / p > < / div >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
2008-11-24 22:53:39 +01:00
a way for developers to share bleeding-edge ideas without hurting the stable code base
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
a segmented architecture so that units of code can be fixed or updated on their own release schedule
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
an outlet for the core developers so that they don’ t have to include every cool new feature under the sun
2008-10-27 07:47:01 +01:00
< / p >
< / li >
2008-11-24 22:53:39 +01:00
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > After reading this guide you should be familiar with:< / p > < / div >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
2008-11-24 22:53:39 +01:00
Creating a plugin from scratch
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
Writing and running tests for the plugin
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
Storing models, views, controllers, helpers and even other plugins in your plugins
< / p >
< / li >
< li >
< p >
Writing generators
< / p >
< / li >
< li >
< p >
Writing custom Rake tasks in your plugin
< / p >
< / li >
< li >
< p >
Generating RDoc documentation for your plugin
< / p >
< / li >
< li >
< p >
Avoiding common pitfalls with < em > init.rb< / em >
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > This guide describes how to build a test-driven plugin that will:< / p > < / div >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
2008-11-24 22:53:39 +01:00
Extend core ruby classes like Hash and String
< / p >
< / li >
< li >
< p >
Add methods to ActiveRecord::Base in the tradition of the < em > acts_as< / em > plugins
< / p >
< / li >
< li >
< p >
Add a view helper that can be used in erb templates
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
Add a new generator that will generate a migration
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
Add a custom generator command
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
A custom route method that can be used in routes.rb
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.< / p > < / div >
2008-10-27 07:47:01 +01:00
< / div >
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_setup" > 1. Setup< / h2 >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
< h3 id = "_create_the_basic_app" > 1.1. Create the basic app< / h3 >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > The examples in this guide require that you have a working rails application. To create a simple rails app execute:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
2008-11-24 22:53:39 +01:00
< pre > < tt > gem install rails
rails yaffle_guide
cd yaffle_guide
2008-10-27 07:47:01 +01:00
script/generate scaffold bird name:string
rake db:migrate
script/server< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > Then navigate to < a href = "http://localhost:3000/birds" > http://localhost:3000/birds< / a > . Make sure you have a functioning rails app before continuing.< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "admonitionblock" >
< table > < tr >
< td class = "icon" >
< img src = "./images/icons/note.png" alt = "Note" / >
< / td >
2008-11-24 22:53:39 +01:00
< td class = "content" >
2009-02-04 21:26:08 +01:00
< div class = "title" > Editor’ s note:< / div > The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.< / td >
2008-10-27 07:47:01 +01:00
< / tr > < / table >
< / div >
2008-11-24 22:53:39 +01:00
< h3 id = "_generate_the_plugin_skeleton" > 1.2. Generate the plugin skeleton< / h3 >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either < em > CamelCased< / em > or < em > under_scored< / em > , as an argument. Pass < tt > --with-generator< / tt > to add an example generator also.< / p > < / div >
< div class = "paragraph" > < p > This creates a plugin in < em > vendor/plugins< / em > including an < em > init.rb< / em > and < em > README< / em > as well as standard < em > lib< / em > , < em > task< / em > , and < em > test< / em > directories.< / p > < / div >
< div class = "paragraph" > < p > Examples:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
2008-11-24 22:53:39 +01:00
< pre > < tt > ./script/generate plugin yaffle
./script/generate plugin yaffle --with-generator< / tt > < / pre >
2008-10-27 07:47:01 +01:00
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > To get more detailed help on the plugin generator, type < tt > ./script/generate plugin< / tt > .< / p > < / div >
< div class = "paragraph" > < p > Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the < tt > --with-generator< / tt > option now:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
2008-11-24 22:53:39 +01:00
< pre > < tt > ./script/generate plugin yaffle --with-generator< / tt > < / pre >
2008-10-27 07:47:01 +01:00
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > You should see the following output:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > create vendor/plugins/yaffle/lib
create vendor/plugins/yaffle/tasks
create vendor/plugins/yaffle/test
create vendor/plugins/yaffle/README
create vendor/plugins/yaffle/MIT-LICENSE
create vendor/plugins/yaffle/Rakefile
create vendor/plugins/yaffle/init.rb
create vendor/plugins/yaffle/install.rb
create vendor/plugins/yaffle/uninstall.rb
create vendor/plugins/yaffle/lib/yaffle.rb
create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
create vendor/plugins/yaffle/test/core_ext_test.rb
create vendor/plugins/yaffle/generators
create vendor/plugins/yaffle/generators/yaffle
create vendor/plugins/yaffle/generators/yaffle/templates
create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
create vendor/plugins/yaffle/generators/yaffle/USAGE< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< h3 id = "_organize_your_files" > 1.3. Organize your files< / h3 >
< div class = "paragraph" > < p > To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > |-- lib
| |-- yaffle
| `-- yaffle.rb
`-- rails
|
`-- init.rb< / tt > < / pre >
< / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/rails/init.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'yaffle'< / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Now you can add any < em > require< / em > statements to < em > lib/yaffle.rb< / em > and keep < em > init.rb< / em > clean.< / p > < / div >
< / div >
< h2 id = "_tests" > 2. Tests< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you’ ll need to add 3 files:< / p > < / div >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
2008-11-24 22:53:39 +01:00
A < em > database.yml< / em > file with all of your connection strings
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
A < em > schema.rb< / em > file with your table definitions
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2008-11-24 22:53:39 +01:00
A test helper method that sets up the database
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< h3 id = "_test_setup" > 2.1. Test Setup< / h3 >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/database.yml:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > sqlite:
:adapter: sqlite
2008-11-24 22:53:39 +01:00
:dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
2008-10-27 07:47:01 +01:00
sqlite3:
:adapter: sqlite3
2008-11-24 22:53:39 +01:00
:dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
2008-10-27 07:47:01 +01:00
postgresql:
:adapter: postgresql
:username: postgres
:password: postgres
:database: yaffle_plugin_test
:min_messages: ERROR
mysql:
:adapter: mysql
:host: localhost
2008-11-24 22:53:39 +01:00
:username: root
:password: password
2008-10-27 07:47:01 +01:00
:database: yaffle_plugin_test< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > For this guide you’ ll need 2 tables/models, Hickwalls and Wickwalls, so add the following:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/schema.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > ActiveRecord< span style = "color: #990000" > ::< / span > Schema< span style = "color: #990000" > .< / span > define< span style = "color: #990000" > (:< / span > version < span style = "color: #990000" > => < / span > < span style = "color: #993399" > 0< / span > < span style = "color: #990000" > )< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
create_table < span style = "color: #990000" > :< / span > hickwalls< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > force < span style = "color: #990000" > => < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > t< span style = "color: #990000" > |< / span >
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > name
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > last_squawk
t< span style = "color: #990000" > .< / span > datetime < span style = "color: #990000" > :< / span > last_squawked_at
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
create_table < span style = "color: #990000" > :< / span > wickwalls< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > force < span style = "color: #990000" > => < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > t< span style = "color: #990000" > |< / span >
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > name
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > last_tweet
t< span style = "color: #990000" > .< / span > datetime < span style = "color: #990000" > :< / span > last_tweeted_at
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
create_table < span style = "color: #990000" > :< / span > woodpeckers< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > force < span style = "color: #990000" > => < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > t< span style = "color: #990000" > |< / span >
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > name
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/test_helper.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > 'RAILS_ENV'< / span > < span style = "color: #990000" > ]< / span > < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > 'test'< / span >
2008-10-27 07:47:01 +01:00
ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > 'RAILS_ROOT'< / span > < span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ||=< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/../../../..'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'test/unit'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > expand_path< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > 'RAILS_ROOT'< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #FF0000" > 'config/environment.rb'< / span > < span style = "color: #990000" > ))< / span >
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > load_schema
config < span style = "color: #990000" > =< / span > YAML< span style = "color: #990000" > ::< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > load< / span > < / span > < span style = "color: #990000" > (< / span > IO< span style = "color: #990000" > .< / span > read< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/database.yml'< / span > < span style = "color: #990000" > ))< / span >
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > logger < span style = "color: #990000" > =< / span > Logger< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > "/debug.log"< / span > < span style = "color: #990000" > )< / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
db_adapter < span style = "color: #990000" > =< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > 'DB'< / span > < span style = "color: #990000" > ]< / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-style: italic" > < span style = "color: #9A1900" > # no db passed, try one of these fine config-free DBs before bombing.< / span > < / span >
db_adapter < span style = "color: #990000" > ||=< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > begin< / span > < / span >
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rubygems'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'sqlite'< / span >
< span style = "color: #FF0000" > 'sqlite'< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > rescue< / span > < / span > MissingSourceFile
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > begin< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'sqlite3'< / span >
< span style = "color: #FF0000" > 'sqlite3'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > rescue< / span > < / span > MissingSourceFile
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > if< / span > < / span > db_adapter< span style = "color: #990000" > .< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > nil< / span > < / span > < span style = "color: #990000" > ?< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > raise< / span > < / span > < span style = "color: #FF0000" > "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > establish_connection< span style = "color: #990000" > (< / span > config< span style = "color: #990000" > [< / span > db_adapter< span style = "color: #990000" > ])< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > load< / span > < / span > < span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > "/schema.rb"< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/../rails/init.rb'< / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Now whenever you write a test that requires the database, you can call < em > load_schema< / em > .< / p > < / div >
< h3 id = "_run_the_plugin_tests" > 2.2. Run the plugin tests< / h3 >
< div class = "paragraph" > < p > Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in < em > vendor/plugins/yaffle/test/yaffle_test.rb< / em > with a sample test. Replace the contents of that file with:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/yaffle_test.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > YaffleTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
load_schema
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Hickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Wickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_schema_has_loaded_correctly
assert_equal < span style = "color: #990000" > [],< / span > Hickwall< span style = "color: #990000" > .< / span > all
assert_equal < span style = "color: #990000" > [],< / span > Wickwall< span style = "color: #990000" > .< / span > all
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > To run this, go to the plugin directory and run < tt > rake< / tt > :< / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > cd vendor/plugins/yaffle
rake< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > You should see output like:< / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > /opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
-- create_table(:hickwalls, {:force=> true})
-> 0.0220s
-- create_table(:wickwalls, {:force=> true})
-> 0.0077s
-- initialize_schema_migrations_table()
-> 0.0007s
-- assume_migrated_upto_version(0)
-> 0.0007s
Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
.
Finished in 0.002236 seconds.
1 test, 1 assertion, 0 failures, 0 errors< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:< / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > rake DB=sqlite
rake DB=sqlite3
rake DB=mysql
rake DB=postgresql< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > Now you are ready to test-drive your plugin!< / p > < / div >
2008-10-27 07:47:01 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_extending_core_classes" > 3. Extending core classes< / h2 >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > This section will explain how to add a method to String that will be available anywhere in your rails app.< / p > < / div >
< div class = "paragraph" > < p > In this example you will add a method to String named < tt > to_squawk< / tt > . To begin, create a new test file with a few assertions:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/core_ext_test.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > CoreExtTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_to_squawk_prepends_the_word_squawk
assert_equal < span style = "color: #FF0000" > "squawk! Hello World"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "Hello World"< / span > < span style = "color: #990000" > .< / span > to_squawk
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Navigate to your plugin directory and run < tt > rake test< / tt > :< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > cd vendor/plugins/yaffle
rake test< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > The test above should fail with the message:< / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > 1) Error:
test_to_squawk_prepends_the_word_squawk(CoreExtTest):
NoMethodError: undefined method `to_squawk' for "Hello World":String
./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > Great - now you are ready to start development.< / p > < / div >
< div class = "paragraph" > < p > Then in < em > lib/yaffle.rb< / em > require < em > lib/core_ext.rb< / em > :< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/core_ext"< / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Finally, create the < em > core_ext.rb< / em > file and add the < em > to_squawk< / em > method:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/core_ext.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > String< span style = "color: #990000" > .< / span > class_eval < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > to_squawk
< span style = "color: #FF0000" > "squawk! #{self}"< / span > < span style = "color: #990000" > .< / span > strip
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > To test that your method does what it says it does, run the unit tests with < tt > rake< / tt > from your plugin directory. To see this in action, fire up a console and start squawking:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > $ ./script/console
> > "Hello World".to_squawk
=> "squawk! Hello World"< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< h3 id = "_working_with_init_rb" > 3.1. Working with init.rb< / h3 >
< div class = "paragraph" > < p > When rails loads plugins it looks for the file named < em > init.rb< / em > or < em > rails/init.rb< / em > . However, when the plugin is initialized, < em > init.rb< / em > is invoked via < tt > eval< / tt > (not < tt > require< / tt > ) so it has slightly different behavior.< / p > < / div >
< div class = "paragraph" > < p > Under certain circumstances if you reopen classes or modules in < em > init.rb< / em > you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from < tt > init.rb< / tt > , as shown above.< / p > < / div >
< div class = "paragraph" > < p > If you must reopen a class in < tt > init.rb< / tt > you can use < tt > module_eval< / tt > or < tt > class_eval< / tt > to avoid any issues:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/rails/init.rb< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > Hash< span style = "color: #990000" > .< / span > class_eval < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > is_a_special_hash?
< span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Another way is to explicitly define the top-level module space for all modules and classes, like < tt > ::Hash< / tt > :< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/rails/init.rb< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > < span style = "color: #990000" > ::< / span > Hash
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > is_a_special_hash?
< span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
2008-10-27 07:47:01 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_add_an_em_acts_as_em_method_to_active_record" > 4. Add an < em > acts_as< / em > method to Active Record< / h2 >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > A common pattern in plugins is to add a method called < em > acts_as_something< / em > to models. In this case, you want to write a method called < em > acts_as_yaffle< / em > that adds a < em > squawk< / em > method to your models.< / p > < / div >
< div class = "paragraph" > < p > To begin, set up your files so that you have:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/acts_as_yaffle_test.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > ActsAsYaffleTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'yaffle/acts_as_yaffle'< / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle
< span style = "font-style: italic" > < span style = "color: #9A1900" > # your code will go here< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Note that after requiring < em > acts_as_yaffle< / em > you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.< / p > < / div >
< div class = "paragraph" > < p > One of the most common plugin patterns for < em > acts_as_yaffle< / em > plugins is to structure your file like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > included< span style = "color: #990000" > (< / span > base< span style = "color: #990000" > )< / span >
base< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > extend< span style = "color: #990000" > ,< / span > ClassMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > ClassMethods
< span style = "font-style: italic" > < span style = "color: #9A1900" > # any method placed here will apply to classes, like Hickwall< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > acts_as_something
send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > InstanceMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > InstanceMethods
< span style = "font-style: italic" > < span style = "color: #9A1900" > # any method placed here will apply to instaces, like @hickwall< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > With structure you can easily separate the methods that will be used for the class (like < tt > Hickwall.some_method< / tt > ) and the instance (like < tt > @hickwell.some_method< / tt > ).< / p > < / div >
< h3 id = "_add_a_class_method" > 4.1. Add a class method< / h3 >
< div class = "paragraph" > < p > This plugin will expect that you’ ve added a method to your model named < em > last_squawk< / em > . However, the plugin users might have already defined a method on their model named < em > last_squawk< / em > that they use for something else. This plugin will allow the name to be changed by adding a class method called < em > yaffle_text_field< / em > .< / p > < / div >
< div class = "paragraph" > < p > To start out, write a failing test that shows the behavior you’ d like:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/acts_as_yaffle_test.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Hickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
acts_as_yaffle
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Wickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
acts_as_yaffle < span style = "color: #990000" > :< / span > yaffle_text_field < span style = "color: #990000" > => < / span > < span style = "color: #990000" > :< / span > last_tweet
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > ActsAsYaffleTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
2008-11-24 22:53:39 +01:00
load_schema
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal < span style = "color: #FF0000" > "last_squawk"< / span > < span style = "color: #990000" > ,< / span > Hickwall< span style = "color: #990000" > .< / span > yaffle_text_field
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal < span style = "color: #FF0000" > "last_tweet"< / span > < span style = "color: #990000" > ,< / span > Wickwall< span style = "color: #990000" > .< / span > yaffle_text_field
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > To make these tests pass, you could modify your < tt > acts_as_yaffle< / tt > file like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > included< span style = "color: #990000" > (< / span > base< span style = "color: #990000" > )< / span >
base< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > extend< span style = "color: #990000" > ,< / span > ClassMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > ClassMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > acts_as_yaffle< span style = "color: #990000" > (< / span > options < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > {}< / span > < span style = "color: #990000" > )< / span >
2008-11-24 22:53:39 +01:00
cattr_accessor < span style = "color: #990000" > :< / span > yaffle_text_field
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > yaffle_text_field < span style = "color: #990000" > =< / span > < span style = "color: #990000" > (< / span > options< span style = "color: #990000" > [:< / span > yaffle_text_field< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ||< / span > < span style = "color: #990000" > :< / span > last_squawk< span style = "color: #990000" > ).< / span > to_s
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
2009-02-04 21:26:08 +01:00
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< / tt > < / pre > < / div > < / div >
< h3 id = "_add_an_instance_method" > 4.2. Add an instance method< / h3 >
< div class = "paragraph" > < p > This plugin will add a method named < em > squawk< / em > to any Active Record objects that call < em > acts_as_yaffle< / em > . The < em > squawk< / em > method will simply set the value of one of the fields in the database.< / p > < / div >
< div class = "paragraph" > < p > To start out, write a failing test that shows the behavior you’ d like:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/acts_as_yaffle_test.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Hickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
acts_as_yaffle
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Wickwall < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
acts_as_yaffle < span style = "color: #990000" > :< / span > yaffle_text_field < span style = "color: #990000" > => < / span > < span style = "color: #990000" > :< / span > last_tweet
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > ActsAsYaffleTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
2008-11-24 22:53:39 +01:00
load_schema
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal < span style = "color: #FF0000" > "last_squawk"< / span > < span style = "color: #990000" > ,< / span > Hickwall< span style = "color: #990000" > .< / span > yaffle_text_field
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_a_wickwalls_yaffle_text_field_should_be_last_tweet
2008-10-27 07:47:01 +01:00
assert_equal < span style = "color: #FF0000" > "last_tweet"< / span > < span style = "color: #990000" > ,< / span > Wickwall< span style = "color: #990000" > .< / span > yaffle_text_field
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_hickwalls_squawk_should_populate_last_squawk
hickwall < span style = "color: #990000" > =< / span > Hickwall< span style = "color: #990000" > .< / span > new
hickwall< span style = "color: #990000" > .< / span > squawk< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "Hello World"< / span > < span style = "color: #990000" > )< / span >
assert_equal < span style = "color: #FF0000" > "squawk! Hello World"< / span > < span style = "color: #990000" > ,< / span > hickwall< span style = "color: #990000" > .< / span > last_squawk
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_wickwalls_squawk_should_populate_last_tweeted_at
wickwall < span style = "color: #990000" > =< / span > Wickwall< span style = "color: #990000" > .< / span > new
wickwall< span style = "color: #990000" > .< / span > squawk< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "Hello World"< / span > < span style = "color: #990000" > )< / span >
2008-11-24 22:53:39 +01:00
assert_equal < span style = "color: #FF0000" > "squawk! Hello World"< / span > < span style = "color: #990000" > ,< / span > wickwall< span style = "color: #990000" > .< / span > last_tweet
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Run this test to make sure the last two tests fail, then update < em > acts_as_yaffle.rb< / em > to look like this:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > included< span style = "color: #990000" > (< / span > base< span style = "color: #990000" > )< / span >
base< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > extend< span style = "color: #990000" > ,< / span > ClassMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > ClassMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > acts_as_yaffle< span style = "color: #990000" > (< / span > options < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > {}< / span > < span style = "color: #990000" > )< / span >
2008-11-24 22:53:39 +01:00
cattr_accessor < span style = "color: #990000" > :< / span > yaffle_text_field
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > yaffle_text_field < span style = "color: #990000" > =< / span > < span style = "color: #990000" > (< / span > options< span style = "color: #990000" > [:< / span > yaffle_text_field< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ||< / span > < span style = "color: #990000" > :< / span > last_squawk< span style = "color: #990000" > ).< / span > to_s
send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > InstanceMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > InstanceMethods
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > squawk< span style = "color: #990000" > (< / span > string< span style = "color: #990000" > )< / span >
write_attribute< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > < span style = "color: #990000" > .< / span > yaffle_text_field< span style = "color: #990000" > ,< / span > string< span style = "color: #990000" > .< / span > to_squawk< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2008-11-24 22:53:39 +01:00
2009-02-04 21:26:08 +01:00
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< / tt > < / pre > < / div > < / div >
2008-11-24 22:53:39 +01:00
< div class = "admonitionblock" >
< table > < tr >
< td class = "icon" >
< img src = "./images/icons/note.png" alt = "Note" / >
< / td >
< td class = "content" >
2009-02-04 21:26:08 +01:00
< div class = "title" > Editor’ s note:< / div > The use of < tt > write_attribute< / tt > to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use < tt > send("#{self.class.yaffle_text_field}=", string.to_squawk)< / tt > .< / td >
2008-11-24 22:53:39 +01:00
< / tr > < / table >
< / div >
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_models" > 5. Models< / h2 >
2008-11-24 22:53:39 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > This section describes how to add a model named < em > Woodpecker< / em > to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it’ s customary to keep them in directories that match the rails directories. For this example, create a file structure like this:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > vendor/plugins/yaffle/
|-- lib
| |-- app
| | |-- controllers
| | |-- helpers
| | |-- models
| | | `-- woodpecker.rb
| | `-- views
| |-- yaffle
| | |-- acts_as_yaffle.rb
| | |-- commands.rb
| | `-- core_ext.rb
| `-- yaffle.rb< / tt > < / pre >
< / div > < / div >
< div class = "paragraph" > < p > As always, start with a test:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/woodpecker_test.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
2008-10-27 07:47:01 +01:00
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > WoodpeckerTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
load_schema
2008-11-24 22:53:39 +01:00
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_woodpecker
assert_kind_of Woodpecker< span style = "color: #990000" > ,< / span > Woodpecker< span style = "color: #990000" > .< / span > new
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > This is just a simple test to make sure the class is being loaded correctly. After watching it fail with < tt > rake< / tt > , you can make it pass like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "color: #990000" > %< / span > w< span style = "color: #FF0000" > {< / span > models < span style = "color: #FF0000" > }< / span > < span style = "color: #990000" > .< / span > each < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > dir< span style = "color: #990000" > |< / span >
path < span style = "color: #990000" > =< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'app'< / span > < span style = "color: #990000" > ,< / span > dir< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > $LOAD_PATH< / span > < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_paths < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_once_paths< span style = "color: #990000" > .< / span > delete< span style = "color: #990000" > (< / span > path< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the < em > load_once_paths< / em > allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/app/models/woodpecker.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > Woodpecker < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Finally, add the following to your plugin’ s < em > schema.rb< / em > :< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/schema.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > create_table < span style = "color: #990000" > :< / span > woodpeckers< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > force < span style = "color: #990000" > => < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > t< span style = "color: #990000" > |< / span >
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > name
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.< / p > < / div >
< / div >
< h2 id = "_controllers" > 6. Controllers< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > This section describes how to add a controller named < em > woodpeckers< / em > to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.< / p > < / div >
< div class = "paragraph" > < p > You can test your plugin’ s controller as you would test any other controller:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'woodpeckers_controller'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'action_controller/test_process'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > WoodpeckersController< span style = "color: #990000" > ;< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > rescue_action< span style = "color: #990000" > (< / span > e< span style = "color: #990000" > )< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > raise< / span > < / span > e < span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < span style = "color: #990000" > ;< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > WoodpeckersControllerTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > setup
< span style = "color: #009900" > @controller< / span > < span style = "color: #990000" > =< / span > WoodpeckersController< span style = "color: #990000" > .< / span > new
< span style = "color: #009900" > @request< / span > < span style = "color: #990000" > =< / span > ActionController< span style = "color: #990000" > ::< / span > TestRequest< span style = "color: #990000" > .< / span > new
< span style = "color: #009900" > @response< / span > < span style = "color: #990000" > =< / span > ActionController< span style = "color: #990000" > ::< / span > TestResponse< span style = "color: #990000" > .< / span > new
ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > draw < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > map< span style = "color: #990000" > |< / span >
map< span style = "color: #990000" > .< / span > resources < span style = "color: #990000" > :< / span > woodpeckers
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_index
get < span style = "color: #990000" > :< / span > index
assert_response < span style = "color: #990000" > :< / span > success
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with < tt > rake< / tt > , you can make it pass like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "color: #990000" > %< / span > w< span style = "color: #FF0000" > {< / span > models controllers < span style = "color: #FF0000" > }< / span > < span style = "color: #990000" > .< / span > each < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > dir< span style = "color: #990000" > |< / span >
path < span style = "color: #990000" > =< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'app'< / span > < span style = "color: #990000" > ,< / span > dir< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > $LOAD_PATH< / span > < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_paths < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_once_paths< span style = "color: #990000" > .< / span > delete< span style = "color: #990000" > (< / span > path< span style = "color: #990000" > )< / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > WoodpeckersController < span style = "color: #990000" > < < / span > ActionController< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > index
render < span style = "color: #990000" > :< / span > text < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "Squawk!"< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to < a href = "http://localhost:3000/woodpeckers" > http://localhost:3000/woodpeckers< / a > to see your controller in action.< / p > < / div >
2008-11-24 22:53:39 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_helpers" > 7. Helpers< / h2 >
2008-11-24 22:53:39 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > This section describes how to add a helper named < em > WoodpeckersHelper< / em > to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.< / p > < / div >
< div class = "paragraph" > < p > You can test your plugin’ s helper as you would test any other helper:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/woodpeckers_helper_test.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > WoodpeckersHelper
2008-10-27 07:47:01 +01:00
2008-11-24 22:53:39 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > WoodpeckersHelperTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_tweet
assert_equal < span style = "color: #FF0000" > "Tweet! Hello"< / span > < span style = "color: #990000" > ,< / span > tweet< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "Hello"< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with < tt > rake< / tt > , you can make it pass like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "color: #990000" > %< / span > w< span style = "color: #FF0000" > {< / span > models controllers helpers < span style = "color: #FF0000" > }< / span > < span style = "color: #990000" > .< / span > each < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > dir< span style = "color: #990000" > |< / span >
path < span style = "color: #990000" > =< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'app'< / span > < span style = "color: #990000" > ,< / span > dir< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > $LOAD_PATH< / span > < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_paths < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_once_paths< span style = "color: #990000" > .< / span > delete< span style = "color: #990000" > (< / span > path< span style = "color: #990000" > )< / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > WoodpeckersHelper
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > tweet< span style = "color: #990000" > (< / span > text< span style = "color: #990000" > )< / span >
< span style = "color: #FF0000" > "Tweet! #{text}"< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.< / p > < / div >
2008-11-24 22:53:39 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_routes" > 8. Routes< / h2 >
2008-11-24 22:53:39 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > In a standard < em > routes.rb< / em > file you use routes like < em > map.connect< / em > or < em > map.resources< / em > . You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with < em > map.yaffles< / em > .< / p > < / div >
< div class = "paragraph" > < p > Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/routing_test.rb< / strong > < / p > < / div >
2008-11-24 22:53:39 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "#{File.dirname(__FILE__)}/test_helper"< / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > RoutingTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > setup
ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > draw < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > map< span style = "color: #990000" > |< / span >
map< span style = "color: #990000" > .< / span > yaffles
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_yaffles_route
assert_recognition < span style = "color: #990000" > :< / span > get< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "/yaffles"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > controller < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "yaffles_controller"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > action < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "index"< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
private
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > assert_recognition< span style = "color: #990000" > (< / span > method< span style = "color: #990000" > ,< / span > path< span style = "color: #990000" > ,< / span > options< span style = "color: #990000" > )< / span >
result < span style = "color: #990000" > =< / span > ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > recognize_path< span style = "color: #990000" > (< / span > path< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > method < span style = "color: #990000" > => < / span > method< span style = "color: #990000" > )< / span >
assert_equal options< span style = "color: #990000" > ,< / span > result
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Once you see the tests fail by running < em > rake< / em > , you can make them pass with:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/routing"< / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/routing.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle < span style = "font-style: italic" > < span style = "color: #9A1900" > #:nodoc:< / span > < / span >
2008-10-27 07:47:01 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Routing < span style = "font-style: italic" > < span style = "color: #9A1900" > #:nodoc:< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > MapperExtensions
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffles
< span style = "color: #009900" > @set< / span > < span style = "color: #990000" > .< / span > add_route< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "/yaffles"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > {< / span > < span style = "color: #990000" > :< / span > controller < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "yaffles_controller"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > action < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "index"< / span > < span style = "color: #FF0000" > }< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > RouteSet< span style = "color: #990000" > ::< / span > Mapper< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > MapperExtensions< / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > config/routes.rb< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > draw < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > map< span style = "color: #990000" > |< / span >
2008-10-27 07:47:01 +01:00
map< span style = "color: #990000" > .< / span > yaffles
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > You can also see if your routes work by running < tt > rake routes< / tt > from your app directory.< / p > < / div >
2008-10-27 07:47:01 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_generators" > 9. Generators< / h2 >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > 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 < em > vendor/plugins/yaffle/generators/yaffle< / em > .< / p > < / div >
< div class = "paragraph" > < p > Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.< / p > < / div >
< h3 id = "_testing_generators" > 9.1. Testing generators< / h3 >
< div class = "paragraph" > < p > Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:< / p > < / div >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
2009-02-04 21:26:08 +01:00
Creates a new fake rails root directory that will serve as destination
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2009-02-04 21:26:08 +01:00
Runs the generator
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2009-02-04 21:26:08 +01:00
Asserts that the correct files were generated
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
2009-02-04 21:26:08 +01:00
Removes the fake rails root
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > 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:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/definition_generator_test.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator/scripts/generate'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > DefinitionGeneratorTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > setup
FileUtils< span style = "color: #990000" > .< / span > mkdir_p< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > @original_files< / span > < span style = "color: #990000" > =< / span > file_list
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > teardown
FileUtils< span style = "color: #990000" > .< / span > rm_r< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_generates_correct_file_name
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Scripts< span style = "color: #990000" > ::< / span > Generate< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > .< / span > run< span style = "color: #990000" > ([< / span > < span style = "color: #FF0000" > "yaffle_definition"< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #990000" > :< / span > destination < span style = "color: #990000" > => < / span > fake_rails_root< span style = "color: #990000" > )< / span >
new_file < span style = "color: #990000" > =< / span > < span style = "color: #990000" > (< / span > file_list < span style = "color: #990000" > -< / span > < span style = "color: #009900" > @original_files< / span > < span style = "color: #990000" > ).< / span > first
assert_equal < span style = "color: #FF0000" > "definition.txt"< / span > < span style = "color: #990000" > ,< / span > File< span style = "color: #990000" > .< / span > basename< span style = "color: #990000" > (< / span > new_file< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
private
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > fake_rails_root
File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'rails_root'< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > file_list
Dir< span style = "color: #990000" > .< / span > glob< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "*"< / span > < span style = "color: #990000" > ))< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > You can run < em > rake< / em > 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.< / p > < / div >
< div class = "paragraph" > < p > To make it pass, create the generator:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > YaffleDefinitionGenerator < span style = "color: #990000" > < < / span > Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > manifest
record < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > m< span style = "color: #990000" > |< / span >
m< span style = "color: #990000" > .< / span > file < span style = "color: #FF0000" > "definition.txt"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "definition.txt"< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< h3 id = "_the_usage_file" > 9.2. The USAGE file< / h3 >
< div class = "paragraph" > < p > 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.< / p > < / div >
< div class = "paragraph" > < p > 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:< / p > < / div >
< div class = "listingblock" >
2008-10-27 07:47:01 +01:00
< div class = "content" >
2009-02-04 21:26:08 +01:00
< pre > < tt > ./script/generate< / tt > < / pre >
< / div > < / div >
< div class = "paragraph" > < p > You should see something like this:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > Installed Generators
Plugins (vendor/plugins): yaffle_definition
Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration< / tt > < / pre >
< / div > < / div >
< div class = "paragraph" > < p > When you run < tt > script/generate yaffle_definition -h< / tt > you should see the contents of your < em > vendor/plugins/yaffle/generators/yaffle_definition/USAGE< / em > .< / p > < / div >
< div class = "paragraph" > < p > For this plugin, update the USAGE file could look like this:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > Description:
Adds a file with the definition of a Yaffle to the app's main directory< / tt > < / pre >
< / div > < / div >
< / div >
< h2 id = "_generator_commands" > 10. Generator Commands< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > You may have noticed above that you can used one of the built-in rails migration commands < tt > migration_template< / tt > . If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.< / p > < / div >
< div class = "paragraph" > < p > This section describes how you you can create your own commands to add and remove a line of text from < em > config/routes.rb< / em > .< / p > < / div >
< div class = "paragraph" > < p > To start, add the following test method:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/route_generator_test.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator/scripts/generate'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator/scripts/destroy'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > RouteGeneratorTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > setup
FileUtils< span style = "color: #990000" > .< / span > mkdir_p< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "config"< / span > < span style = "color: #990000" > ))< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > teardown
FileUtils< span style = "color: #990000" > .< / span > rm_r< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_generates_route
content < span style = "color: #990000" > =< / span > < span style = "color: #990000" > < < -< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > END< / span > < / span >
ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > draw < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > map< span style = "color: #990000" > |< / span >
map< span style = "color: #990000" > .< / span > connect < span style = "color: #FF0000" > ':controller/:action/:id'< / span >
map< span style = "color: #990000" > .< / span > connect < span style = "color: #FF0000" > ':controller/:action/:id.:format'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > END< / span > < / span >
File< span style = "color: #990000" > .< / span > open< span style = "color: #990000" > (< / span > routes_path< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > 'wb'< / span > < span style = "color: #990000" > )< / span > < span style = "color: #FF0000" > {< / span > < span style = "color: #990000" > |< / span > f< span style = "color: #990000" > |< / span > f< span style = "color: #990000" > .< / span > write< span style = "color: #990000" > (< / span > content< span style = "color: #990000" > )< / span > < span style = "color: #FF0000" > }< / span >
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Scripts< span style = "color: #990000" > ::< / span > Generate< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > .< / span > run< span style = "color: #990000" > ([< / span > < span style = "color: #FF0000" > "yaffle_route"< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #990000" > :< / span > destination < span style = "color: #990000" > => < / span > fake_rails_root< span style = "color: #990000" > )< / span >
assert_match < span style = "color: #FF6600" > /map\.yaffles/< / span > < span style = "color: #990000" > ,< / span > File< span style = "color: #990000" > .< / span > read< span style = "color: #990000" > (< / span > routes_path< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_destroys_route
content < span style = "color: #990000" > =< / span > < span style = "color: #990000" > < < -< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > END< / span > < / span >
ActionController< span style = "color: #990000" > ::< / span > Routing< span style = "color: #990000" > ::< / span > Routes< span style = "color: #990000" > .< / span > draw < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > map< span style = "color: #990000" > |< / span >
map< span style = "color: #990000" > .< / span > yaffles
map< span style = "color: #990000" > .< / span > connect < span style = "color: #FF0000" > ':controller/:action/:id'< / span >
map< span style = "color: #990000" > .< / span > connect < span style = "color: #FF0000" > ':controller/:action/:id.:format'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > END< / span > < / span >
File< span style = "color: #990000" > .< / span > open< span style = "color: #990000" > (< / span > routes_path< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > 'wb'< / span > < span style = "color: #990000" > )< / span > < span style = "color: #FF0000" > {< / span > < span style = "color: #990000" > |< / span > f< span style = "color: #990000" > |< / span > f< span style = "color: #990000" > .< / span > write< span style = "color: #990000" > (< / span > content< span style = "color: #990000" > )< / span > < span style = "color: #FF0000" > }< / span >
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Scripts< span style = "color: #990000" > ::< / span > Destroy< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > .< / span > run< span style = "color: #990000" > ([< / span > < span style = "color: #FF0000" > "yaffle_route"< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #990000" > :< / span > destination < span style = "color: #990000" > => < / span > fake_rails_root< span style = "color: #990000" > )< / span >
assert_no_match < span style = "color: #FF6600" > /map\.yaffles/< / span > < span style = "color: #990000" > ,< / span > File< span style = "color: #990000" > .< / span > read< span style = "color: #990000" > (< / span > routes_path< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
private
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > fake_rails_root
File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > "rails_root"< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > routes_path
File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "config"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "routes.rb"< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Run < tt > rake< / tt > to watch the test fail, then make the test pass add the following:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/commands"< / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle/commands.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator/commands'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Yaffle < span style = "font-style: italic" > < span style = "color: #9A1900" > #:nodoc:< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Generator < span style = "font-style: italic" > < span style = "color: #9A1900" > #:nodoc:< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Commands < span style = "font-style: italic" > < span style = "color: #9A1900" > #:nodoc:< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Create
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffle_route
logger< span style = "color: #990000" > .< / span > route < span style = "color: #FF0000" > "map.yaffle"< / span >
look_for < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > 'ActionController::Routing::Routes.draw do |map|'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > unless< / span > < / span > options< span style = "color: #990000" > [:< / span > pretend< span style = "color: #990000" > ]< / span >
gsub_file< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > 'config/routes.rb'< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF6600" > /(#{Regexp.escape(look_for)})/< / span > mi< span style = "color: #990000" > )< / span > < span style = "color: #FF0000" > {< / span > < span style = "color: #990000" > |< / span > match< span style = "color: #990000" > |< / span > < span style = "color: #FF0000" > "#{match}\n map.yaffles\n"< / span > < span style = "color: #FF0000" > }< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Destroy
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffle_route
logger< span style = "color: #990000" > .< / span > route < span style = "color: #FF0000" > "map.yaffle"< / span >
gsub_file < span style = "color: #FF0000" > 'config/routes.rb'< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF6600" > /\n.+?map\.yaffles/< / span > mi< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > ''< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > List
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffle_route
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > module< / span > < / span > Update
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffle_route
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Create< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Create
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Destroy< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Destroy
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > List< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > List
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Update< span style = "color: #990000" > .< / span > send < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > include< / span > < / span > < span style = "color: #990000" > ,< / span > Yaffle< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Commands< span style = "color: #990000" > ::< / span > Update< / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/generators/yaffle_route/yaffle_route_generator.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > YaffleRouteGenerator < span style = "color: #990000" > < < / span > Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Base
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > manifest
record < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > m< span style = "color: #990000" > |< / span >
m< span style = "color: #990000" > .< / span > yaffle_route
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > To see this work, type:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > ./script/generate yaffle_route
./script/destroy yaffle_route< / tt > < / pre >
< / div > < / div >
< div class = "admonitionblock" >
< table > < tr >
< td class = "icon" >
< img src = "./images/icons/note.png" alt = "Note" / >
< / td >
< td class = "content" >
< div class = "title" > Editor’ s note:< / div > If you haven’ t set up the custom route from above, < em > script/destroy< / em > will fail and you’ ll have to remove it manually.< / td >
< / tr > < / table >
< / div >
< / div >
< h2 id = "_migrations" > 11. Migrations< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > If your plugin requires changes to the app’ s database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.< / p > < / div >
< div class = "paragraph" > < p > If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.< / p > < / div >
< div class = "paragraph" > < p > Let’ s say you have the following migration in your plugin:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > CreateBirdhouses < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Migration
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > up
create_table < span style = "color: #990000" > :< / span > birdhouses< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > force < span style = "color: #990000" > => < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > t< span style = "color: #990000" > |< / span >
t< span style = "color: #990000" > .< / span > string < span style = "color: #990000" > :< / span > name
t< span style = "color: #990000" > .< / span > timestamps
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > down
drop_table < span style = "color: #990000" > :< / span > birdhouses
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > Here are a few possibilities for how to allow developers to use your plugin migrations:< / p > < / div >
< h3 id = "_create_a_custom_rake_task" > 11.1. Create a custom rake task< / h3 >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/tasks/yaffle_tasks.rake:< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > namespace < span style = "color: #990000" > :< / span > db < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
namespace < span style = "color: #990000" > :< / span > migrate < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
desc < span style = "color: #FF0000" > "Migrate the database through scripts in vendor/plugins/yaffle/lib/db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."< / span >
task < span style = "color: #990000" > :< / span > yaffle < span style = "color: #990000" > => < / span > < span style = "color: #990000" > :< / span > environment < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
ActiveRecord< span style = "color: #990000" > ::< / span > Migration< span style = "color: #990000" > .< / span > verbose < span style = "color: #990000" > =< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "VERBOSE"< / span > < span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ?< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "VERBOSE"< / span > < span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ==< / span > < span style = "color: #FF0000" > "true"< / span > < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span >
ActiveRecord< span style = "color: #990000" > ::< / span > Migrator< span style = "color: #990000" > .< / span > migrate< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "vendor/plugins/yaffle/lib/db/migrate/"< / span > < span style = "color: #990000" > ,< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "VERSION"< / span > < span style = "color: #990000" > ]< / span > < span style = "color: #990000" > ?< / span > ENV< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "VERSION"< / span > < span style = "color: #990000" > ].< / span > to_i < span style = "color: #990000" > :< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > nil< / span > < / span > < span style = "color: #990000" > )< / span >
Rake< span style = "color: #990000" > ::< / span > Task< span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "db:schema:dump"< / span > < span style = "color: #990000" > ].< / span > invoke < span style = "font-weight: bold" > < span style = "color: #0000FF" > if< / span > < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > schema_format < span style = "color: #990000" > ==< / span > < span style = "color: #990000" > :< / span > ruby
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< h3 id = "_call_migrations_directly" > 11.2. Call migrations directly< / h3 >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb:< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > Dir< span style = "color: #990000" > .< / span > glob< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > "db"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "migrate"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "*"< / span > < span style = "color: #990000" > )).< / span > each < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > file< span style = "color: #990000" > |< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > file
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > < strong > db/migrate/20081116181115_create_birdhouses.rb:< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > CreateBirdhouses < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Migration
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > up
Yaffle< span style = "color: #990000" > ::< / span > CreateBirdhouses< span style = "color: #990000" > .< / span > up
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > down
Yaffle< span style = "color: #990000" > ::< / span > CreateBirdhouses< span style = "color: #990000" > .< / span > down
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "admonitionblock" >
< table > < tr >
< td class = "icon" >
< img src = "./images/icons/note.png" alt = "Note" / >
< / td >
< td class = "content" >
< div class = "title" > Editor’ s note:< / div > several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.< / td >
< / tr > < / table >
< / div >
< h3 id = "_generate_migrations" > 11.3. Generate migrations< / h3 >
< div class = "paragraph" > < p > 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:< / p > < / div >
< div class = "ulist" > < ul >
< li >
< p >
call your script/generate script and pass in whatever options they need
< / p >
< / li >
< li >
< p >
examine the generated migration, adding/removing columns or other options as necessary
< / p >
< / li >
< / ul > < / div >
< div class = "paragraph" > < p > This example will demonstrate how to use one of the built-in generator methods named < em > migration_template< / em > to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it’ s best to write a test first:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > )< / span > < span style = "color: #990000" > +< / span > < span style = "color: #FF0000" > '/test_helper.rb'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator'< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > 'rails_generator/scripts/generate'< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > MigrationGeneratorTest < span style = "color: #990000" > < < / span > Test< span style = "color: #990000" > ::< / span > Unit< span style = "color: #990000" > ::< / span > TestCase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > setup
FileUtils< span style = "color: #990000" > .< / span > mkdir_p< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > @original_files< / span > < span style = "color: #990000" > =< / span > file_list
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > teardown
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > pluralize_table_names < span style = "color: #990000" > =< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > true< / span > < / span >
FileUtils< span style = "color: #990000" > .< / span > rm_r< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_generates_correct_file_name
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Scripts< span style = "color: #990000" > ::< / span > Generate< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > .< / span > run< span style = "color: #990000" > ([< / span > < span style = "color: #FF0000" > "yaffle_migration"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #990000" > :< / span > destination < span style = "color: #990000" > => < / span > fake_rails_root< span style = "color: #990000" > )< / span >
new_file < span style = "color: #990000" > =< / span > < span style = "color: #990000" > (< / span > file_list < span style = "color: #990000" > -< / span > < span style = "color: #009900" > @original_files< / span > < span style = "color: #990000" > ).< / span > first
assert_match < span style = "color: #FF6600" > /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migrations/< / span > < span style = "color: #990000" > ,< / span > new_file
assert_match < span style = "color: #FF6600" > /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migrations do |t|/< / span > < span style = "color: #990000" > ,< / span > File< span style = "color: #990000" > .< / span > read< span style = "color: #990000" > (< / span > new_file< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > test_pluralizes_properly
ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > pluralize_table_names < span style = "color: #990000" > =< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > false< / span > < / span >
Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > Scripts< span style = "color: #990000" > ::< / span > Generate< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > .< / span > run< span style = "color: #990000" > ([< / span > < span style = "color: #FF0000" > "yaffle_migration"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"< / span > < span style = "color: #990000" > ],< / span > < span style = "color: #990000" > :< / span > destination < span style = "color: #990000" > => < / span > fake_rails_root< span style = "color: #990000" > )< / span >
new_file < span style = "color: #990000" > =< / span > < span style = "color: #990000" > (< / span > file_list < span style = "color: #990000" > -< / span > < span style = "color: #009900" > @original_files< / span > < span style = "color: #990000" > ).< / span > first
assert_match < span style = "color: #FF6600" > /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migration/< / span > < span style = "color: #990000" > ,< / span > new_file
assert_match < span style = "color: #FF6600" > /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migration do |t|/< / span > < span style = "color: #990000" > ,< / span > File< span style = "color: #990000" > .< / span > read< span style = "color: #990000" > (< / span > new_file< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
private
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > fake_rails_root
File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'rails_root'< / span > < span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > file_list
Dir< span style = "color: #990000" > .< / span > glob< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > fake_rails_root< span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "db"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "migrate"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "*"< / span > < span style = "color: #990000" > ))< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "admonitionblock" >
< table > < tr >
< td class = "icon" >
< img src = "./images/icons/note.png" alt = "Note" / >
< / td >
< td class = "content" >
< div class = "title" > Editor’ s note:< / div > the migration generator checks to see if a migation already exists, and it’ s hard-coded to check the < em > db/migrate< / em > directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.< / td >
< / tr > < / table >
< / div >
< div class = "paragraph" > < p > After running the test with < em > rake< / em > you can make it pass with:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/generators/yaffle_migration/yaffle_migration_generator.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > YaffleMigrationGenerator < span style = "color: #990000" > < < / span > Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > NamedBase
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > manifest
record < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > m< span style = "color: #990000" > |< / span >
m< span style = "color: #990000" > .< / span > migration_template < span style = "color: #FF0000" > 'migration:migration.rb'< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "db/migrate"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > {< / span > < span style = "color: #990000" > :< / span > assigns < span style = "color: #990000" > => < / span > yaffle_local_assigns< span style = "color: #990000" > ,< / span >
< span style = "color: #990000" > :< / span > migration_file_name < span style = "color: #990000" > => < / span > < span style = "color: #FF0000" > "add_yaffle_fields_to_#{custom_file_name}"< / span >
< span style = "color: #FF0000" > }< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
private
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > custom_file_name
custom_name < span style = "color: #990000" > =< / span > class_name< span style = "color: #990000" > .< / span > underscore< span style = "color: #990000" > .< / span > downcase
custom_name < span style = "color: #990000" > =< / span > custom_name< span style = "color: #990000" > .< / span > pluralize < span style = "font-weight: bold" > < span style = "color: #0000FF" > if< / span > < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Base< span style = "color: #990000" > .< / span > pluralize_table_names
custom_name
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > yaffle_local_assigns
returning< span style = "color: #990000" > (< / span > assigns < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > {}< / span > < span style = "color: #990000" > )< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
assigns< span style = "color: #990000" > [:< / span > migration_action< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "add"< / span >
assigns< span style = "color: #990000" > [:< / span > class_name< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "add_yaffle_fields_to_#{custom_file_name}"< / span >
assigns< span style = "color: #990000" > [:< / span > table_name< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > =< / span > custom_file_name
assigns< span style = "color: #990000" > [:< / span > attributes< span style = "color: #990000" > ]< / span > < span style = "color: #990000" > =< / span > < span style = "color: #990000" > [< / span > Rails< span style = "color: #990000" > ::< / span > Generator< span style = "color: #990000" > ::< / span > GeneratedAttribute< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > (< / span > < span style = "color: #FF0000" > "last_squawk"< / span > < span style = "color: #990000" > ,< / span > < span style = "color: #FF0000" > "string"< / span > < span style = "color: #990000" > )]< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > The generator creates a new file in < em > db/migrate< / em > with a timestamp and an < em > add_column< / em > statement. It reuses the built in rails < tt > migration_template< / tt > method, and reuses the built-in rails migration template.< / p > < / div >
< div class = "paragraph" > < p > It’ s courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won’ t have to manually change the generated files if they’ ve turned pluralization off.< / p > < / div >
< div class = "paragraph" > < p > To run the generator, type the following at the command line:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > ./script/generate yaffle_migration bird< / tt > < / pre >
2008-10-27 07:47:01 +01:00
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > and you will see a new file:< / p > < / div >
< div class = "paragraph" > < p > < strong > db/migrate/20080529225649_add_yaffle_fields_to_birds.rb< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #0000FF" > class< / span > < / span > AddYaffleFieldsToBirds < span style = "color: #990000" > < < / span > ActiveRecord< span style = "color: #990000" > ::< / span > Migration
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > up
add_column < span style = "color: #990000" > :< / span > birds< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > last_squawk< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > string
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > def< / span > < / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > self< / span > < / span > < span style = "color: #990000" > .< / span > down
remove_column < span style = "color: #990000" > :< / span > birds< span style = "color: #990000" > ,< / span > < span style = "color: #990000" > :< / span > last_squawk
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< / div >
< h2 id = "_rake_tasks" > 12. Rake tasks< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > When you created the plugin with the built-in rails generator, it generated a rake file for you in < em > vendor/plugins/yaffle/tasks/yaffle_tasks.rake< / em > . Any rake task you add here will be available to the app.< / p > < / div >
< div class = "paragraph" > < p > Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/tasks/yaffle_tasks.rake< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2008-11-24 22:53:39 +01:00
< pre > < tt > namespace < span style = "color: #990000" > :< / span > yaffle < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
2008-10-27 07:47:01 +01:00
desc < span style = "color: #FF0000" > "Prints out the word 'Yaffle'"< / span >
task < span style = "color: #990000" > :< / span > squawk < span style = "color: #990000" > => < / span > < span style = "color: #990000" > :< / span > environment < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span >
puts < span style = "color: #FF0000" > "squawk!"< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
2009-02-04 21:26:08 +01:00
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > When you run < tt > rake -T< / tt > from your plugin you will see:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > yaffle:squawk # Prints out the word 'Yaffle'< / tt > < / pre >
< / div > < / div >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.< / p > < / div >
< div class = "paragraph" > < p > Note that tasks from < em > vendor/plugins/yaffle/Rakefile< / em > are not available to the main app.< / p > < / div >
< / div >
< h2 id = "_plugingems" > 13. PluginGems< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > 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.< / p > < / div >
< div class = "paragraph" > < p > Historically rails plugins loaded the plugin’ s < em > init.rb< / em > file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, < em > init.rb< / em > was moved to < em > rails/init.rb< / em > .< / p > < / div >
< div class = "paragraph" > < p > It’ s common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in < em > Rakefile< / em > . A rake task that packages the gem might look like this:< / p > < / div >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/Rakefile:< / strong > < / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
2009-02-04 21:26:08 +01:00
< pre > < tt > PKG_FILES < span style = "color: #990000" > =< / span > FileList< span style = "color: #990000" > [< / span >
< span style = "color: #FF0000" > '[a-zA-Z]*'< / span > < span style = "color: #990000" > ,< / span >
< span style = "color: #FF0000" > 'generators/**/*'< / span > < span style = "color: #990000" > ,< / span >
< span style = "color: #FF0000" > 'lib/**/*'< / span > < span style = "color: #990000" > ,< / span >
< span style = "color: #FF0000" > 'rails/**/*'< / span > < span style = "color: #990000" > ,< / span >
< span style = "color: #FF0000" > 'tasks/**/*'< / span > < span style = "color: #990000" > ,< / span >
< span style = "color: #FF0000" > 'test/**/*'< / span >
< span style = "color: #990000" > ]< / span >
spec < span style = "color: #990000" > =< / span > Gem< span style = "color: #990000" > ::< / span > Specification< span style = "color: #990000" > .< / span > new < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > s< span style = "color: #990000" > |< / span >
s< span style = "color: #990000" > .< / span > name < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "yaffle"< / span >
s< span style = "color: #990000" > .< / span > version < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "0.0.1"< / span >
s< span style = "color: #990000" > .< / span > author < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "Gleeful Yaffler"< / span >
s< span style = "color: #990000" > .< / span > email < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "yaffle@example.com"< / span >
s< span style = "color: #990000" > .< / span > homepage < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "http://yafflers.example.com/"< / span >
s< span style = "color: #990000" > .< / span > platform < span style = "color: #990000" > =< / span > Gem< span style = "color: #990000" > ::< / span > Platform< span style = "color: #990000" > ::< / span > RUBY
s< span style = "color: #990000" > .< / span > summary < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "Sharing Yaffle Goodness"< / span >
s< span style = "color: #990000" > .< / span > files < span style = "color: #990000" > =< / span > PKG_FILES< span style = "color: #990000" > .< / span > to_a
s< span style = "color: #990000" > .< / span > require_path < span style = "color: #990000" > =< / span > < span style = "color: #FF0000" > "lib"< / span >
s< span style = "color: #990000" > .< / span > has_rdoc < span style = "color: #990000" > =< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > false< / span > < / span >
s< span style = "color: #990000" > .< / span > extra_rdoc_files < span style = "color: #990000" > =< / span > < span style = "color: #990000" > [< / span > < span style = "color: #FF0000" > "README"< / span > < span style = "color: #990000" > ]< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
desc < span style = "color: #FF0000" > 'Turn this plugin into a gem.'< / span >
Rake< span style = "color: #990000" > ::< / span > GemPackageTask< span style = "color: #990000" > .< / span > new< span style = "color: #990000" > (< / span > spec< span style = "color: #990000" > )< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > pkg< span style = "color: #990000" > |< / span >
pkg< span style = "color: #990000" > .< / span > gem_spec < span style = "color: #990000" > =< / span > spec
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span > < / tt > < / pre > < / div > < / div >
< div class = "paragraph" > < p > To build and install the gem locally, run the following commands:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > cd vendor/plugins/yaffle
rake gem
sudo gem install pkg/yaffle-0.0.1.gem< / tt > < / pre >
< / div > < / div >
< div class = "paragraph" > < p > To test this, create a new rails app, add < em > config.gem "yaffle"< / em > to environment.rb and all of your plugin’ s functionality will be available to you.< / p > < / div >
2008-10-27 07:47:01 +01:00
< / div >
2009-02-04 21:26:08 +01:00
< h2 id = "_rdoc_documentation" > 14. RDoc Documentation< / h2 >
2008-10-27 07:47:01 +01:00
< div class = "sectionbody" >
2009-02-04 21:26:08 +01:00
< div class = "paragraph" > < p > Once your plugin is stable and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.< / p > < / div >
< div class = "paragraph" > < p > 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:< / p > < / div >
< div class = "ulist" > < ul >
< li >
< p >
Your name
< / p >
< / li >
< li >
< p >
How to install
< / p >
< / li >
< li >
< p >
How to add the functionality to the app (several examples of common use cases)
< / p >
< / li >
< li >
< p >
Warning, gotchas or tips that might help save users time
< / p >
< / li >
< / ul > < / div >
< div class = "paragraph" > < p > Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It’ s also customary to add < em > #:nodoc:< / em > comments to those parts of the code that are not part of the public api.< / p > < / div >
< div class = "paragraph" > < p > Once your comments are good to go, navigate to your plugin directory and run:< / p > < / div >
< div class = "listingblock" >
< div class = "content" >
< pre > < tt > rake rdoc< / tt > < / pre >
< / div > < / div >
< / div >
< h2 id = "_appendix" > 15. Appendix< / h2 >
< div class = "sectionbody" >
< div class = "paragraph" > < p > If you prefer to use RSpec instead of Test::Unit, you may be interested in the < a href = "http://github.com/pat-maddox/rspec-plugin-generator/tree/master" > RSpec Plugin Generator< / a > .< / p > < / div >
< h3 id = "_references" > 15.1. References< / h3 >
< div class = "ulist" > < ul >
2008-10-27 07:47:01 +01:00
< li >
< p >
< a href = "http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i" > http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i< / a >
< / p >
< / li >
< li >
< p >
2009-02-04 21:26:08 +01:00
< a href = "http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-ii" > http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-ii< / a >
2008-10-27 07:47:01 +01:00
< / p >
< / li >
< li >
< p >
< a href = "http://github.com/technoweenie/attachment_fu/tree/master" > http://github.com/technoweenie/attachment_fu/tree/master< / a >
< / p >
< / li >
< li >
< p >
< a href = "http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html" > http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html< / a >
< / p >
< / li >
2009-02-04 21:26:08 +01:00
< li >
< p >
< a href = "http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins" > http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins< / a >
< / p >
< / li >
< li >
< p >
< a href = "http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2" > http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2< / a > .
< / p >
< / li >
2008-10-27 07:47:01 +01:00
< / ul > < / div >
2009-02-04 21:26:08 +01:00
< h3 id = "_contents_of_em_lib_yaffle_rb_em" > 15.2. Contents of < em > lib/yaffle.rb< / em > < / h3 >
< div class = "paragraph" > < p > < strong > vendor/plugins/yaffle/lib/yaffle.rb:< / strong > < / p > < / div >
< div class = "listingblock" >
< div class = "content" > <!-- Generator: GNU source - highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
< pre > < tt > < span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/core_ext"< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/acts_as_yaffle"< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/commands"< / span >
< span style = "font-weight: bold" > < span style = "color: #000080" > require< / span > < / span > < span style = "color: #FF0000" > "yaffle/routing"< / span >
< span style = "color: #990000" > %< / span > w< span style = "color: #FF0000" > {< / span > models controllers helpers < span style = "color: #FF0000" > }< / span > < span style = "color: #990000" > .< / span > each < span style = "font-weight: bold" > < span style = "color: #0000FF" > do< / span > < / span > < span style = "color: #990000" > |< / span > dir< span style = "color: #990000" > |< / span >
path < span style = "color: #990000" > =< / span > File< span style = "color: #990000" > .< / span > join< span style = "color: #990000" > (< / span > File< span style = "color: #990000" > .< / span > dirname< span style = "color: #990000" > (< / span > < span style = "font-weight: bold" > < span style = "color: #0000FF" > __FILE__< / span > < / span > < span style = "color: #990000" > ),< / span > < span style = "color: #FF0000" > 'app'< / span > < span style = "color: #990000" > ,< / span > dir< span style = "color: #990000" > )< / span >
< span style = "color: #009900" > $LOAD_PATH< / span > < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_paths < span style = "color: #990000" > < < < / span > path
ActiveSupport< span style = "color: #990000" > ::< / span > Dependencies< span style = "color: #990000" > .< / span > load_once_paths< span style = "color: #990000" > .< / span > delete< span style = "color: #990000" > (< / span > path< span style = "color: #990000" > )< / span >
< span style = "font-weight: bold" > < span style = "color: #0000FF" > end< / span > < / span >
< span style = "font-style: italic" > < span style = "color: #9A1900" > # optionally:< / span > < / span >
< span style = "font-style: italic" > < span style = "color: #9A1900" > # Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|< / span > < / span >
< span style = "font-style: italic" > < span style = "color: #9A1900" > # require file< / span > < / span >
< span style = "font-style: italic" > < span style = "color: #9A1900" > # end< / span > < / span >
< / tt > < / pre > < / div > < / div >
< h3 id = "_final_plugin_directory_structure" > 15.3. Final plugin directory structure< / h3 >
< div class = "paragraph" > < p > The final plugin should have a directory structure that looks something like this:< / p > < / div >
2008-10-27 07:47:01 +01:00
< div class = "listingblock" >
< div class = "content" >
2009-02-04 21:26:08 +01:00
< pre > < tt > |-- MIT-LICENSE
|-- README
|-- Rakefile
|-- generators
| |-- yaffle_definition
| | |-- USAGE
| | |-- templates
| | | `-- definition.txt
| | `-- yaffle_definition_generator.rb
| |-- yaffle_migration
| | |-- USAGE
| | |-- templates
| | `-- yaffle_migration_generator.rb
| `-- yaffle_route
| |-- USAGE
| |-- templates
| `-- yaffle_route_generator.rb
|-- install.rb
|-- lib
| |-- app
| | |-- controllers
| | | `-- woodpeckers_controller.rb
| | |-- helpers
| | | `-- woodpeckers_helper.rb
| | `-- models
| | `-- woodpecker.rb
| |-- db
| | `-- migrate
| | `-- 20081116181115_create_birdhouses.rb
| |-- yaffle
| | |-- acts_as_yaffle.rb
| | |-- commands.rb
| | |-- core_ext.rb
| | `-- routing.rb
| `-- yaffle.rb
|-- pkg
| `-- yaffle-0.0.1.gem
|-- rails
| `-- init.rb
|-- tasks
| `-- yaffle_tasks.rake
|-- test
| |-- acts_as_yaffle_test.rb
| |-- core_ext_test.rb
| |-- database.yml
| |-- debug.log
| |-- definition_generator_test.rb
| |-- migration_generator_test.rb
| |-- route_generator_test.rb
| |-- routes_test.rb
| |-- schema.rb
| |-- test_helper.rb
| |-- woodpecker_test.rb
| |-- woodpeckers_controller_test.rb
| |-- wookpeckers_helper_test.rb
| |-- yaffle_plugin.sqlite3.db
| `-- yaffle_test.rb
`-- uninstall.rb< / tt > < / pre >
2008-10-27 07:47:01 +01:00
< / div > < / div >
< / div >
2009-02-04 21:26:08 +01:00
< / div >
< / div >
2008-10-27 07:47:01 +01:00
< / body >
< / html >