Module: Camping::Helpers

Module | Camping::Helpers
In: | lib/camping-unabridged.rb

Helpers contains methods available in your controllers and views. You may add methods of your own to this module, including many helper methods from Rails. This is analogous to Rails’ `ApplicationHelper` module.

## Using ActionPack Helpers

If you‘d like to include helpers from Rails’ modules, you‘ll need to look up the helper module in the Rails documentation at api.rubyonrails.org/.

For example, if you look up the `ActionView::Helpers::FormTagHelper` class, you‘ll find that it‘s loaded from the `action_view/helpers/form_tag_helper.rb` file. You‘ll need to have the ActionPack gem installed for this to work.

Often the helpers depends on other helpers, so you would have to look up the dependencies too. `FormTagHelper` for instance required the `content_tag` provided by `TagHelper`.

      require 'action_view/helpers/form_tag_helper'

      module Nuts::Helpers
        include ActionView::Helpers::TagHelper
        include ActionView::Helpers::FormTagHelper
      end

## Return a response immediately

If you need to return a response inside a helper, you can use `throw :halt`.

      module Nuts::Helpers
        def requires_login!
          unless @state.user_id
            redirect Login
            throw :halt
          end
        end
      end

      module Nuts::Controllers
        class Admin
          def get
            requires_login!
            "Never gets here unless you're logged in"
          end
        end
      end

### Methods

/ R URL

### Public Instance methods

/(p)

Simply builds a complete path from a path `p` within the app. If your application is mounted at `/blog`:

      self / "/view/1"    #=> "/blog/view/1"
      self / "styles.css" #=> "styles.css"
      self / R(Edit, 1)   #=> "/blog/edit/1"

R(c,*g)

From inside your controllers and views, you will often need to figure out the route used to get to a certain controller `c`. Pass the controller class and any arguments into the R method, a string containing the route will be returned to you.

Assuming you have a specific route in an edit controller:

      class Edit < R '/edit/(\d+)'

A specific route to the Edit controller can be built with:

      R(Edit, 1)

Which outputs: `/edit/1`.

If a controller has many routes, the route will be selected if it is the first in the routing list to have the right number of arguments.

## Using R in the View

Keep in mind that this route doesn‘t include the root path. You will need to use `/` (the slash method above) in your controllers. Or, go ahead and use the Helpers#URL method to build a complete URL for a route.

However, in your views, the :href, :src and :action attributes automatically pass through the slash method, so you are encouraged to use `R` or `URL` in your views.

     module Nuts::Views
       def menu
         div.menu! do
           a 'Home', :href => URL()
           a 'Profile', :href => "/profile"
           a 'Logout', :href => R(Logout)
           a 'Google', :href => 'http://google.com'
         end
       end
     end

Let‘s say the above example takes place inside an application mounted at `localhost:3301/frodo` and that a controller named `Logout` is assigned to route `/logout`. The HTML will come out as:

      <div id="menu">
        <a href="http://localhost:3301/frodo/">Home</a>
        <a href="/frodo/profile">Profile</a>
        <a href="/frodo/logout">Logout</a>
        <a href="http://google.com">Google</a>
      </div>

URL(c='/',*a)

Builds a URL route to a controller or a path, returning a URI object. This way you‘ll get the hostname and the port number, a complete URL.

You can use this to grab URLs for controllers using the R-style syntax. So, if your application is mounted at `test.ing/blog/` and you have a View controller which routes as `R ’/view/(\d+)’`:

      URL(View, @post.id)    #=> #<URL:http://test.ing/blog/view/12>

Or you can use the direct path:

      self.URL               #=> #<URL:http://test.ing/blog/>
      self.URL + "view/12"   #=> #<URL:http://test.ing/blog/view/12>
      URL("/view/12")        #=> #<URL:http://test.ing/blog/view/12>

It‘s okay to pass URL strings through this method as well:

      URL("http://google.com")  #=> #<URL:http://google.com>

Any string which doesn‘t begin with a slash will pass through unscathed.

[Validate]