From 88a13fad4fae2c2088188008248e15498a2ca466 Mon Sep 17 00:00:00 2001 From: Jeff Dean Date: Fri, 14 Nov 2008 03:02:05 -0500 Subject: Rails plugin: Expanded helpers section --- railties/doc/guides/html/creating_plugins.html | 75 +++++++++----------------- 1 file changed, 26 insertions(+), 49 deletions(-) (limited to 'railties/doc/guides/html/creating_plugins.html') diff --git a/railties/doc/guides/html/creating_plugins.html b/railties/doc/guides/html/creating_plugins.html index 3fa7bff260..023a4ddbab 100644 --- a/railties/doc/guides/html/creating_plugins.html +++ b/railties/doc/guides/html/creating_plugins.html @@ -258,7 +258,7 @@ ul#navMain { Add a controller
  • - Create a squawk_info_for view helper + Add a helper
  • Add a Custom Route @@ -1330,79 +1330,56 @@ http://www.gnu.org/software/src-highlite -->

    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.

    -

    8. Create a squawk_info_for view helper

    +

    8. Add a helper

    -

    Creating a view helper is a 3-step process:

    -
      -
    • -

      -Add an appropriately named file to the lib directory. -

      -
    • -
    • -

      -Require the file and hooks in init.rb. -

      -
    • -
    • -

      -Write the tests. -

      -
    • -
    -

    First, create the test to define the functionality you want:

    -

    vendor/plugins/yaffle/test/view_helpers_test.rb

    +

    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

    require File.dirname(__FILE__) + '/test_helper.rb'
    -include YaffleViewHelper
    +include WoodpeckersHelper
     
    -class ViewHelpersTest < Test::Unit::TestCase
    -  def test_squawk_info_for_should_return_the_text_and_date
    -    time = Time.now
    -    hickwall = Hickwall.new
    -    hickwall.last_squawk = "Hello World"
    -    hickwall.last_squawked_at = time
    -    assert_equal "Hello World, #{time.to_s}", squawk_info_for(hickwall)
    +class WoodpeckersHelperTest < Test::Unit::TestCase
    +  def test_tweet
    +    assert_equal "Tweet! Hello", tweet("Hello")
       end
     end
     
    -

    Then add the following statements to init.rb:

    -

    vendor/plugins/yaffle/init.rb

    +

    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:

    -
    require "view_helpers"
    -ActionView::Base.send :include, YaffleViewHelper
    +
    %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
     
    -

    Then add the view helpers file and

    -

    vendor/plugins/yaffle/lib/view_helpers.rb

    +

    vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:

    -
    module YaffleViewHelper
    -  def squawk_info_for(yaffle)
    -    returning "" do |result|
    -      result << yaffle.read_attribute(yaffle.class.yaffle_text_field)
    -      result << ", "
    -      result << yaffle.read_attribute(yaffle.class.yaffle_date_field).to_s
    -    end
    +
    module WoodpeckersHelper
    +
    +  def tweet(text)
    +    "Tweet! #{text}"
       end
    +
     end
     
    -

    You can also test this in script/console by using the helper method:

    -
    -
    -
    $ ./script/console
    ->> helper.squawk_info_for(@some_yaffle_instance)
    -
    +

    Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.

    9. Add a Custom Route

    -- cgit v1.2.3