Module: Camping::Helpers

Includes:
Controllers, Models
Included in:
Views
Defined in:
lib/camping-unabridged.rb,
lib/camping.rb

Overview

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.

A helper often 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

Constant Summary

Constants included from Controllers

Controllers::A, Controllers::I, Controllers::N

Constants included from Models

Models::A, Models::Base

Instance Method Summary collapse

Methods included from Controllers

D, #M, M, R, v

Instance Method Details

#/(p) ⇒ Object

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"


224
# File 'lib/camping-unabridged.rb', line 224

def /(p) p[0]==?/ ?(@root+@url_prefix.dup.prepend("/").chop+p) : p end

#app_nameObject

Just a helper to tell you the App Name During the instantiation of the app, “Camping” is replaced with the Apps namespace.



258
# File 'lib/camping-unabridged.rb', line 258

def app_name;"Camping"end

#R(c, *g) ⇒ Object

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 http://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>


206
207
208
209
210
# File 'lib/camping-unabridged.rb', line 206

def R c,*g;p,h=
/\(.+?\)/,g.grep(Hash);g-=h;raise"bad route"if !u=c.urls.find{|x|break x if
x.scan(p).size==g.size&&/^#{x}\/?$/=~(x=g.inject(x){|x,a|x.sub p,U.escape((a.
to_param rescue a))}.gsub(CampTools.descape){$1})};h.any?? u+"?"+U.build_query(h[0]) : u
end

#URL(c = '/', *a) ⇒ Object

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 http://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.



249
250
# File 'lib/camping-unabridged.rb', line 249

def URL c='/',*a;c=R(c,*a)if c.respond_to?(
:urls);c=self/c;c=@request.url[/.{8,}?(?=\/|$)/]+c if c[0]==?/;URI c end