Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / creating_plugins.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <title>The Basics of Creating Rails Plugins</title>
6 <!--[if lt IE 8]>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8 <![endif]-->
9 <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10 <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11 <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12 <style type="text/css">
13 div#container {
14 max-width: 900px;
15 padding-bottom: 3em;
16 }
17
18 div#content {
19 margin-left: 200px;
20 }
21
22 div#container.notoc {
23 max-width: 600px;
24 }
25
26 .notoc div#content {
27 margin-left: 0;
28 }
29
30 pre {
31 line-height: 1.4em;
32 }
33
34 #content p tt {
35 background: #eeeeee;
36 border: solid 1px #cccccc;
37 padding: 3px;
38 }
39
40 dt {
41 font-weight: bold;
42 }
43
44 #content dt tt {
45 font-size: 10pt;
46 }
47
48 dd {
49 margin-left: 3em;
50 }
51
52 #content dt tt, #content pre tt {
53 background: none;
54 padding: 0;
55 border: 0;
56 }
57
58 #content .olist ol {
59 margin-left: 2em;
60 }
61
62 #header {
63 position: relative;
64 max-width: 840px;
65 margin-left: auto;
66 margin-right: auto;
67 }
68
69 #header.notoc {
70 max-width: 580px;
71 }
72
73 #logo {
74 position: absolute;
75 left: 10px;
76 top: 10px;
77 width: 110px;
78 height: 140px;
79 }
80
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83 position: absolute;
84 width: 392px;
85 height: 55px;
86 left: 145px;
87 top: 20px;
88 margin: 0;
89 padding: 0;
90 }
91
92 #site_title span {
93 display: none;
94 }
95
96 #site_title_tagline {
97 display: none;
98 }
99
100 ul#navMain {
101 position: absolute;
102 margin: 0;
103 padding: 0;
104 top: 97px;
105 left: 145px;
106 }
107
108 .left-floaty, .right-floaty {
109 padding: 15px;
110 }
111
112 .admonitionblock,
113 .tableblock {
114 margin-left: 1em;
115 margin-right: 1em;
116 margin-top: 0.25em;
117 margin-bottom: 1em;
118 }
119
120 .admonitionblock .icon {
121 padding-right: 8px;
122 }
123
124 .admonitionblock .content {
125 border: solid 1px #ffda78;
126 background: #fffebd;
127 padding: 10px;
128 padding-top: 8px;
129 padding-bottom: 8px;
130 }
131
132 .admonitionblock .title {
133 font-size: 140%;
134 margin-bottom: 0.5em;
135 }
136
137 .tableblock table {
138 border: solid 1px #aaaaff;
139 background: #f0f0ff;
140 }
141
142 .tableblock th {
143 background: #e0e0e0;
144 }
145
146 .tableblock th,
147 .tableblock td {
148 padding: 3px;
149 padding-left: 5px;
150 padding-right: 5px;
151 }
152
153 .sidebarblock {
154 margin-top: 0.25em;
155 margin: 1em;
156 border: solid 1px #ccccbb;
157 padding: 8px;
158 background: #ffffe0;
159 }
160
161 .sidebarblock .sidebar-title {
162 font-size: 140%;
163 font-weight: 600;
164 margin-bottom: 0.3em;
165 }
166
167 .sidebarblock .sidebar-content > .para:last-child > p {
168 margin-bottom: 0;
169 }
170
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
173 }
174
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
177 }
178
179 </style>
180 </head>
181 <body>
182 <div id="header" >
183 <div id="logo">
184 <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185 </div>
186
187 <h1 id="site_title"><span>Ruby on Rails</span></h1>
188 <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
190 <ul id="navMain">
191 <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192 <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193 </ul>
194 </div>
195
196 <div id="container">
197
198 <div id="sidebar">
199 <h2>Chapters</h2>
200 <ol>
201 <li>
202 <a href="#_preparation">Preparation</a>
203 <ul>
204
205 <li><a href="#_create_the_basic_app">Create the basic app</a></li>
206
207 <li><a href="#_generate_the_plugin_skeleton">Generate the plugin skeleton</a></li>
208
209 <li><a href="#_setup_the_plugin_for_testing">Setup the plugin for testing</a></li>
210
211 <li><a href="#_run_the_plugin_tests">Run the plugin tests</a></li>
212
213 </ul>
214 </li>
215 <li>
216 <a href="#_extending_core_classes">Extending core classes</a>
217 <ul>
218
219 <li><a href="#_creating_the_test">Creating the test</a></li>
220
221 <li><a href="#_organize_your_files">Organize your files</a></li>
222
223 <li><a href="#_working_with_init_rb">Working with init.rb</a></li>
224
225 </ul>
226 </li>
227 <li>
228 <a href="#_add_an_tt_acts_as_yaffle_tt_method_to_active_record">Add an <tt>acts_as_yaffle</tt> method to Active Record</a>
229 <ul>
230
231 <li><a href="#_add_a_class_method">Add a class method</a></li>
232
233 <li><a href="#_add_an_instance_method">Add an instance method</a></li>
234
235 </ul>
236 </li>
237 <li>
238 <a href="#_create_a_generator">Create a generator</a>
239 <ul>
240
241 <li><a href="#_testing_generators">Testing generators</a></li>
242
243 <li><a href="#_adding_to_the_manifest">Adding to the manifest</a></li>
244
245 <li><a href="#_manually_test_the_generator">Manually test the generator</a></li>
246
247 <li><a href="#_the_usage_file">The USAGE file</a></li>
248
249 </ul>
250 </li>
251 <li>
252 <a href="#_add_a_custom_generator_command">Add a custom generator command</a>
253 </li>
254 <li>
255 <a href="#_add_a_model">Add a model</a>
256 </li>
257 <li>
258 <a href="#_add_a_controller">Add a controller</a>
259 </li>
260 <li>
261 <a href="#_add_a_helper">Add a helper</a>
262 </li>
263 <li>
264 <a href="#_add_a_custom_route">Add a Custom Route</a>
265 </li>
266 <li>
267 <a href="#_odds_and_ends">Odds and ends</a>
268 <ul>
269
270 <li><a href="#_generate_rdoc_documentation">Generate RDoc Documentation</a></li>
271
272 <li><a href="#_write_custom_rake_tasks_in_your_plugin">Write custom Rake tasks in your plugin</a></li>
273
274 <li><a href="#_store_plugins_in_alternate_locations">Store plugins in alternate locations</a></li>
275
276 <li><a href="#_create_your_own_plugin_loaders_and_plugin_locators">Create your own Plugin Loaders and Plugin Locators</a></li>
277
278 <li><a href="#_use_custom_plugin_generators">Use Custom Plugin Generators</a></li>
279
280 </ul>
281 </li>
282 <li>
283 <a href="#_appendix">Appendix</a>
284 <ul>
285
286 <li><a href="#_references">References</a></li>
287
288 <li><a href="#_final_plugin_directory_structure">Final plugin directory structure</a></li>
289
290 </ul>
291 </li>
292 </ol>
293 </div>
294
295 <div id="content">
296 <h1>The Basics of Creating Rails Plugins</h1>
297 <div id="preamble">
298 <div class="sectionbody">
299 <div class="para"><p>A Rails plugin is either an extension or a modification of the core framework. Plugins provide:</p></div>
300 <div class="ilist"><ul>
301 <li>
302 <p>
303 a way for developers to share bleeding-edge ideas without hurting the stable code base
304 </p>
305 </li>
306 <li>
307 <p>
308 a segmented architecture so that units of code can be fixed or updated on their own release schedule
309 </p>
310 </li>
311 <li>
312 <p>
313 an outlet for the core developers so that they don’t have to include every cool new feature under the sun
314 </p>
315 </li>
316 </ul></div>
317 <div class="para"><p>After reading this guide you should be familiar with:</p></div>
318 <div class="ilist"><ul>
319 <li>
320 <p>
321 Creating a plugin from scratch
322 </p>
323 </li>
324 <li>
325 <p>
326 Writing and running tests for the plugin
327 </p>
328 </li>
329 <li>
330 <p>
331 Storing models, views, controllers, helpers and even other plugins in your plugins
332 </p>
333 </li>
334 <li>
335 <p>
336 Writing generators
337 </p>
338 </li>
339 <li>
340 <p>
341 Writing custom Rake tasks in your plugin
342 </p>
343 </li>
344 <li>
345 <p>
346 Generating RDoc documentation for your plugin
347 </p>
348 </li>
349 <li>
350 <p>
351 Avoiding common pitfalls with <em>init.rb</em>
352 </p>
353 </li>
354 </ul></div>
355 <div class="para"><p>This guide describes how to build a test-driven plugin that will:</p></div>
356 <div class="ilist"><ul>
357 <li>
358 <p>
359 Extend core ruby classes like Hash and String
360 </p>
361 </li>
362 <li>
363 <p>
364 Add methods to ActiveRecord::Base in the tradition of the <em>acts_as</em> plugins
365 </p>
366 </li>
367 <li>
368 <p>
369 Add a view helper that can be used in erb templates
370 </p>
371 </li>
372 <li>
373 <p>
374 Add a new generator that will generate a migration
375 </p>
376 </li>
377 <li>
378 <p>
379 Add a custom generator command
380 </p>
381 </li>
382 <li>
383 <p>
384 A custom route method that can be used in routes.rb
385 </p>
386 </li>
387 </ul></div>
388 <div class="para"><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>
389 </div>
390 </div>
391 <h2 id="_preparation">1. Preparation</h2>
392 <div class="sectionbody">
393 <h3 id="_create_the_basic_app">1.1. Create the basic app</h3>
394 <div class="para"><p>The examples in this guide require that you have a working rails application. To create a simple rails app execute:</p></div>
395 <div class="listingblock">
396 <div class="content">
397 <pre><tt>gem install rails
398 rails yaffle_guide
399 cd yaffle_guide
400 script/generate scaffold bird name:string
401 rake db:migrate
402 script/server</tt></pre>
403 </div></div>
404 <div class="para"><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>
405 <div class="admonitionblock">
406 <table><tr>
407 <td class="icon">
408 <img src="./images/icons/note.png" alt="Note" />
409 </td>
410 <td class="content">
411 <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>
412 </tr></table>
413 </div>
414 <h3 id="_generate_the_plugin_skeleton">1.2. Generate the plugin skeleton</h3>
415 <div class="para"><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>
416 <div class="para"><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>
417 <div class="para"><p>Examples:</p></div>
418 <div class="listingblock">
419 <div class="content">
420 <pre><tt>./script/generate plugin yaffle
421 ./script/generate plugin yaffle --with-generator</tt></pre>
422 </div></div>
423 <div class="para"><p>To get more detailed help on the plugin generator, type <tt>./script/generate plugin</tt>.</p></div>
424 <div class="para"><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>
425 <div class="listingblock">
426 <div class="content">
427 <pre><tt>./script/generate plugin yaffle --with-generator</tt></pre>
428 </div></div>
429 <div class="para"><p>You should see the following output:</p></div>
430 <div class="listingblock">
431 <div class="content">
432 <pre><tt>create vendor/plugins/yaffle/lib
433 create vendor/plugins/yaffle/tasks
434 create vendor/plugins/yaffle/test
435 create vendor/plugins/yaffle/README
436 create vendor/plugins/yaffle/MIT-LICENSE
437 create vendor/plugins/yaffle/Rakefile
438 create vendor/plugins/yaffle/init.rb
439 create vendor/plugins/yaffle/install.rb
440 create vendor/plugins/yaffle/uninstall.rb
441 create vendor/plugins/yaffle/lib/yaffle.rb
442 create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
443 create vendor/plugins/yaffle/test/core_ext_test.rb
444 create vendor/plugins/yaffle/generators
445 create vendor/plugins/yaffle/generators/yaffle
446 create vendor/plugins/yaffle/generators/yaffle/templates
447 create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
448 create vendor/plugins/yaffle/generators/yaffle/USAGE</tt></pre>
449 </div></div>
450 <div class="para"><p>To begin just change one thing - move <em>init.rb</em> to <em>rails/init.rb</em>.</p></div>
451 <h3 id="_setup_the_plugin_for_testing">1.3. Setup the plugin for testing</h3>
452 <div class="para"><p>If your plugin interacts with a database, you'll need to setup a database connection. In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. This guide will not cover how to use fixtures in plugin tests.</p></div>
453 <div class="para"><p>To setup your plugin to allow for easy testing you'll need to add 3 files:</p></div>
454 <div class="ilist"><ul>
455 <li>
456 <p>
457 A <em>database.yml</em> file with all of your connection strings
458 </p>
459 </li>
460 <li>
461 <p>
462 A <em>schema.rb</em> file with your table definitions
463 </p>
464 </li>
465 <li>
466 <p>
467 A test helper method that sets up the database
468 </p>
469 </li>
470 </ul></div>
471 <div class="para"><p><strong>vendor/plugins/yaffle/test/database.yml:</strong></p></div>
472 <div class="listingblock">
473 <div class="content">
474 <pre><tt>sqlite:
475 :adapter: sqlite
476 :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
477
478 sqlite3:
479 :adapter: sqlite3
480 :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
481
482 postgresql:
483 :adapter: postgresql
484 :username: postgres
485 :password: postgres
486 :database: yaffle_plugin_test
487 :min_messages: ERROR
488
489 mysql:
490 :adapter: mysql
491 :host: localhost
492 :username: root
493 :password: password
494 :database: yaffle_plugin_test</tt></pre>
495 </div></div>
496 <div class="para"><p>For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:</p></div>
497 <div class="para"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
498 <div class="listingblock">
499 <div class="content"><!-- Generator: GNU source-highlight 2.9
500 by Lorenzo Bettini
501 http://www.lorenzobettini.it
502 http://www.gnu.org/software/src-highlite -->
503 <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">=&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
504 create_table <span style="color: #990000">:</span>hickwalls<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</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>
505 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
506 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>last_squawk
507 t<span style="color: #990000">.</span>datetime <span style="color: #990000">:</span>last_squawked_at
508 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
509 create_table <span style="color: #990000">:</span>wickwalls<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</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>
510 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
511 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>last_tweet
512 t<span style="color: #990000">.</span>datetime <span style="color: #990000">:</span>last_tweeted_at
513 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
514 create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</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>
515 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
516 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
517 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
518 </tt></pre></div></div>
519 <div class="para"><p><strong>vendor/plugins/yaffle/test/test_helper.rb:</strong></p></div>
520 <div class="listingblock">
521 <div class="content"><!-- Generator: GNU source-highlight 2.9
522 by Lorenzo Bettini
523 http://www.lorenzobettini.it
524 http://www.gnu.org/software/src-highlite -->
525 <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>
526 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>
527
528 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test/unit'</span>
529 <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>
530
531 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> load_schema
532 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>
533 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>
534
535 db_adapter <span style="color: #990000">=</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'DB'</span><span style="color: #990000">]</span>
536
537 <span style="font-style: italic"><span style="color: #9A1900"># no db passed, try one of these fine config-free DBs before bombing.</span></span>
538 db_adapter <span style="color: #990000">||=</span>
539 <span style="font-weight: bold"><span style="color: #0000FF">begin</span></span>
540 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rubygems'</span>
541 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sqlite'</span>
542 <span style="color: #FF0000">'sqlite'</span>
543 <span style="font-weight: bold"><span style="color: #0000FF">rescue</span></span> MissingSourceFile
544 <span style="font-weight: bold"><span style="color: #0000FF">begin</span></span>
545 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sqlite3'</span>
546 <span style="color: #FF0000">'sqlite3'</span>
547 <span style="font-weight: bold"><span style="color: #0000FF">rescue</span></span> MissingSourceFile
548 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
549 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
550
551 <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>
552 <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>
553 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
554
555 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>
556 <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>
557 <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>
558 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
559 </tt></pre></div></div>
560 <div class="para"><p>Now whenever you write a test that requires the database, you can call <em>load_schema</em>.</p></div>
561 <h3 id="_run_the_plugin_tests">1.4. Run the plugin tests</h3>
562 <div class="para"><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>
563 <div class="para"><p><strong>vendor/plugins/yaffle/test/yaffle_test.rb:</strong></p></div>
564 <div class="listingblock">
565 <div class="content"><!-- Generator: GNU source-highlight 2.9
566 by Lorenzo Bettini
567 http://www.lorenzobettini.it
568 http://www.gnu.org/software/src-highlite -->
569 <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>
570
571 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
572 load_schema
573
574 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
575 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
576
577 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
578 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
579
580 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_schema_has_loaded_correctly
581 assert_equal <span style="color: #990000">[],</span> Hickwall<span style="color: #990000">.</span>all
582 assert_equal <span style="color: #990000">[],</span> Wickwall<span style="color: #990000">.</span>all
583 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
584
585 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
586 </tt></pre></div></div>
587 <div class="para"><p>To run this, go to the plugin directory and run <tt>rake</tt>:</p></div>
588 <div class="listingblock">
589 <div class="content">
590 <pre><tt>cd vendor/plugins/yaffle
591 rake</tt></pre>
592 </div></div>
593 <div class="para"><p>You should see output like:</p></div>
594 <div class="listingblock">
595 <div class="content">
596 <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"
597 -- create_table(:hickwalls, {:force=&gt;true})
598 -&gt; 0.0220s
599 -- create_table(:wickwalls, {:force=&gt;true})
600 -&gt; 0.0077s
601 -- initialize_schema_migrations_table()
602 -&gt; 0.0007s
603 -- assume_migrated_upto_version(0)
604 -&gt; 0.0007s
605 Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
606 Started
607 .
608 Finished in 0.002236 seconds.
609
610 1 test, 1 assertion, 0 failures, 0 errors</tt></pre>
611 </div></div>
612 <div class="para"><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>
613 <div class="listingblock">
614 <div class="content">
615 <pre><tt>rake DB=sqlite
616 rake DB=sqlite3
617 rake DB=mysql
618 rake DB=postgresql</tt></pre>
619 </div></div>
620 <div class="para"><p>Now you are ready to test-drive your plugin!</p></div>
621 </div>
622 <h2 id="_extending_core_classes">2. Extending core classes</h2>
623 <div class="sectionbody">
624 <div class="para"><p>This section will explain how to add a method to String that will be available anywhere in your rails app by:</p></div>
625 <div class="ilist"><ul>
626 <li>
627 <p>
628 Writing tests for the desired behavior
629 </p>
630 </li>
631 <li>
632 <p>
633 Creating and requiring the correct files
634 </p>
635 </li>
636 </ul></div>
637 <h3 id="_creating_the_test">2.1. Creating the test</h3>
638 <div class="para"><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>
639 <div class="para"><p><strong>vendor/plugins/yaffle/test/core_ext_test.rb</strong></p></div>
640 <div class="listingblock">
641 <div class="content"><!-- Generator: GNU source-highlight 2.9
642 by Lorenzo Bettini
643 http://www.lorenzobettini.it
644 http://www.gnu.org/software/src-highlite -->
645 <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>
646
647 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CoreExtTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
648 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_to_squawk_prepends_the_word_squawk
649 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
650 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
651 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
652 </tt></pre></div></div>
653 <div class="para"><p>Navigate to your plugin directory and run <tt>rake test</tt>:</p></div>
654 <div class="listingblock">
655 <div class="content">
656 <pre><tt>cd vendor/plugins/yaffle
657 rake test</tt></pre>
658 </div></div>
659 <div class="para"><p>The test above should fail with the message:</p></div>
660 <div class="listingblock">
661 <div class="content">
662 <pre><tt> 1) Error:
663 test_to_squawk_prepends_the_word_squawk(CoreExtTest):
664 NoMethodError: undefined method `to_squawk' for "Hello World":String
665 ./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'</tt></pre>
666 </div></div>
667 <div class="para"><p>Great - now you are ready to start development.</p></div>
668 <h3 id="_organize_your_files">2.2. Organize your files</h3>
669 <div class="para"><p>A common pattern in rails plugins is to set up the file structure like this:</p></div>
670 <div class="listingblock">
671 <div class="content">
672 <pre><tt>|-- lib
673 | |-- yaffle
674 | | `-- core_ext.rb
675 | `-- yaffle.rb</tt></pre>
676 </div></div>
677 <div class="para"><p>The first thing we need to to is to require our <em>lib/yaffle.rb</em> file from <em>rails/init.rb</em>:</p></div>
678 <div class="para"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
679 <div class="listingblock">
680 <div class="content"><!-- Generator: GNU source-highlight 2.9
681 by Lorenzo Bettini
682 http://www.lorenzobettini.it
683 http://www.gnu.org/software/src-highlite -->
684 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle'</span>
685 </tt></pre></div></div>
686 <div class="para"><p>Then in <em>lib/yaffle.rb</em> require <em>lib/core_ext.rb</em>:</p></div>
687 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
688 <div class="listingblock">
689 <div class="content"><!-- Generator: GNU source-highlight 2.9
690 by Lorenzo Bettini
691 http://www.lorenzobettini.it
692 http://www.gnu.org/software/src-highlite -->
693 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/core_ext"</span>
694 </tt></pre></div></div>
695 <div class="para"><p>Finally, create the <em>core_ext.rb</em> file and add the <em>to_squawk</em> method:</p></div>
696 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/core_ext.rb</strong></p></div>
697 <div class="listingblock">
698 <div class="content"><!-- Generator: GNU source-highlight 2.9
699 by Lorenzo Bettini
700 http://www.lorenzobettini.it
701 http://www.gnu.org/software/src-highlite -->
702 <pre><tt>String<span style="color: #990000">.</span>class_eval <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
703 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> to_squawk
704 <span style="color: #FF0000">"squawk! #{self}"</span><span style="color: #990000">.</span>strip
705 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
706 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
707 </tt></pre></div></div>
708 <div class="para"><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>
709 <div class="listingblock">
710 <div class="content">
711 <pre><tt>$ ./script/console
712 &gt;&gt; "Hello World".to_squawk
713 =&gt; "squawk! Hello World"</tt></pre>
714 </div></div>
715 <h3 id="_working_with_init_rb">2.3. Working with init.rb</h3>
716 <div class="para"><p>When rails loads plugins it looks for the file named init.rb. 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>
717 <div class="para"><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>
718 <div class="para"><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>
719 <div class="para"><p><strong>vendor/plugins/yaffle/init.rb</strong></p></div>
720 <div class="listingblock">
721 <div class="content"><!-- Generator: GNU source-highlight 2.9
722 by Lorenzo Bettini
723 http://www.lorenzobettini.it
724 http://www.gnu.org/software/src-highlite -->
725 <pre><tt>Hash<span style="color: #990000">.</span>class_eval <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
726 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
727 <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
728 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
729 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
730 </tt></pre></div></div>
731 <div class="para"><p>Another way is to explicitly define the top-level module space for all modules and classes, like <tt>::Hash</tt>:</p></div>
732 <div class="para"><p><strong>vendor/plugins/yaffle/init.rb</strong></p></div>
733 <div class="listingblock">
734 <div class="content"><!-- Generator: GNU source-highlight 2.9
735 by Lorenzo Bettini
736 http://www.lorenzobettini.it
737 http://www.gnu.org/software/src-highlite -->
738 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #990000">::</span>Hash
739 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
740 <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
741 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
742 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
743 </tt></pre></div></div>
744 </div>
745 <h2 id="_add_an_tt_acts_as_yaffle_tt_method_to_active_record">3. Add an <tt>acts_as_yaffle</tt> method to Active Record</h2>
746 <div class="sectionbody">
747 <div class="para"><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>
748 <div class="para"><p>To begin, set up your files so that you have:</p></div>
749 <div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
750 <div class="listingblock">
751 <div class="content"><!-- Generator: GNU source-highlight 2.9
752 by Lorenzo Bettini
753 http://www.lorenzobettini.it
754 http://www.gnu.org/software/src-highlite -->
755 <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>
756
757 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
758 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
759 </tt></pre></div></div>
760 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
761 <div class="listingblock">
762 <div class="content"><!-- Generator: GNU source-highlight 2.9
763 by Lorenzo Bettini
764 http://www.lorenzobettini.it
765 http://www.gnu.org/software/src-highlite -->
766 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle/acts_as_yaffle'</span>
767 </tt></pre></div></div>
768 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
769 <div class="listingblock">
770 <div class="content"><!-- Generator: GNU source-highlight 2.9
771 by Lorenzo Bettini
772 http://www.lorenzobettini.it
773 http://www.gnu.org/software/src-highlite -->
774 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
775 <span style="font-style: italic"><span style="color: #9A1900"># your code will go here</span></span>
776 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
777 </tt></pre></div></div>
778 <div class="para"><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>
779 <div class="para"><p>One of the most common plugin patterns for <em>acts_as_yaffle</em> plugins is to structure your file like so:</p></div>
780 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
781 <div class="listingblock">
782 <div class="content"><!-- Generator: GNU source-highlight 2.9
783 by Lorenzo Bettini
784 http://www.lorenzobettini.it
785 http://www.gnu.org/software/src-highlite -->
786 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
787 <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>
788 base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
789 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
790
791 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
792 <span style="font-style: italic"><span style="color: #9A1900"># any method placed here will apply to classes, like Hickwall</span></span>
793 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> acts_as_something
794 send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> InstanceMethods
795 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
796 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
797
798 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> InstanceMethods
799 <span style="font-style: italic"><span style="color: #9A1900"># any method placed here will apply to instaces, like @hickwall</span></span>
800 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
801 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
802 </tt></pre></div></div>
803 <div class="para"><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>
804 <h3 id="_add_a_class_method">3.1. Add a class method</h3>
805 <div class="para"><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>
806 <div class="para"><p>To start out, write a failing test that shows the behavior you'd like:</p></div>
807 <div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
808 <div class="listingblock">
809 <div class="content"><!-- Generator: GNU source-highlight 2.9
810 by Lorenzo Bettini
811 http://www.lorenzobettini.it
812 http://www.gnu.org/software/src-highlite -->
813 <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>
814
815 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
816 acts_as_yaffle
817 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
818
819 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
820 acts_as_yaffle <span style="color: #990000">:</span>yaffle_text_field <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>last_tweet
821 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
822
823 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
824 load_schema
825
826 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_hickwalls_yaffle_text_field_should_be_last_squawk
827 assert_equal <span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> Hickwall<span style="color: #990000">.</span>yaffle_text_field
828 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
829
830 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_wickwalls_yaffle_text_field_should_be_last_tweet
831 assert_equal <span style="color: #FF0000">"last_tweet"</span><span style="color: #990000">,</span> Wickwall<span style="color: #990000">.</span>yaffle_text_field
832 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
833 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
834 </tt></pre></div></div>
835 <div class="para"><p>To make these tests pass, you could modify your <tt>acts_as_yaffle</tt> file like so:</p></div>
836 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
837 <div class="listingblock">
838 <div class="content"><!-- Generator: GNU source-highlight 2.9
839 by Lorenzo Bettini
840 http://www.lorenzobettini.it
841 http://www.gnu.org/software/src-highlite -->
842 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
843 <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>
844 base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
845 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
846
847 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
848 <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>
849 cattr_accessor <span style="color: #990000">:</span>yaffle_text_field
850 <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
851 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
852 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
853 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
854
855 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
856 </tt></pre></div></div>
857 <h3 id="_add_an_instance_method">3.2. Add an instance method</h3>
858 <div class="para"><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>
859 <div class="para"><p>To start out, write a failing test that shows the behavior you'd like:</p></div>
860 <div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
861 <div class="listingblock">
862 <div class="content"><!-- Generator: GNU source-highlight 2.9
863 by Lorenzo Bettini
864 http://www.lorenzobettini.it
865 http://www.gnu.org/software/src-highlite -->
866 <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>
867
868 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
869 acts_as_yaffle
870 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
871
872 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
873 acts_as_yaffle <span style="color: #990000">:</span>yaffle_text_field <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>last_tweet
874 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
875
876 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
877 load_schema
878
879 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_hickwalls_yaffle_text_field_should_be_last_squawk
880 assert_equal <span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> Hickwall<span style="color: #990000">.</span>yaffle_text_field
881 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
882
883 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_wickwalls_yaffle_text_field_should_be_last_tweet
884 assert_equal <span style="color: #FF0000">"last_tweet"</span><span style="color: #990000">,</span> Wickwall<span style="color: #990000">.</span>yaffle_text_field
885 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
886
887 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_hickwalls_squawk_should_populate_last_squawk
888 hickwall <span style="color: #990000">=</span> Hickwall<span style="color: #990000">.</span>new
889 hickwall<span style="color: #990000">.</span>squawk<span style="color: #990000">(</span><span style="color: #FF0000">"Hello World"</span><span style="color: #990000">)</span>
890 assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> hickwall<span style="color: #990000">.</span>last_squawk
891 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
892
893 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_wickwalls_squawk_should_populate_last_tweeted_at
894 wickwall <span style="color: #990000">=</span> Wickwall<span style="color: #990000">.</span>new
895 wickwall<span style="color: #990000">.</span>squawk<span style="color: #990000">(</span><span style="color: #FF0000">"Hello World"</span><span style="color: #990000">)</span>
896 assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> wickwall<span style="color: #990000">.</span>last_tweet
897 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
898 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
899 </tt></pre></div></div>
900 <div class="para"><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>
901 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
902 <div class="listingblock">
903 <div class="content"><!-- Generator: GNU source-highlight 2.9
904 by Lorenzo Bettini
905 http://www.lorenzobettini.it
906 http://www.gnu.org/software/src-highlite -->
907 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
908 <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>
909 base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
910 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
911
912 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
913 <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>
914 cattr_accessor <span style="color: #990000">:</span>yaffle_text_field
915 <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
916 send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> InstanceMethods
917 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
918 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
919
920 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> InstanceMethods
921 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> squawk<span style="color: #990000">(</span>string<span style="color: #990000">)</span>
922 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>
923 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
924 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
925 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
926
927 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
928 </tt></pre></div></div>
929 <div class="admonitionblock">
930 <table><tr>
931 <td class="icon">
932 <img src="./images/icons/note.png" alt="Note" />
933 </td>
934 <td class="content">
935 <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>
936 </tr></table>
937 </div>
938 </div>
939 <h2 id="_create_a_generator">4. Create a generator</h2>
940 <div class="sectionbody">
941 <div class="para"><p>Many plugins ship with generators. When you created the plugin above, you specified the &#8212;with-generator option, so you already have the generator stubs in <em>vendor/plugins/yaffle/generators/yaffle</em>.</p></div>
942 <div class="para"><p>Building generators is a complex topic unto itself and this section will cover one small aspect of generators: creating a generator that adds a time-stamped migration.</p></div>
943 <div class="para"><p>To create a generator you must:</p></div>
944 <div class="ilist"><ul>
945 <li>
946 <p>
947 Add your instructions to the <em>manifest</em> method of the generator
948 </p>
949 </li>
950 <li>
951 <p>
952 Add any necessary template files to the templates directory
953 </p>
954 </li>
955 <li>
956 <p>
957 Test the generator manually by running various combinations of <tt>script/generate</tt> and <tt>script/destroy</tt>
958 </p>
959 </li>
960 <li>
961 <p>
962 Update the USAGE file to add helpful documentation for your generator
963 </p>
964 </li>
965 </ul></div>
966 <h3 id="_testing_generators">4.1. Testing generators</h3>
967 <div class="para"><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>
968 <div class="ilist"><ul>
969 <li>
970 <p>
971 Creates a new fake rails root directory that will serve as destination
972 </p>
973 </li>
974 <li>
975 <p>
976 Runs the generator forward and backward, making whatever assertions are necessary
977 </p>
978 </li>
979 <li>
980 <p>
981 Removes the fake rails root
982 </p>
983 </li>
984 </ul></div>
985 <div class="para"><p>For the generator in this section, the test could look something like this:</p></div>
986 <div class="para"><p><strong>vendor/plugins/yaffle/test/yaffle_generator_test.rb</strong></p></div>
987 <div class="listingblock">
988 <div class="content"><!-- Generator: GNU source-highlight 2.9
989 by Lorenzo Bettini
990 http://www.lorenzobettini.it
991 http://www.gnu.org/software/src-highlite -->
992 <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>
993 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
994 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/generate'</span>
995 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/destroy'</span>
996
997 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> GeneratorTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
998
999 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> fake_rails_root
1000 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>
1001 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1002
1003 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> file_list
1004 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>
1005 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1006
1007 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
1008 FileUtils<span style="color: #990000">.</span>mkdir_p<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
1009 <span style="color: #009900">@original_files</span> <span style="color: #990000">=</span> file_list
1010 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1011
1012 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
1013 FileUtils<span style="color: #990000">.</span>rm_r<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
1014 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1015
1016 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_generates_correct_file_name
1017 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"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"bird"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
1018 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
1019 assert_match <span style="color: #FF6600">/add_yaffle_fields_to_bird/</span><span style="color: #990000">,</span> new_file
1020 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1021
1022 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1023 </tt></pre></div></div>
1024 <div class="para"><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>
1025 <h3 id="_adding_to_the_manifest">4.2. Adding to the manifest</h3>
1026 <div class="para"><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. To start, update your generator file to look like this:</p></div>
1027 <div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb</strong></p></div>
1028 <div class="listingblock">
1029 <div class="content"><!-- Generator: GNU source-highlight 2.9
1030 by Lorenzo Bettini
1031 http://www.lorenzobettini.it
1032 http://www.gnu.org/software/src-highlite -->
1033 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleGenerator <span style="color: #990000">&lt;</span> Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>NamedBase
1034 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> manifest
1035 record <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
1036 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">=&gt;</span> yaffle_local_assigns<span style="color: #990000">,</span>
1037 <span style="color: #990000">:</span>migration_file_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"add_yaffle_fields_to_#{custom_file_name}"</span>
1038 <span style="color: #FF0000">}</span>
1039 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1040 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1041
1042 private
1043 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> custom_file_name
1044 custom_name <span style="color: #990000">=</span> class_name<span style="color: #990000">.</span>underscore<span style="color: #990000">.</span>downcase
1045 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
1046 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1047
1048 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_local_assigns
1049 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>
1050 assigns<span style="color: #990000">[:</span>migration_action<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"add"</span>
1051 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>
1052 assigns<span style="color: #990000">[:</span>table_name<span style="color: #990000">]</span> <span style="color: #990000">=</span> custom_file_name
1053 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>
1054 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1055 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1056 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1057 </tt></pre></div></div>
1058 <div class="para"><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>
1059 <div class="para"><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>
1060 <h3 id="_manually_test_the_generator">4.3. Manually test the generator</h3>
1061 <div class="para"><p>To run the generator, type the following at the command line:</p></div>
1062 <div class="listingblock">
1063 <div class="content">
1064 <pre><tt>./script/generate yaffle bird</tt></pre>
1065 </div></div>
1066 <div class="para"><p>and you will see a new file:</p></div>
1067 <div class="para"><p><strong>db/migrate/20080529225649_add_yaffle_fields_to_birds.rb</strong></p></div>
1068 <div class="listingblock">
1069 <div class="content"><!-- Generator: GNU source-highlight 2.9
1070 by Lorenzo Bettini
1071 http://www.lorenzobettini.it
1072 http://www.gnu.org/software/src-highlite -->
1073 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddYaffleFieldsToBirds <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
1074 <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
1075 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
1076 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1077
1078 <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
1079 remove_column <span style="color: #990000">:</span>birds<span style="color: #990000">,</span> <span style="color: #990000">:</span>last_squawk
1080 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1081 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1082 </tt></pre></div></div>
1083 <h3 id="_the_usage_file">4.4. The USAGE file</h3>
1084 <div class="para"><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>
1085 <div class="listingblock">
1086 <div class="content">
1087 <pre><tt>script/generate</tt></pre>
1088 </div></div>
1089 <div class="para"><p>You should see something like this:</p></div>
1090 <div class="listingblock">
1091 <div class="content">
1092 <pre><tt>Installed Generators
1093 Plugins (vendor/plugins): yaffle
1094 Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration</tt></pre>
1095 </div></div>
1096 <div class="para"><p>When you run <tt>script/generate yaffle</tt> you should see the contents of your <em>vendor/plugins/yaffle/generators/yaffle/USAGE</em> file.</p></div>
1097 <div class="para"><p>For this plugin, update the USAGE file looks like this:</p></div>
1098 <div class="listingblock">
1099 <div class="content">
1100 <pre><tt>Description:
1101 Creates a migration that adds yaffle squawk fields to the given model
1102
1103 Example:
1104 ./script/generate yaffle hickwall
1105
1106 This will create:
1107 db/migrate/TIMESTAMP_add_yaffle_fields_to_hickwall</tt></pre>
1108 </div></div>
1109 </div>
1110 <h2 id="_add_a_custom_generator_command">5. Add a custom generator command</h2>
1111 <div class="sectionbody">
1112 <div class="para"><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>
1113 <div class="para"><p>This section describes how you you can create your own commands to add and remove a line of text from <em>routes.rb</em>. This example creates a very simple method that adds or removes a text file.</p></div>
1114 <div class="para"><p>To start, add the following test method:</p></div>
1115 <div class="para"><p><strong>vendor/plugins/yaffle/test/generator_test.rb</strong></p></div>
1116 <div class="listingblock">
1117 <div class="content"><!-- Generator: GNU source-highlight 2.9
1118 by Lorenzo Bettini
1119 http://www.lorenzobettini.it
1120 http://www.gnu.org/software/src-highlite -->
1121 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_generates_definition
1122 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"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"bird"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
1123 definition <span style="color: #990000">=</span> File<span style="color: #990000">.</span>read<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">"definition.txt"</span><span style="color: #990000">))</span>
1124 assert_match <span style="color: #FF6600">/Yaffle\:/</span><span style="color: #990000">,</span> definition
1125 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1126 </tt></pre></div></div>
1127 <div class="para"><p>Run <tt>rake</tt> to watch the test fail, then make the test pass add the following:</p></div>
1128 <div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle/templates/definition.txt</strong></p></div>
1129 <div class="listingblock">
1130 <div class="content">
1131 <pre><tt>Yaffle: A bird</tt></pre>
1132 </div></div>
1133 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
1134 <div class="listingblock">
1135 <div class="content"><!-- Generator: GNU source-highlight 2.9
1136 by Lorenzo Bettini
1137 http://www.lorenzobettini.it
1138 http://www.gnu.org/software/src-highlite -->
1139 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/commands"</span>
1140 </tt></pre></div></div>
1141 <div class="para"><p><strong>vendor/plugins/yaffle/lib/commands.rb</strong></p></div>
1142 <div class="listingblock">
1143 <div class="content"><!-- Generator: GNU source-highlight 2.9
1144 by Lorenzo Bettini
1145 http://www.lorenzobettini.it
1146 http://www.gnu.org/software/src-highlite -->
1147 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
1148 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/commands'</span>
1149
1150 <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>
1151 <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>
1152 <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>
1153 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Create
1154 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_definition
1155 file<span style="color: #990000">(</span><span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">)</span>
1156 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1157 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1158
1159 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Destroy
1160 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_definition
1161 file<span style="color: #990000">(</span><span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">)</span>
1162 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1163 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1164
1165 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> List
1166 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_definition
1167 file<span style="color: #990000">(</span><span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">)</span>
1168 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1169 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1170
1171 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Update
1172 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_definition
1173 file<span style="color: #990000">(</span><span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">)</span>
1174 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1175 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1176 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1177 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1178 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1179
1180 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
1181 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
1182 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
1183 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
1184 </tt></pre></div></div>
1185 <div class="para"><p>Finally, call your new method in the manifest:</p></div>
1186 <div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb</strong></p></div>
1187 <div class="listingblock">
1188 <div class="content"><!-- Generator: GNU source-highlight 2.9
1189 by Lorenzo Bettini
1190 http://www.lorenzobettini.it
1191 http://www.gnu.org/software/src-highlite -->
1192 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleGenerator <span style="color: #990000">&lt;</span> Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>NamedBase
1193 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> manifest
1194 m<span style="color: #990000">.</span>yaffle_definition
1195 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1196 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1197 </tt></pre></div></div>
1198 </div>
1199 <h2 id="_add_a_model">6. Add a model</h2>
1200 <div class="sectionbody">
1201 <div class="para"><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>
1202 <div class="listingblock">
1203 <div class="content">
1204 <pre><tt>vendor/plugins/yaffle/
1205 |-- lib
1206 | |-- app
1207 | | |-- controllers
1208 | | |-- helpers
1209 | | |-- models
1210 | | | `-- woodpecker.rb
1211 | | `-- views
1212 | |-- yaffle
1213 | | |-- acts_as_yaffle.rb
1214 | | |-- commands.rb
1215 | | `-- core_ext.rb
1216 | `-- yaffle.rb</tt></pre>
1217 </div></div>
1218 <div class="para"><p>As always, start with a test:</p></div>
1219 <div class="para"><p><strong>vendor/plugins/yaffle/yaffle/woodpecker_test.rb:</strong></p></div>
1220 <div class="listingblock">
1221 <div class="content"><!-- Generator: GNU source-highlight 2.9
1222 by Lorenzo Bettini
1223 http://www.lorenzobettini.it
1224 http://www.gnu.org/software/src-highlite -->
1225 <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>
1226
1227 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckerTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
1228 load_schema
1229
1230 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_woodpecker
1231 assert_kind_of Woodpecker<span style="color: #990000">,</span> Woodpecker<span style="color: #990000">.</span>new
1232 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1233 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1234 </tt></pre></div></div>
1235 <div class="para"><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>
1236 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
1237 <div class="listingblock">
1238 <div class="content"><!-- Generator: GNU source-highlight 2.9
1239 by Lorenzo Bettini
1240 http://www.lorenzobettini.it
1241 http://www.gnu.org/software/src-highlite -->
1242 <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>
1243 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>
1244 <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
1245 ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
1246 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>
1247 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1248 </tt></pre></div></div>
1249 <div class="para"><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>
1250 <div class="para"><p><strong>vendor/plugins/yaffle/lib/app/models/woodpecker.rb:</strong></p></div>
1251 <div class="listingblock">
1252 <div class="content"><!-- Generator: GNU source-highlight 2.9
1253 by Lorenzo Bettini
1254 http://www.lorenzobettini.it
1255 http://www.gnu.org/software/src-highlite -->
1256 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Woodpecker <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1257 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1258 </tt></pre></div></div>
1259 <div class="para"><p>Finally, add the following to your plugin's <em>schema.rb</em>:</p></div>
1260 <div class="para"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
1261 <div class="listingblock">
1262 <div class="content"><!-- Generator: GNU source-highlight 2.9
1263 by Lorenzo Bettini
1264 http://www.lorenzobettini.it
1265 http://www.gnu.org/software/src-highlite -->
1266 <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">=&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1267 create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</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>
1268 t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
1269 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1270 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1271 </tt></pre></div></div>
1272 <div class="para"><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>
1273 </div>
1274 <h2 id="_add_a_controller">7. Add a controller</h2>
1275 <div class="sectionbody">
1276 <div class="para"><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>
1277 <div class="para"><p>You can test your plugin's controller as you would test any other controller:</p></div>
1278 <div class="para"><p><strong>vendor/plugins/yaffle/yaffle/woodpeckers_controller_test.rb:</strong></p></div>
1279 <div class="listingblock">
1280 <div class="content"><!-- Generator: GNU source-highlight 2.9
1281 by Lorenzo Bettini
1282 http://www.lorenzobettini.it
1283 http://www.gnu.org/software/src-highlite -->
1284 <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>
1285 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'woodpeckers_controller'</span>
1286 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'action_controller/test_process'</span>
1287
1288 <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>
1289
1290 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersControllerTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
1291 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
1292 <span style="color: #009900">@controller</span> <span style="color: #990000">=</span> WoodpeckersController<span style="color: #990000">.</span>new
1293 <span style="color: #009900">@request</span> <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>TestRequest<span style="color: #990000">.</span>new
1294 <span style="color: #009900">@response</span> <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>TestResponse<span style="color: #990000">.</span>new
1295 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1296
1297 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_index
1298 get <span style="color: #990000">:</span>index
1299 assert_response <span style="color: #990000">:</span>success
1300 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1301 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1302 </tt></pre></div></div>
1303 <div class="para"><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>
1304 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
1305 <div class="listingblock">
1306 <div class="content"><!-- Generator: GNU source-highlight 2.9
1307 by Lorenzo Bettini
1308 http://www.lorenzobettini.it
1309 http://www.gnu.org/software/src-highlite -->
1310 <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>
1311 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>
1312 <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
1313 ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
1314 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>
1315 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1316 </tt></pre></div></div>
1317 <div class="para"><p><strong>vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:</strong></p></div>
1318 <div class="listingblock">
1319 <div class="content"><!-- Generator: GNU source-highlight 2.9
1320 by Lorenzo Bettini
1321 http://www.lorenzobettini.it
1322 http://www.gnu.org/software/src-highlite -->
1323 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
1324
1325 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
1326 render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Squawk!"</span>
1327 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1328
1329 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1330 </tt></pre></div></div>
1331 <div class="para"><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>
1332 </div>
1333 <h2 id="_add_a_helper">8. Add a helper</h2>
1334 <div class="sectionbody">
1335 <div class="para"><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>
1336 <div class="para"><p>You can test your plugin's helper as you would test any other helper:</p></div>
1337 <div class="para"><p><strong>vendor/plugins/yaffle/test/woodpeckers_helper_test.rb</strong></p></div>
1338 <div class="listingblock">
1339 <div class="content"><!-- Generator: GNU source-highlight 2.9
1340 by Lorenzo Bettini
1341 http://www.lorenzobettini.it
1342 http://www.gnu.org/software/src-highlite -->
1343 <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>
1344 <span style="font-weight: bold"><span style="color: #0000FF">include</span></span> WoodpeckersHelper
1345
1346 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersHelperTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
1347 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_tweet
1348 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>
1349 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1350 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1351 </tt></pre></div></div>
1352 <div class="para"><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>
1353 <div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
1354 <div class="listingblock">
1355 <div class="content"><!-- Generator: GNU source-highlight 2.9
1356 by Lorenzo Bettini
1357 http://www.lorenzobettini.it
1358 http://www.gnu.org/software/src-highlite -->
1359 <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>
1360 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>
1361 <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
1362 ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
1363 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>
1364 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1365
1366 ActionView<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> WoodpeckersHelper
1367 </tt></pre></div></div>
1368 <div class="para"><p><strong>vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:</strong></p></div>
1369 <div class="listingblock">
1370 <div class="content"><!-- Generator: GNU source-highlight 2.9
1371 by Lorenzo Bettini
1372 http://www.lorenzobettini.it
1373 http://www.gnu.org/software/src-highlite -->
1374 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> WoodpeckersHelper
1375
1376 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> tweet<span style="color: #990000">(</span>text<span style="color: #990000">)</span>
1377 <span style="color: #FF0000">"Tweet! #{text}"</span>
1378 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1379
1380 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1381 </tt></pre></div></div>
1382 <div class="para"><p>Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.</p></div>
1383 </div>
1384 <h2 id="_add_a_custom_route">9. Add a Custom Route</h2>
1385 <div class="sectionbody">
1386 <div class="para"><p>Testing routes in plugins can be complex, especially if the controllers are also in the plugin itself. Jamis Buck showed a great example of this in <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></div>
1387 <div class="para"><p><strong>vendor/plugins/yaffle/test/routing_test.rb</strong></p></div>
1388 <div class="listingblock">
1389 <div class="content"><!-- Generator: GNU source-highlight 2.9
1390 by Lorenzo Bettini
1391 http://www.lorenzobettini.it
1392 http://www.gnu.org/software/src-highlite -->
1393 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"#{File.dirname(__FILE__)}/test_helper"</span>
1394
1395 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> RoutingTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
1396
1397 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
1398 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>
1399 map<span style="color: #990000">.</span>yaffles
1400 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1401 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1402
1403 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_yaffles_route
1404 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">=&gt;</span> <span style="color: #FF0000">"yaffles_controller"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span>
1405 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1406
1407 private
1408
1409 <span style="font-style: italic"><span style="color: #9A1900"># yes, I know about assert_recognizes, but it has proven problematic to</span></span>
1410 <span style="font-style: italic"><span style="color: #9A1900"># use in these tests, since it uses RouteSet#recognize (which actually</span></span>
1411 <span style="font-style: italic"><span style="color: #9A1900"># tries to instantiate the controller) and because it uses an awkward</span></span>
1412 <span style="font-style: italic"><span style="color: #9A1900"># parameter order.</span></span>
1413 <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>
1414 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">=&gt;</span> method<span style="color: #990000">)</span>
1415 assert_equal options<span style="color: #990000">,</span> result
1416 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1417 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1418 </tt></pre></div></div>
1419 <div class="para"><p><strong>vendor/plugins/yaffle/init.rb</strong></p></div>
1420 <div class="listingblock">
1421 <div class="content"><!-- Generator: GNU source-highlight 2.9
1422 by Lorenzo Bettini
1423 http://www.lorenzobettini.it
1424 http://www.gnu.org/software/src-highlite -->
1425 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"routing"</span>
1426 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
1427 </tt></pre></div></div>
1428 <div class="para"><p><strong>vendor/plugins/yaffle/lib/routing.rb</strong></p></div>
1429 <div class="listingblock">
1430 <div class="content"><!-- Generator: GNU source-highlight 2.9
1431 by Lorenzo Bettini
1432 http://www.lorenzobettini.it
1433 http://www.gnu.org/software/src-highlite -->
1434 <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>
1435 <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>
1436 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MapperExtensions
1437 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffles
1438 <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">=&gt;</span> <span style="color: #FF0000">"yaffles_controller"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1439 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1440 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1441 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1442 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1443 </tt></pre></div></div>
1444 <div class="para"><p><strong>config/routes.rb</strong></p></div>
1445 <div class="listingblock">
1446 <div class="content"><!-- Generator: GNU source-highlight 2.9
1447 by Lorenzo Bettini
1448 http://www.lorenzobettini.it
1449 http://www.gnu.org/software/src-highlite -->
1450 <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>
1451 <span style="color: #990000">...</span>
1452 map<span style="color: #990000">.</span>yaffles
1453 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1454 </tt></pre></div></div>
1455 <div class="para"><p>You can also see if your routes work by running <tt>rake routes</tt> from your app directory.</p></div>
1456 </div>
1457 <h2 id="_odds_and_ends">10. Odds and ends</h2>
1458 <div class="sectionbody">
1459 <h3 id="_generate_rdoc_documentation">10.1. Generate RDoc Documentation</h3>
1460 <div class="para"><p>Once your plugin is stable, the tests pass on all database and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.</p></div>
1461 <div class="para"><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>
1462 <div class="ilist"><ul>
1463 <li>
1464 <p>
1465 Your name.
1466 </p>
1467 </li>
1468 <li>
1469 <p>
1470 How to install.
1471 </p>
1472 </li>
1473 <li>
1474 <p>
1475 How to add the functionality to the app (several examples of common use cases).
1476 </p>
1477 </li>
1478 <li>
1479 <p>
1480 Warning, gotchas or tips that might help save users time.
1481 </p>
1482 </li>
1483 </ul></div>
1484 <div class="para"><p>Once your README is solid, go through and add rdoc comments to all of the methods that developers will use.</p></div>
1485 <div class="para"><p>Before you generate your documentation, be sure to go through and add nodoc comments to those modules and methods that are not important to your users.</p></div>
1486 <div class="para"><p>Once your comments are good to go, navigate to your plugin directory and run:</p></div>
1487 <div class="literalblock">
1488 <div class="content">
1489 <pre><tt>rake rdoc</tt></pre>
1490 </div></div>
1491 <h3 id="_write_custom_rake_tasks_in_your_plugin">10.2. Write custom Rake tasks in your plugin</h3>
1492 <div class="para"><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.rake</em>. Any rake task you add here will be available to the app.</p></div>
1493 <div class="para"><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>
1494 <div class="para"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake</strong></p></div>
1495 <div class="listingblock">
1496 <div class="content"><!-- Generator: GNU source-highlight 2.9
1497 by Lorenzo Bettini
1498 http://www.lorenzobettini.it
1499 http://www.gnu.org/software/src-highlite -->
1500 <pre><tt>namespace <span style="color: #990000">:</span>yaffle <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1501 desc <span style="color: #FF0000">"Prints out the word 'Yaffle'"</span>
1502 task <span style="color: #990000">:</span>squawk <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>environment <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1503 puts <span style="color: #FF0000">"squawk!"</span>
1504 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1505 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1506 </tt></pre></div></div>
1507 <div class="para"><p>When you run <tt>rake -T</tt> from your plugin you will see:</p></div>
1508 <div class="listingblock">
1509 <div class="content">
1510 <pre><tt>yaffle:squawk # Prints out the word 'Yaffle'</tt></pre>
1511 </div></div>
1512 <div class="para"><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>
1513 <h3 id="_store_plugins_in_alternate_locations">10.3. Store plugins in alternate locations</h3>
1514 <div class="para"><p>You can store plugins wherever you want - you just have to add those plugins to the plugins path in <em>environment.rb</em>.</p></div>
1515 <div class="para"><p>Since the plugin is only loaded after the plugin paths are defined, you can't redefine this in your plugins - but it may be good to now.</p></div>
1516 <div class="para"><p>You can even store plugins inside of other plugins for complete plugin madness!</p></div>
1517 <div class="listingblock">
1518 <div class="content"><!-- Generator: GNU source-highlight 2.9
1519 by Lorenzo Bettini
1520 http://www.lorenzobettini.it
1521 http://www.gnu.org/software/src-highlite -->
1522 <pre><tt>config<span style="color: #990000">.</span>plugin_paths <span style="color: #990000">&lt;&lt;</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>RAILS_ROOT<span style="color: #990000">,</span><span style="color: #FF0000">"vendor"</span><span style="color: #990000">,</span><span style="color: #FF0000">"plugins"</span><span style="color: #990000">,</span><span style="color: #FF0000">"yaffle"</span><span style="color: #990000">,</span><span style="color: #FF0000">"lib"</span><span style="color: #990000">,</span><span style="color: #FF0000">"plugins"</span><span style="color: #990000">)</span>
1523 </tt></pre></div></div>
1524 <h3 id="_create_your_own_plugin_loaders_and_plugin_locators">10.4. Create your own Plugin Loaders and Plugin Locators</h3>
1525 <div class="para"><p>If the built-in plugin behavior is inadequate, you can change almost every aspect of the location and loading process. You can write your own plugin locators and plugin loaders, but that's beyond the scope of this tutorial.</p></div>
1526 <h3 id="_use_custom_plugin_generators">10.5. Use Custom Plugin Generators</h3>
1527 <div class="para"><p>If you are an RSpec fan, you can install the <tt>rspec_plugin_generator</tt> gem, which will generate the spec folder and database for you. See <a href="http://github.com/pat-maddox/rspec-plugin-generator/tree/master">http://github.com/pat-maddox/rspec-plugin-generator/tree/master</a>.</p></div>
1528 </div>
1529 <h2 id="_appendix">11. Appendix</h2>
1530 <div class="sectionbody">
1531 <h3 id="_references">11.1. References</h3>
1532 <div class="ilist"><ul>
1533 <li>
1534 <p>
1535 <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>
1536 </p>
1537 </li>
1538 <li>
1539 <p>
1540 <a href="http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-rails-plugins-part-ii">http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-rails-plugins-part-ii</a>
1541 </p>
1542 </li>
1543 <li>
1544 <p>
1545 <a href="http://github.com/technoweenie/attachment_fu/tree/master">http://github.com/technoweenie/attachment_fu/tree/master</a>
1546 </p>
1547 </li>
1548 <li>
1549 <p>
1550 <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>
1551 </p>
1552 </li>
1553 </ul></div>
1554 <h3 id="_final_plugin_directory_structure">11.2. Final plugin directory structure</h3>
1555 <div class="para"><p>The final plugin should have a directory structure that looks something like this:</p></div>
1556 <div class="listingblock">
1557 <div class="content">
1558 <pre><tt> |-- MIT-LICENSE
1559 |-- README
1560 |-- Rakefile
1561 |-- generators
1562 | `-- yaffle
1563 | |-- USAGE
1564 | |-- templates
1565 | | `-- definition.txt
1566 | `-- yaffle_generator.rb
1567 |-- init.rb
1568 |-- install.rb
1569 |-- lib
1570 | |-- acts_as_yaffle.rb
1571 | |-- commands.rb
1572 | |-- core_ext.rb
1573 | |-- routing.rb
1574 | `-- view_helpers.rb
1575 |-- tasks
1576 | `-- yaffle_tasks.rake
1577 |-- test
1578 | |-- acts_as_yaffle_test.rb
1579 | |-- core_ext_test.rb
1580 | |-- database.yml
1581 | |-- debug.log
1582 | |-- routing_test.rb
1583 | |-- schema.rb
1584 | |-- test_helper.rb
1585 | `-- view_helpers_test.rb
1586 |-- uninstall.rb
1587 `-- yaffle_plugin.sqlite3.db</tt></pre>
1588 </div></div>
1589 </div>
1590
1591 </div>
1592 </div>
1593 </body>
1594 </html>