views:

181

answers:

2

When you call javascript_include_tag :defaults you usually get: prototype.js, effects.js, dragdrop.js, and controls.js.

These are stored in a constant in ActionView::Helpers::AssetTagHelper called 'JAVASCRIPT_DEFAULT_SOURCES`. My application uses jQuery, so I want to replace the Prototype references with something more useful.

I added an initializer with these lines, based on the source code from jRails:

ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES = %w{ jquery-1.4.min jquery-ui jquery.cookie }
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default

But when I do this, I get: warning: already initialized constant JAVASCRIPT_DEFAULT_SOURCES during startup.

What's the correct way of changing this value? In the source code it checks for the constant before setting it, but apparently that happens before it runs the initializer scripts.


The Rails 3.0 release will provide much greater flexibility with choice of JS libraries, so I guess this is a problem with an expiration date.

A: 

Why not just do something like this?

<%= javascript_include_tag "jquery-1.4.min", "jquery-ui", "jquery.cookie" %>

You dont need to use the ":defaults" option. Take at look at:

http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#M001713

Keith Pitt
Of course I'm already doing that. I'm trying to figure out how to use `:defaults` simply because I _want_ to. There's no reason why a web framework should be so tightly-coupled to a javascript framework.
Adam Lassek
A: 

According to this thread on ruby-forum, you can't. Although, if you look here, there are a few .diff files that you could grab that would at least let you do something like this:

# environment.rb
ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey, "head", "body", "tail"
ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey, "head", "body", "tail"

# your .erb/.haml files
javascript_include_tag :monkey
stylesheet_link_tag :monkey

I know this obviously doesn't help you do what you're trying to do, but hopefully it at least helps point you in a positive direction.

jerhinesmith
Yeah, I saw that, although there was never a definite resolution to the question hence my asking it here. Hard-coding this stuff is such bad design I can't believe they don't provide an easy way to change it.
Adam Lassek
Agreed. One of the first things I do in any new rails project is swap out all of the prototype stuff for jquery.
jerhinesmith