diff options
author | Jeff Dean <jeff@zilkey.com> | 2008-11-14 03:02:05 -0500 |
---|---|---|
committer | Jeff Dean <jeff@zilkey.com> | 2008-11-14 03:02:05 -0500 |
commit | 88a13fad4fae2c2088188008248e15498a2ca466 (patch) | |
tree | 91ef9f1db1ccb13063587cd2bca48247c3efa324 /railties/doc/guides/source/creating_plugins | |
parent | 7eb249291d1c8a8af14c52de4767a36ba8f924e3 (diff) | |
download | rails-88a13fad4fae2c2088188008248e15498a2ca466.tar.gz rails-88a13fad4fae2c2088188008248e15498a2ca466.tar.bz2 rails-88a13fad4fae2c2088188008248e15498a2ca466.zip |
Rails plugin: Expanded helpers section
Diffstat (limited to 'railties/doc/guides/source/creating_plugins')
3 files changed, 52 insertions, 62 deletions
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) ---------------------------------------------------------------- |