weppos / helperful

A collection of useful Rails helpers.

This URL has Read+Write access

README.rdoc

Helperful

Helperful aims to be a collection of useful and reusable Rails helpers.

Rails Installation

As a Gem

This is the preferred way to install Helperful and the best way if you want install a stable version.

  $ gem install weppos-helperful --source http://gems.github.com

You can specify the GEM dependency in your application environment.rb file:

  Rails::Initializer.run do |config|

    # other configurations

    config.gem "weppos-helperful", :lib => "helperful", :source => "http://gems.github.com"
  end

As a Plugin

This is the preferred way if you want to live on the edge and install a development version.

  $ script/plugin install git://github.com/weppos/helperful.git

Getting Started

Helper methods are grouped into different files according to their scope.

Before using an helper method you should include the helper file in your Rails application, as you would expected to do for a standard Rails helper. All helpers belongs to the Helperful namespace to prevent conflicts with default Rails helpers. Don’t forget to add the namespace when including an helper from your controller.

  class MyController < ApplicationController

    # include the title_helper
    helper 'helperful/title'

    # include the title_helper passing the qualified the module name
    helper Helperful::TitleHelper

  end

Moreover, the Helperful library provides a helpful method called helperful. It aliases the standard Rails ActionController::Base#helper method with the exception that it automatically prepends the helperful namespace when necessary.

  class MyController < ApplicationController

    # include the title_helper
    helperful :title

    # include the title_helper passing the qualified the module name
    helperful Helperful::TitleHelper

  end

The following lines are equivalent:

  helper    'helperful/title'
  helper    :'helperful/title'
  helper    Helperful::TitleHelper
  helperful 'title'
  helperful :title
  helperful Helperful::TitleHelper

The helperful methods accepts any parameter accepted by the original helper method.

  helper    'helperful/title', 'helperful/affiliations'
  helperful :title, :affiliations

See the Rails documentation for ActionController::Base#helper method for more details about how to include an helper into a Rails application.

Once included, all helper methods are available in the View.

  <html>
    <title><%= title 'This is a title' %></title>
    <body>
      <%= yield %>
    </body>
  </html>

Helpers

This is a short description of all available helpers. Please refer to the documentation available at the beginning of any helper file for further details.

Affiliations Helper

Provides a set of helpers for working with online affiliations.

The tradedoubler_verification_tag helper method returns the site verification tag required by Tradedoubler to verify the publisher account ownership.

  # In your template
  <html>
    <head>
      <%= tradedoubler_verification_tag('00112233') %>
    </head>
    <body>
      This is your page content.
    </body>
  </html>

  # Will produce the following output.
  <html>
    <head>
      <%= tradedoubler_verification_tag('00112233') %>
    </head>
    <body>
      <!-- TradeDoubler site verification 00112233 -->
    </body>
  </html>

Content Helper

Provides a set of helpers for capturing and working with your page content in a more effective way.

The sidebar helper method is a syntactic sugar addition to store content for your website sidebars.

  <% sidebar do %>
    <div>This is a sidebar module.</div>
  <% end %>
  <% sidebar do %>
    <div>This is an other sidebar module.</div>
  <% end %>

  <%= yield :sidebar # Produces the following output %>
  <div>This is an other sidebar module.</div>
  <div>This is a sidebar module.</div>

The has_content? helper is a natural fulfillment for the original content_for helper.

  <% content_for :foo do %>
    <div>This is a foo content.</div>
  <% end %>

  <% has_content? :foo  # => true %>
  <% has_content? "foo" # => true %>

Javascript Helper

Provides a set of helpers for working with JavaScript in your views.

The javascript_content_for helper combines the features of content_for and javascript_tag into a single helper.

  <% javascript_content_for :head do %>
    $("#id").hide();
  <% end %>

The code above looks like much more readable than the following one. Isn’t it?

  <% javascript_content_for :head do; javascript_tag do %>
    $("#id").hide();
  <% end; end %>

Title Helper

Provides an helper for managing page title in Rails views and layouts.

  # Include the helper in your controller.
  # You might want to include it in ApplicationController to make it available
  # always and everywhere in your templates.
  class ApplicationController < ActionController::Base
    helperful :title
  end

  # Now you can use set a title in your action
  # Example. index.html.rb
  <h1><%= title 'This is a title' %></h1>

  # And print the title with a :site decorator in your layout.
  <html>
    <head>
      <title><%= title :site => 'My Cool Site!' %></title>
    </head>
    <body>
      <%= yield %>
    </body>
  </html>

Originally available at gist.github.com/3840.

Credits

Author:Simone Carletti <weppos@weppos.net>

Resources

License

Copyright © 2008-2009 Simone Carletti, Helperful is released under the MIT license.