Showing posts with label helper. Show all posts
Showing posts with label helper. Show all posts

Tuesday, October 2, 2007

helper: Toggler

I made a new helper to help out with toggling a div. To make sure the div is invisible upon load, make sure you add the style="display: none;" in the div.

To call the helper, use it like this:

<%= toggler("click me to open that div", "div-id") %>

Tuesday, September 25, 2007

helper: check_box_collection

Following the Lynda.com RoR video tutorials, you're taught how to make code for checkboxes. It's a bit of a hassle because there's a lot of cutting and pasting per checkbox collection you want to have. With the following code, you just paste several times to get things working.

I couldn't find a way to display this properly in blogger, so I posted that code in pastie.
Helper: http://pastie.caboo.se/104016

Code you will put in the model that will "contain the check list". For example, if a user has_and_belongs_to_many :roles, this code will be pasted on the User model.
http://pastie.caboo.se/100607

Then, when you want to generate the checklist, simply paste this in your view:
http://pastie.caboo.se/100609

When updating or saving, make sure you tell the user (or the model that has the checklist) to update! I used mine the following way in the controller:
http://pastie.caboo.se/100608

helper: Getting the full URL

There’s nothing built in Rails that gives you the full URL. I made a helper, that gives this to you. It doesn’t display properly here.


def get_full_url(base_url)
return base_url + request.path + get_params_as_url_friendly
end

def get_params_as_url_friendly
count = 0
param_size = request.query_parameters.size
parameters = ""

for param in request.query_parameters
parameter = "#{param[0]}=#{param[1]}"

if count == 0
parameter = "?#{parameter}"
else
parameter = "&#{parameter}"
end

count += 1
parameters += parameter
end

return parameters
end

helper: number_to_php

Here's a helper to convert a number to Philippine Pesos. For other currencies, just replace the symbol.


def number_to_php(amt)
return number_to_currency(amt, :unit => "PHP ")
end

Helper: user_authorized?

When using ACLSystem, you can check if someone has permission to do something. However, before you ask, you have to make sure the user is logged in in the first place. Instead of having all that code in the view, I made a helper so that we won't have to check and see if the
session['user'].nil?
each time we want to ask if the user has permission to do so.


def user_authorized?(perm) #works with ACL controller
return session['user'].authorized?(perm) if not session['user'].nil?
return false
end

Helper: get_url(url)

Answer to making our relative URLs to absolute URLs through the application helper:


def get_url(url)
return "http://localhost:3000" + url
end


If URL were /model/file/1/a.jpg, then you'd get http://localhost:3000/model/file/1/a.jpg in return. When moving to a new webserver, we just need to edit the helper and change the base URL.

Helper: ms_to_hh_mm_ss(number)

Convert a number (milliseconds) the format hh:mm:ss


def ms_to_hh_mm_ss(number)
seconds = "%02d" % (number % 6)
minutes = "%02d" % ( (number / 60000 ) % 60)
hours = "%02d" % ( (number / 3600000) % 24)

return (hours + minutes + seconds).scan(/../).join(':')
end


Place this in the applications_helper.rb to make it available everywhere in the application.