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 ++++++++-------------- .../doc/guides/source/creating_plugins/helpers.txt | 51 +++++++++++++++ .../doc/guides/source/creating_plugins/index.txt | 2 +- .../guides/source/creating_plugins/view_helper.txt | 61 ------------------ 4 files changed, 78 insertions(+), 111 deletions(-) create mode 100644 railties/doc/guides/source/creating_plugins/helpers.txt delete mode 100644 railties/doc/guides/source/creating_plugins/view_helper.txt (limited to 'railties') 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

    diff --git a/railties/doc/guides/source/creating_plugins/helpers.txt b/railties/doc/guides/source/creating_plugins/helpers.txt new file mode 100644 index 0000000000..51b4cebb01 --- /dev/null +++ b/railties/doc/guides/source/creating_plugins/helpers.txt @@ -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. diff --git a/railties/doc/guides/source/creating_plugins/index.txt b/railties/doc/guides/source/creating_plugins/index.txt index 67e6aec39c..19484e2830 100644 --- a/railties/doc/guides/source/creating_plugins/index.txt +++ b/railties/doc/guides/source/creating_plugins/index.txt @@ -43,7 +43,7 @@ include::models.txt[] include::controllers.txt[] -include::view_helper.txt[] +include::helpers.txt[] include::custom_route.txt[] diff --git a/railties/doc/guides/source/creating_plugins/view_helper.txt b/railties/doc/guides/source/creating_plugins/view_helper.txt deleted file mode 100644 index 4eaec93824..0000000000 --- a/railties/doc/guides/source/creating_plugins/view_helper.txt +++ /dev/null @@ -1,61 +0,0 @@ -== Create a `squawk_info_for` view 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* - -[source, ruby] ---------------------------------------------------------------- -require File.dirname(__FILE__) + '/test_helper.rb' -include YaffleViewHelper - -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) - end -end ---------------------------------------------------------------- - -Then add the following statements to init.rb: - -*vendor/plugins/yaffle/init.rb* - -[source, ruby] ---------------------------------------------------------------- -require "view_helpers" -ActionView::Base.send :include, YaffleViewHelper ---------------------------------------------------------------- - -Then add the view helpers file and - -*vendor/plugins/yaffle/lib/view_helpers.rb* - -[source, ruby] ---------------------------------------------------------------- -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 - end -end ---------------------------------------------------------------- - -You can also test this in script/console by using the `helper` method: - ---------------------------------------------------------------- -$ ./script/console ->> helper.squawk_info_for(@some_yaffle_instance) ---------------------------------------------------------------- -- cgit v1.2.3