As usual, forgot to 'bzr add' these.
Completes the upgrade to Rails 2.2.2.
This commit is contained in:
Jacques Distler 2008-11-24 16:19:37 -06:00
parent 2e81ca2d30
commit 620052a5ba
170 changed files with 20116 additions and 0 deletions

View file

@ -0,0 +1,59 @@
== Add a controller ==
This section describes how to add a controller named 'woodpeckers' to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.
You can test your plugin's controller as you would test any other controller:
*vendor/plugins/yaffle/yaffle/woodpeckers_controller_test.rb:*
[source, ruby]
----------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
require 'woodpeckers_controller'
require 'action_controller/test_process'
class WoodpeckersController; def rescue_action(e) raise e end; end
class WoodpeckersControllerTest < Test::Unit::TestCase
def setup
@controller = WoodpeckersController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_index
get :index
assert_response :success
end
end
----------------------------------------------
This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
*vendor/plugins/yaffle/lib/yaffle.rb:*
[source, ruby]
----------------------------------------------
%w{ models controllers }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
----------------------------------------------
*vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:*
[source, ruby]
----------------------------------------------
class WoodpeckersController < ActionController::Base
def index
render :text => "Squawk!"
end
end
----------------------------------------------
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 http://localhost:3000/woodpeckers to see your controller in action.

View file

@ -0,0 +1,123 @@
== Extending core classes ==
This section will explain how to add a method to String that will be available anywhere in your rails app by:
* Writing tests for the desired behavior
* Creating and requiring the correct files
=== Creating the test ===
In this example you will add a method to String named `to_squawk`. To begin, create a new test file with a few assertions:
*vendor/plugins/yaffle/test/core_ext_test.rb*
[source, ruby]
--------------------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
class CoreExtTest < Test::Unit::TestCase
def test_to_squawk_prepends_the_word_squawk
assert_equal "squawk! Hello World", "Hello World".to_squawk
end
end
--------------------------------------------------------
Navigate to your plugin directory and run `rake test`:
--------------------------------------------------------
cd vendor/plugins/yaffle
rake test
--------------------------------------------------------
The test above should fail with the message:
--------------------------------------------------------
1) Error:
test_to_squawk_prepends_the_word_squawk(CoreExtTest):
NoMethodError: undefined method `to_squawk' for "Hello World":String
./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'
--------------------------------------------------------
Great - now you are ready to start development.
=== Organize your files ===
A common pattern in rails plugins is to set up the file structure like this:
--------------------------------------------------------
|-- lib
| |-- yaffle
| | `-- core_ext.rb
| `-- yaffle.rb
--------------------------------------------------------
The first thing we need to to is to require our 'lib/yaffle.rb' file from 'rails/init.rb':
*vendor/plugins/yaffle/rails/init.rb*
[source, ruby]
--------------------------------------------------------
require 'yaffle'
--------------------------------------------------------
Then in 'lib/yaffle.rb' require 'lib/core_ext.rb':
*vendor/plugins/yaffle/lib/yaffle.rb*
[source, ruby]
--------------------------------------------------------
require "yaffle/core_ext"
--------------------------------------------------------
Finally, create the 'core_ext.rb' file and add the 'to_squawk' method:
*vendor/plugins/yaffle/lib/yaffle/core_ext.rb*
[source, ruby]
--------------------------------------------------------
String.class_eval do
def to_squawk
"squawk! #{self}".strip
end
end
--------------------------------------------------------
To test that your method does what it says it does, run the unit tests with `rake` from your plugin directory. To see this in action, fire up a console and start squawking:
--------------------------------------------------------
$ ./script/console
>> "Hello World".to_squawk
=> "squawk! Hello World"
--------------------------------------------------------
=== Working with init.rb ===
When rails loads plugins it looks for the file named init.rb. However, when the plugin is initialized, 'init.rb' is invoked via `eval` (not `require`) so it has slightly different behavior.
Under certain circumstances if you reopen classes or modules in 'init.rb' 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 `init.rb`, as shown above.
If you must reopen a class in `init.rb` you can use `module_eval` or `class_eval` to avoid any issues:
*vendor/plugins/yaffle/init.rb*
[source, ruby]
---------------------------------------------------
Hash.class_eval do
def is_a_special_hash?
true
end
end
---------------------------------------------------
Another way is to explicitly define the top-level module space for all modules and classes, like `::Hash`:
*vendor/plugins/yaffle/init.rb*
[source, ruby]
---------------------------------------------------
class ::Hash
def is_a_special_hash?
true
end
end
---------------------------------------------------

View file

@ -0,0 +1 @@
http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins

View file

@ -0,0 +1,89 @@
== Add a custom generator command ==
You may have noticed above that you can used one of the built-in rails migration commands `migration_template`. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
This section describes how you you can create your own commands to add and remove a line of text from 'routes.rb'. This example creates a very simple method that adds or removes a text file.
To start, add the following test method:
*vendor/plugins/yaffle/test/generator_test.rb*
[source, ruby]
-----------------------------------------------------------
def test_generates_definition
Rails::Generator::Scripts::Generate.new.run(["yaffle", "bird"], :destination => fake_rails_root)
definition = File.read(File.join(fake_rails_root, "definition.txt"))
assert_match /Yaffle\:/, definition
end
-----------------------------------------------------------
Run `rake` to watch the test fail, then make the test pass add the following:
*vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*
-----------------------------------------------------------
Yaffle: A bird
-----------------------------------------------------------
*vendor/plugins/yaffle/lib/yaffle.rb*
[source, ruby]
-----------------------------------------------------------
require "yaffle/commands"
-----------------------------------------------------------
*vendor/plugins/yaffle/lib/commands.rb*
[source, ruby]
-----------------------------------------------------------
require 'rails_generator'
require 'rails_generator/commands'
module Yaffle #:nodoc:
module Generator #:nodoc:
module Commands #:nodoc:
module Create
def yaffle_definition
file("definition.txt", "definition.txt")
end
end
module Destroy
def yaffle_definition
file("definition.txt", "definition.txt")
end
end
module List
def yaffle_definition
file("definition.txt", "definition.txt")
end
end
module Update
def yaffle_definition
file("definition.txt", "definition.txt")
end
end
end
end
end
Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
-----------------------------------------------------------
Finally, call your new method in the manifest:
*vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
[source, ruby]
-----------------------------------------------------------
class YaffleGenerator < Rails::Generator::NamedBase
def manifest
m.yaffle_definition
end
end
-----------------------------------------------------------

View file

@ -0,0 +1,51 @@
== Add a helper ==
This section describes how to add a helper named 'WoodpeckersHelper' 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.
You can test your plugin's helper as you would test any other helper:
*vendor/plugins/yaffle/test/woodpeckers_helper_test.rb*
[source, ruby]
---------------------------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
include WoodpeckersHelper
class WoodpeckersHelperTest < Test::Unit::TestCase
def test_tweet
assert_equal "Tweet! Hello", tweet("Hello")
end
end
---------------------------------------------------------------
This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
*vendor/plugins/yaffle/lib/yaffle.rb:*
[source, ruby]
----------------------------------------------
%w{ models controllers helpers }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
ActionView::Base.send :include, WoodpeckersHelper
----------------------------------------------
*vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:*
[source, ruby]
----------------------------------------------
module WoodpeckersHelper
def tweet(text)
"Tweet! #{text}"
end
end
----------------------------------------------
Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.

View file

@ -0,0 +1,76 @@
== Add a model ==
This section describes how to add a model named 'Woodpecker' 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:
---------------------------------------------------------
vendor/plugins/yaffle/
|-- lib
| |-- app
| | |-- controllers
| | |-- helpers
| | |-- models
| | | `-- woodpecker.rb
| | `-- views
| |-- yaffle
| | |-- acts_as_yaffle.rb
| | |-- commands.rb
| | `-- core_ext.rb
| `-- yaffle.rb
---------------------------------------------------------
As always, start with a test:
*vendor/plugins/yaffle/yaffle/woodpecker_test.rb:*
[source, ruby]
----------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
class WoodpeckerTest < Test::Unit::TestCase
load_schema
def test_woodpecker
assert_kind_of Woodpecker, Woodpecker.new
end
end
----------------------------------------------
This is just a simple test to make sure the class is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
*vendor/plugins/yaffle/lib/yaffle.rb:*
[source, ruby]
----------------------------------------------
%w{ models }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
----------------------------------------------
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 'load_once_paths' 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.
*vendor/plugins/yaffle/lib/app/models/woodpecker.rb:*
[source, ruby]
----------------------------------------------
class Woodpecker < ActiveRecord::Base
end
----------------------------------------------
Finally, add the following to your plugin's 'schema.rb':
*vendor/plugins/yaffle/test/schema.rb:*
[source, ruby]
----------------------------------------------
ActiveRecord::Schema.define(:version => 0) do
create_table :woodpeckers, :force => true do |t|
t.string :name
end
end
----------------------------------------------
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.

View file

@ -0,0 +1,230 @@
== Preparation ==
=== Create the basic app ===
The examples in this guide require that you have a working rails application. To create a simple rails app execute:
------------------------------------------------
gem install rails
rails yaffle_guide
cd yaffle_guide
script/generate scaffold bird name:string
rake db:migrate
script/server
------------------------------------------------
Then navigate to http://localhost:3000/birds. Make sure you have a functioning rails app before continuing.
.Editor's note:
NOTE: 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.
=== Generate the plugin skeleton ===
Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either 'CamelCased' or 'under_scored', as an argument. Pass `\--with-generator` to add an example generator also.
This creates a plugin in 'vendor/plugins' including an 'init.rb' and 'README' as well as standard 'lib', 'task', and 'test' directories.
Examples:
----------------------------------------------
./script/generate plugin yaffle
./script/generate plugin yaffle --with-generator
----------------------------------------------
To get more detailed help on the plugin generator, type `./script/generate plugin`.
Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the `\--with-generator` option now:
----------------------------------------------
./script/generate plugin yaffle --with-generator
----------------------------------------------
You should see the following output:
----------------------------------------------
create vendor/plugins/yaffle/lib
create vendor/plugins/yaffle/tasks
create vendor/plugins/yaffle/test
create vendor/plugins/yaffle/README
create vendor/plugins/yaffle/MIT-LICENSE
create vendor/plugins/yaffle/Rakefile
create vendor/plugins/yaffle/init.rb
create vendor/plugins/yaffle/install.rb
create vendor/plugins/yaffle/uninstall.rb
create vendor/plugins/yaffle/lib/yaffle.rb
create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
create vendor/plugins/yaffle/test/core_ext_test.rb
create vendor/plugins/yaffle/generators
create vendor/plugins/yaffle/generators/yaffle
create vendor/plugins/yaffle/generators/yaffle/templates
create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
create vendor/plugins/yaffle/generators/yaffle/USAGE
----------------------------------------------
To begin just change one thing - move 'init.rb' to 'rails/init.rb'.
=== Setup the plugin for testing ===
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.
To setup your plugin to allow for easy testing you'll need to add 3 files:
* A 'database.yml' file with all of your connection strings
* A 'schema.rb' file with your table definitions
* A test helper method that sets up the database
*vendor/plugins/yaffle/test/database.yml:*
----------------------------------------------
sqlite:
:adapter: sqlite
:dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
sqlite3:
:adapter: sqlite3
:dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
postgresql:
:adapter: postgresql
:username: postgres
:password: postgres
:database: yaffle_plugin_test
:min_messages: ERROR
mysql:
:adapter: mysql
:host: localhost
:username: root
:password: password
:database: yaffle_plugin_test
----------------------------------------------
For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:
*vendor/plugins/yaffle/test/schema.rb:*
[source, ruby]
----------------------------------------------
ActiveRecord::Schema.define(:version => 0) do
create_table :hickwalls, :force => true do |t|
t.string :name
t.string :last_squawk
t.datetime :last_squawked_at
end
create_table :wickwalls, :force => true do |t|
t.string :name
t.string :last_tweet
t.datetime :last_tweeted_at
end
create_table :woodpeckers, :force => true do |t|
t.string :name
end
end
----------------------------------------------
*vendor/plugins/yaffle/test/test_helper.rb:*
[source, ruby]
----------------------------------------------
ENV['RAILS_ENV'] = 'test'
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
def load_schema
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
db_adapter = ENV['DB']
# no db passed, try one of these fine config-free DBs before bombing.
db_adapter ||=
begin
require 'rubygems'
require 'sqlite'
'sqlite'
rescue MissingSourceFile
begin
require 'sqlite3'
'sqlite3'
rescue MissingSourceFile
end
end
if db_adapter.nil?
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
end
ActiveRecord::Base.establish_connection(config[db_adapter])
load(File.dirname(__FILE__) + "/schema.rb")
require File.dirname(__FILE__) + '/../rails/init.rb'
end
----------------------------------------------
Now whenever you write a test that requires the database, you can call 'load_schema'.
=== Run the plugin tests ===
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 'vendor/plugins/yaffle/test/yaffle_test.rb' with a sample test. Replace the contents of that file with:
*vendor/plugins/yaffle/test/yaffle_test.rb:*
[source, ruby]
----------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
class YaffleTest < Test::Unit::TestCase
load_schema
class Hickwall < ActiveRecord::Base
end
class Wickwall < ActiveRecord::Base
end
def test_schema_has_loaded_correctly
assert_equal [], Hickwall.all
assert_equal [], Wickwall.all
end
end
----------------------------------------------
To run this, go to the plugin directory and run `rake`:
----------------------------------------------
cd vendor/plugins/yaffle
rake
----------------------------------------------
You should see output like:
----------------------------------------------
/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
-- create_table(:hickwalls, {:force=>true})
-> 0.0220s
-- create_table(:wickwalls, {:force=>true})
-> 0.0077s
-- initialize_schema_migrations_table()
-> 0.0007s
-- assume_migrated_upto_version(0)
-> 0.0007s
Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
.
Finished in 0.002236 seconds.
1 test, 1 assertion, 0 failures, 0 errors
----------------------------------------------
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:
----------------------------------------------
rake DB=sqlite
rake DB=sqlite3
rake DB=mysql
rake DB=postgresql
----------------------------------------------
Now you are ready to test-drive your plugin!