[ANN] RequireResourceHelper for JS and CSS

The following was posted on my blog at http://blog.inquirylabs.com.

A friend of mine recently expressed interest in this code snippet, so
I thought I’d send it out to anyone else who’s interested. This
module makes it a lot easier to ‘require’ javascript and cascading
stylesheet dependencies inside your views or controllers. By
‘convention’, the code will automatically include any javascript or
CSS files whose name matches the current controller’s name. In
addition, any file named like ‘controller_name_action_name.css’ will
also be included for specific actions (likewise for JS). It was
inspired by a portion of code I saw from Demetrius Nunes.

To use it, put the following in your application.rb:

class ApplicationController < ActionController::Base
  helper 'require_resource'
end

Then in my layout’s HTML “head” section:

<%= stylesheet_auto_link_tags %>
<%= javascript_auto_include_tags %>

Supposing I am writing an action for WelcomeController called
‘index.rhtml’, (and that each of the files actually exists), I can
now assume that this HTML will be present in the layout:

<script src="/javascripts/welcome.js" type="text/javascript"></script>
<script src="/javascripts/welcome_index.js" type="text/javascript"></

script>

If the files don’t exist, the above HTML won’t be included.

In the case that the CSS or JS is not named ‘by convention’ as a file
that corresponds with the controller’s name, I use the
“require_javascript” code. In one of my views (index.rhtml, for
example):

<% require_javascript 'popitup' %>

Then, I can call the js function popitup later:
<%= link_to_function “click me”, “popitup(‘#{url_for :action =>
‘click’}’)” %>

module RequireResourceHelper

Adds resources such as stylesheet or javascript files to the

corresponding array of

resources that will be ‘required’ by the layout. The

+resource_type+ is either

:javascript or :stylesheet. The +extension+ is optional, and

should normally correspond

with the resource type, e.g. ‘css’ for :stylesheet and ‘js’

for :javascript.
def autorequire(resource_type, extension = nil)
extensions = {:stylesheet => ‘css’, :javascript => ‘js’}
path = “#{RAILS_ROOT}/public/#{resource_type.to_s.pluralize}/”
candidates = [ “#{controller.controller_name}”,
“#{controller.controller_name}_#
{controller.action_name}” ]

 for candidate in candidates
   if FileTest.exist?("#{path}/#{candidate}.#{extension ||

extensions[resource_type]}")
require_resource(resource_type, candidate)
end
end
end

def stylesheet_auto_link_tags
autorequire(:stylesheet)
@stylesheets.uniq.inject(“”) do |buffer, css|
buffer << stylesheet_link_tag(css) + "\n "
end
end

def javascript_auto_include_tags
autorequire(:javascript)
@javascripts.uniq.inject(“”) do |buffer, js|
buffer << javascript_include_tag(js) + "\n "
end
end

def require_resource(type, name)
variable = type.to_s.pluralize
array = instance_variable_get(“@#{variable}”)
instance_variable_set(“@#{variable}”, (array || []) | [name])
end

def require_javascript(script)
require_resource(:javascript, script)
end

def require_stylesheet(sheet)
require_resource(:stylesheet, sheet)
end
end

Duane J.
(canadaduane)
http://blog.inquirylabs.com/

Pretty cool!

I wrote something similar to this, where it would just check for the
existance of a resouce before including it. but this is more
comprehensive.
thanks!