Wednesday 6 January 2010

Accessing helpers in models

Have a couple of v simple helpers which I needed access to in my model for the PDF.
Simply trying to call them fails because application_helper.rb is only available to views and controllers.
Bit of googling and tip o' the hat to hJLHPBv2 and to So far, it’s RoR for some helpful pointers.
Hived off the helper methods into their own Module and put it in the ./lib folder.

module DisplayHelper


def img_list(index = nil)
img_array = [["JPEG", 1], ["TIFF", 2], ["EPS", 3], ["Lo-res", 4], ["WN", 5], ["CMYK",6]]
if (index)
img_array[index-1][0]
else
img_array
end
end

def supply_on_list(index = nil)
supply_array = [["CD", 1], ["DVD", 2], ["CD&DVD", 3], ["email", 4], ["WN", 5], ["Zip",6], ["Multiple",7]]
if (index)
supply_array[index-1][0]
else
supply_array
end
end

def scan_or_photo(index = nil)
scan_array = [["Scanning", 1], ["Photography", 2], ["Scanning & Photography", 3]]
if (index)
if (index >0)
scan_array[index-1][0]
else
return ''
end
else
scan_array
end
end

def batch_image_list(index = nil)
batch_image_array = [["Not received", 1], ["Booked in", 2], ["Shot", 3], ["Cut out", 4], ["Retouched", 5], ["Checked",6], ["Approved",7], ["Out",8]]
if (index)
batch_image_array[index-1][0]
else
batch_image_array
end
end
end


Then explicitly called that helper in the model

include DisplayHelper

And ta-da. All works.
I don't think I am offending the MVC gods as there is no mark-up going on

No comments:

Post a Comment