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::FormHelper class, you‘ll find that it‘s loaded from the action_view/helpers/form_helper.rb file. You‘ll need to have the ActionPack gem installed for this to work.

  require 'action_view/helpers/form_helper.rb'

  # This example is unfinished.. soon..

Methods

/   R   URL   errors_for  

Public Instance methods

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"

[Source]

     # File lib/camping-unabridged.rb, line 238
238:     def /(p); p[/^\//]?@root+p:p end

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.

You may also pass in a model object and the ID of the object will be used.

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 Blog::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="//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>

[Source]

     # File lib/camping-unabridged.rb, line 205
205:     def R(c,*g)
206:       p=/\(.+?\)/
207:       g.inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|
208:         s.sub p,C.escape((a[a.class.primary_key]rescue a))
209:       }
210:     end

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. No scheme is given (http or https).

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://test.ing/blog/view/12>

Or you can use the direct path:

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

Since no scheme is given, you will need to add the scheme yourself:

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

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

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

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

[Source]

     # File lib/camping-unabridged.rb, line 265
265:     def URL c='/',*a
266:       c = R(c, *a) if c.respond_to? :urls
267:       c = self/c
268:       c = "//"+@env.HTTP_HOST+c if c[/^\//]
269:       URI(c)
270:     end

Shows AR validation errors for the object passed. There is no output if there are no errors.

An example might look like:

  errors_for @post

Might (depending on actual data) render something like this in Markaby:

  ul.errors do
    li "Body can't be empty"
    li "Title must be unique"
  end

Add a simple ul.errors {color:red; font-weight:bold;} CSS rule and you have built-in, usable error checking in only one line of code. :-)

See AR validation documentation for details on validations.

[Source]

     # File lib/camping-unabridged.rb, line 230
230:     def errors_for(o); ul.errors { o.errors.each_full { |er| li er } } if o.errors.any?; end

[Validate]