2015-01-14 01:22:13 -05:00
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON http://guides.rubyonrails.org.**
2014-12-23 17:32:50 -05:00
2012-09-01 17:25:58 -04:00
The Basics of Creating Rails Plugins
====================================
2009-02-05 20:57:02 -05:00
A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
2013-12-04 09:34:22 -05:00
* A way for developers to share bleeding-edge ideas without hurting the stable code base.
* A segmented architecture so that units of code can be fixed or updated on their own release schedule.
* An outlet for the core developers so that they don't have to include every cool new feature under the sun.
2009-02-05 20:57:02 -05:00
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2009-02-05 20:57:02 -05:00
2012-12-07 12:50:09 -05:00
* How to create a plugin from scratch.
* How to write and run tests for the plugin.
2009-02-05 20:57:02 -05:00
This guide describes how to build a test-driven plugin that will:
2012-11-29 08:14:08 -05:00
* Extend core Ruby classes like Hash and String.
2015-12-12 08:25:00 -05:00
* Add methods to `ApplicationRecord` in the tradition of the `acts_as` plugins.
2011-02-13 16:21:17 -05:00
* Give you information about where to put generators in your plugin.
2009-02-05 20:57:02 -05:00
2011-04-13 20:49:14 -04:00
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
2011-02-13 16:21:17 -05:00
goodness.
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
Setup
-----
2009-02-05 20:57:02 -05:00
2012-09-03 21:21:24 -04:00
Currently, Rails plugins are built as gems, _gemified plugins_ . They can be shared across
2016-07-12 02:51:36 -04:00
different Rails applications using RubyGems and Bundler if desired.
2011-05-21 20:33:28 -04:00
2012-09-01 17:25:58 -04:00
### Generate a gemified plugin.
2009-02-05 20:57:02 -05:00
2012-12-08 02:20:35 -05:00
Rails ships with a `rails plugin new` command which creates a
2013-10-07 22:26:00 -04:00
skeleton for developing any kind of Rails extension with the ability
2015-12-03 02:31:16 -05:00
to run integration tests using a dummy Rails application. Create your
2013-10-07 22:26:00 -04:00
plugin with the command:
```bash
2014-09-28 10:12:46 -04:00
$ rails plugin new yaffle
2013-10-07 22:26:00 -04:00
```
See usage and options by asking for help:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-09-28 10:12:46 -04:00
$ rails plugin new --help
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
Testing Your Newly Generated Plugin
2012-09-01 17:25:58 -04:00
-----------------------------------
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
You can navigate to the directory that contains the plugin, run the `bundle install` command
2015-12-03 02:31:16 -05:00
and run the one generated test using the `bin/test` command.
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
You should see:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-08-19 00:07:01 -04:00
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
This will tell you that everything got generated properly and you are ready to start adding functionality.
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
Extending Core Classes
----------------------
2009-02-05 20:57:02 -05:00
2016-08-19 04:47:07 -04:00
This section will explain how to add a method to String that will be available anywhere in your Rails application.
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
In this example you will add a method to String named `to_squawk` . To begin, create a new test file with a few assertions:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/test/core_ext_test.rb
2010-04-30 17:19:44 -04:00
2017-08-14 17:49:41 -04:00
require "test_helper"
2009-02-05 20:57:02 -05:00
2013-07-18 01:34:21 -04:00
class CoreExtTest < ActiveSupport::TestCase
2009-02-05 20:57:02 -05:00
def test_to_squawk_prepends_the_word_squawk
assert_equal "squawk! Hello World", "Hello World".to_squawk
end
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
Run `bin/test` to run the test. This test should fail because we haven't implemented the `to_squawk` method:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2015-12-03 02:31:16 -05:00
E
Error:
CoreExtTest#test_to_squawk_prepends_the_word_squawk:
NoMethodError: undefined method `to_squawk' for "Hello World":String
bin/test /path/to/yaffle/test/core_ext_test.rb:4
.
Finished in 0.003358s, 595.6483 runs/s, 297.8242 assertions/s.
2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
Great - now you are ready to start development.
2017-08-14 17:49:41 -04:00
In `lib/yaffle.rb` , add `require "yaffle/core_ext"` :
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle.rb
2010-04-30 17:19:44 -04:00
2017-08-14 17:49:41 -04:00
require "yaffle/railtie"
require "yaffle/core_ext"
2011-02-13 16:21:17 -05:00
module Yaffle
2017-08-14 17:49:41 -04:00
# Your code goes here...
2011-02-13 16:21:17 -05:00
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Finally, create the `core_ext.rb` file and add the `to_squawk` method:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle/core_ext.rb
2010-08-14 01:13:00 -04:00
2017-08-14 17:49:41 -04:00
class String
2009-02-05 20:57:02 -05:00
def to_squawk
"squawk! #{self}".strip
end
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
To test that your method does what it says it does, run the unit tests with `bin/test` from your plugin directory.
2011-02-13 16:21:17 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-08-19 00:07:01 -04:00
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2011-02-13 16:21:17 -05:00
2017-08-22 01:40:56 -04:00
To see this in action, change to the `test/dummy` directory, fire up a console and start squawking:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-05-20 07:29:18 -04:00
$ bin/rails console
2009-02-05 20:57:02 -05:00
>> "Hello World".to_squawk
=> "squawk! Hello World"
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
Add an "acts_as" Method to Active Record
----------------------------------------
2010-02-27 18:14:48 -05:00
2013-09-14 08:26:08 -04:00
A common pattern in plugins is to add a method called `acts_as_something` to models. In this case, you
want to write a method called `acts_as_yaffle` that adds a `squawk` method to your Active Record models.
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
To begin, set up your files so that you have:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/test/acts_as_yaffle_test.rb
2010-04-30 17:19:44 -04:00
2017-08-14 17:49:41 -04:00
require "test_helper"
2011-02-13 16:21:17 -05:00
2013-07-18 01:34:21 -04:00
class ActsAsYaffleTest < ActiveSupport::TestCase
2009-02-05 20:57:02 -05:00
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle.rb
2010-04-30 17:19:44 -04:00
2017-08-14 17:49:41 -04:00
require "yaffle/railtie"
require "yaffle/core_ext"
require "yaffle/acts_as_yaffle"
2011-02-13 16:21:17 -05:00
module Yaffle
2017-08-14 17:49:41 -04:00
# Your code goes here...
2009-02-05 20:57:02 -05:00
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle/acts_as_yaffle.rb
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
module Yaffle
module ActsAsYaffle
end
2009-02-05 20:57:02 -05:00
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
### Add a Class Method
2009-02-05 20:57:02 -05:00
2013-09-14 08:26:08 -04:00
This plugin will expect that you've added a method to your model named `last_squawk` . However, the
plugin users might have already defined a method on their model named `last_squawk` that they use
for something else. This plugin will allow the name to be changed by adding a class method called `yaffle_text_field` .
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
To start out, write a failing test that shows the behavior you'd like:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/test/acts_as_yaffle_test.rb
2009-02-05 20:57:02 -05:00
2017-08-14 17:49:41 -04:00
require "test_helper"
2009-02-05 20:57:02 -05:00
2013-07-18 01:34:21 -04:00
class ActsAsYaffleTest < ActiveSupport::TestCase
2011-02-13 16:21:17 -05:00
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
2012-08-30 17:35:17 -04:00
assert_equal "last_squawk", Hickwall.yaffle_text_field
2009-02-05 20:57:02 -05:00
end
2011-02-13 16:21:17 -05:00
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
2012-08-30 17:35:17 -04:00
assert_equal "last_tweet", Wickwall.yaffle_text_field
2009-02-05 20:57:02 -05:00
end
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
When you run `bin/test` , you should see the following:
2009-02-05 20:57:02 -05:00
2012-09-03 21:21:24 -04:00
```
2015-12-03 02:31:16 -05:00
# Running:
..E
Error:
ActsAsYaffleTest#test_a_wickwalls_yaffle_text_field_should_be_last_tweet:
NameError: uninitialized constant ActsAsYaffleTest::Wickwall
bin/test /path/to/yaffle/test/acts_as_yaffle_test.rb:8
E
Error:
ActsAsYaffleTest#test_a_hickwalls_yaffle_text_field_should_be_last_squawk:
NameError: uninitialized constant ActsAsYaffleTest::Hickwall
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
bin/test /path/to/yaffle/test/acts_as_yaffle_test.rb:4
Finished in 0.004812s, 831.2949 runs/s, 415.6475 assertions/s.
4 runs, 2 assertions, 0 failures, 2 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2011-02-13 16:21:17 -05:00
This tells us that we don't have the necessary models (Hickwall and Wickwall) that we are trying to test.
2011-04-13 20:49:14 -04:00
We can easily generate these models in our "dummy" Rails application by running the following commands from the
2017-09-17 08:40:11 -04:00
`test/dummy` directory:
2011-02-13 16:21:17 -05:00
2012-09-01 20:45:26 -04:00
```bash
2011-02-24 16:29:37 -05:00
$ cd test/dummy
2014-05-20 07:29:18 -04:00
$ bin/rails generate model Hickwall last_squawk:string
$ bin/rails generate model Wickwall last_squawk:string last_tweet:string
2012-09-01 17:08:06 -04:00
```
2011-02-13 16:21:17 -05:00
Now you can create the necessary database tables in your testing database by navigating to your dummy app
2013-12-04 09:34:22 -05:00
and migrating the database. First, run:
2011-02-13 16:21:17 -05:00
2012-09-01 20:45:26 -04:00
```bash
2011-02-24 16:29:37 -05:00
$ cd test/dummy
2016-01-19 06:08:56 -05:00
$ bin/rails db:migrate
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
While you are here, change the Hickwall and Wickwall models so that they know that they are supposed to act
like yaffles.
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# test/dummy/app/models/hickwall.rb
2009-02-05 20:57:02 -05:00
2015-12-12 08:25:00 -05:00
class Hickwall < ApplicationRecord
2009-02-05 20:57:02 -05:00
acts_as_yaffle
end
2011-02-13 16:21:17 -05:00
# test/dummy/app/models/wickwall.rb
2015-12-12 08:25:00 -05:00
class Wickwall < ApplicationRecord
2012-11-16 05:28:16 -05:00
acts_as_yaffle yaffle_text_field: :last_tweet
2009-02-05 20:57:02 -05:00
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
We will also add code to define the `acts_as_yaffle` method.
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle/acts_as_yaffle.rb
2015-12-12 08:25:00 -05:00
2011-02-13 16:21:17 -05:00
module Yaffle
module ActsAsYaffle
extend ActiveSupport::Concern
2017-08-14 17:49:41 -04:00
class_methods do
2011-02-13 16:21:17 -05:00
def acts_as_yaffle(options = {})
end
end
2009-02-05 20:57:02 -05:00
end
end
2011-02-13 16:21:17 -05:00
2015-12-12 08:25:00 -05:00
# test/dummy/app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include Yaffle::ActsAsYaffle
self.abstract_class = true
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
You can then return to the root directory (`cd ../..`) of your plugin and rerun the tests using `bin/test` .
2011-02-13 16:21:17 -05:00
2012-09-03 21:21:24 -04:00
```
2015-12-03 02:31:16 -05:00
# Running:
.E
Error:
ActsAsYaffleTest#test_a_hickwalls_yaffle_text_field_should_be_last_squawk:
NoMethodError: undefined method `yaffle_text_field' for #< Class:0x0055974ebbe9d8 >
bin/test /path/to/yaffle/test/acts_as_yaffle_test.rb:4
E
Error:
ActsAsYaffleTest#test_a_wickwalls_yaffle_text_field_should_be_last_tweet:
NoMethodError: undefined method `yaffle_text_field' for #< Class:0x0055974eb8cfc8 >
bin/test /path/to/yaffle/test/acts_as_yaffle_test.rb:8
2011-02-13 16:21:17 -05:00
2015-12-03 02:31:16 -05:00
.
2011-02-13 16:21:17 -05:00
2015-12-03 02:31:16 -05:00
Finished in 0.008263s, 484.0999 runs/s, 242.0500 assertions/s.
2011-02-13 16:21:17 -05:00
2015-12-03 02:31:16 -05:00
4 runs, 2 assertions, 0 failures, 2 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
Getting closer... Now we will implement the code of the `acts_as_yaffle` method to make the tests pass.
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/lib/yaffle/acts_as_yaffle.rb
2009-02-05 20:57:02 -05:00
module Yaffle
2011-02-13 16:21:17 -05:00
module ActsAsYaffle
2015-11-17 16:39:03 -05:00
extend ActiveSupport::Concern
2011-02-13 16:21:17 -05:00
2017-08-14 17:49:41 -04:00
class_methods do
2011-02-13 16:21:17 -05:00
def acts_as_yaffle(options = {})
2017-05-31 05:16:20 -04:00
cattr_accessor :yaffle_text_field, default: (options[:yaffle_text_field] || :last_squawk).to_s
2011-02-13 16:21:17 -05:00
end
2009-02-05 20:57:02 -05:00
end
end
end
2015-12-12 08:25:00 -05:00
# test/dummy/app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include Yaffle::ActsAsYaffle
self.abstract_class = true
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
When you run `bin/test` , you should see the tests all pass:
2011-02-13 16:21:17 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-08-19 00:07:01 -04:00
4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2011-02-13 16:21:17 -05:00
2012-09-01 17:25:58 -04:00
### Add an Instance Method
2009-02-05 20:57:02 -05:00
2017-09-25 08:05:17 -04:00
This plugin will add a method named 'squawk' to any Active Record object that calls `acts_as_yaffle` . The 'squawk'
2011-02-13 16:21:17 -05:00
method will simply set the value of one of the fields in the database.
2009-02-05 20:57:02 -05:00
To start out, write a failing test that shows the behavior you'd like:
2012-09-01 17:08:06 -04:00
```ruby
2011-02-13 16:21:17 -05:00
# yaffle/test/acts_as_yaffle_test.rb
2017-08-14 17:49:41 -04:00
require "test_helper"
2009-02-05 20:57:02 -05:00
2013-07-18 01:34:21 -04:00
class ActsAsYaffleTest < ActiveSupport::TestCase
2009-02-05 20:57:02 -05:00
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal "last_squawk", Hickwall.yaffle_text_field
end
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal "last_tweet", Wickwall.yaffle_text_field
end
def test_hickwalls_squawk_should_populate_last_squawk
hickwall = Hickwall.new
hickwall.squawk("Hello World")
assert_equal "squawk! Hello World", hickwall.last_squawk
end
2011-08-26 11:34:57 -04:00
def test_wickwalls_squawk_should_populate_last_tweet
2009-02-05 20:57:02 -05:00
wickwall = Wickwall.new
wickwall.squawk("Hello World")
assert_equal "squawk! Hello World", wickwall.last_tweet
end
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2011-08-26 11:34:57 -04:00
Run the test to make sure the last two tests fail with an error that contains "NoMethodError: undefined method `squawk'",
2017-09-25 08:05:17 -04:00
then update `acts_as_yaffle.rb` to look like this:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-09-03 21:21:24 -04:00
# yaffle/lib/yaffle/acts_as_yaffle.rb
2011-02-13 16:21:17 -05:00
2009-02-05 20:57:02 -05:00
module Yaffle
2011-02-13 16:21:17 -05:00
module ActsAsYaffle
extend ActiveSupport::Concern
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
included do
2017-08-14 17:49:41 -04:00
def squawk(string)
write_attribute(self.class.yaffle_text_field, string.to_squawk)
end
2011-02-13 16:21:17 -05:00
end
2017-08-14 17:49:41 -04:00
class_methods do
2011-02-13 16:21:17 -05:00
def acts_as_yaffle(options = {})
2017-05-31 05:16:20 -04:00
cattr_accessor :yaffle_text_field, default: (options[:yaffle_text_field] || :last_squawk).to_s
2012-03-16 15:53:16 -04:00
end
2009-02-05 20:57:02 -05:00
end
end
end
2015-12-12 08:25:00 -05:00
# test/dummy/app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include Yaffle::ActsAsYaffle
self.abstract_class = true
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2015-12-03 02:31:16 -05:00
Run `bin/test` one final time and you should see:
2011-08-16 22:48:01 -04:00
2012-09-03 21:21:24 -04:00
```
2014-08-19 00:07:01 -04:00
6 runs, 6 assertions, 0 failures, 0 errors, 0 skips
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
NOTE: The use of `write_attribute` 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:
```ruby
send("#{self.class.yaffle_text_field}=", string.to_squawk)
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
Generators
----------
2009-02-05 20:57:02 -05:00
2017-08-22 01:40:56 -04:00
Generators can be included in your gem simply by creating them in a `lib/generators` directory of your plugin. More information about
2017-08-14 17:49:41 -04:00
the creation of generators can be found in the [Generators Guide ](generators.html ).
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
Publishing Your Gem
2012-09-01 17:25:58 -04:00
-------------------
2009-02-05 20:57:02 -05:00
2011-08-26 11:34:57 -04:00
Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
2017-10-06 02:02:56 -04:00
commit the code to a Git repository (like GitHub) and add a line to the `Gemfile` of the application in question:
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2017-08-14 17:49:41 -04:00
gem "yaffle", git: "https://github.com/rails/yaffle.git"
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
After running `bundle install` , your gem functionality will be available to the application.
2009-02-05 20:57:02 -05:00
2017-08-14 17:49:41 -04:00
When the gem is ready to be shared as a formal release, it can be published to [RubyGems ](https://rubygems.org ).
For more information about publishing gems to RubyGems, see: [Publishing your gem ](http://guides.rubygems.org/publishing ).
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
RDoc Documentation
------------------
2009-02-05 20:57:02 -05:00
2013-12-04 09:34:22 -05:00
Once your plugin is stable and you are ready to deploy, do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
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:
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
* Your name
* How to install
* How to add the functionality to the app (several examples of common use cases)
2011-08-26 11:34:57 -04:00
* Warnings, gotchas or tips that might help users and save them time
2009-02-05 20:57:02 -05:00
2017-08-14 17:49:41 -04:00
Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It's also customary to add `#:nodoc:` comments to those parts of the code that are not included in the public API.
2009-02-05 20:57:02 -05:00
2011-02-13 16:21:17 -05:00
Once your comments are good to go, navigate to your plugin directory and run:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-12-21 23:45:03 -05:00
$ bundle exec rake rdoc
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
### References
2009-02-05 20:57:02 -05:00
2012-09-02 01:08:20 -04:00
* [Developing a RubyGem using Bundler ](https://github.com/radar/guides/blob/master/gem-development.md )
* [Using .gemspecs as Intended ](http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/ )
2014-08-12 05:07:07 -04:00
* [Gemspec Reference ](http://guides.rubygems.org/specification-reference/ )