Skip to content

Commit

Permalink
Reworked javascript_include_tag behavior: now “:mootools” only gives …
Browse files Browse the repository at this point in the history
…you the MooTools libs, and “:behaviours” only gives you inclusion “/public/behaviours/…” scripts. “:defaults” behaves as before, giving you the MooTools libs, followed by 'application.js', followed by behaviours.
  • Loading branch information
capnslipp committed Jun 5, 2010
1 parent 472ddfc commit c345969
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/rails/asset_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,43 @@ module ActionView::Helpers::AssetTagHelper

MOOTOOLS_CORE_SOURCE = 'mootools-1.2.4-core'
MOOTOOLS_MORE_SOURCE = 'mootools-1.2.4.4-more'
JAVASCRIPT_DEFAULT_SOURCES = [MOOTOOLS_CORE_SOURCE, MOOTOOLS_MORE_SOURCE, 'application', 'mootools_patch']
@@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup

def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
@sources = sources
@min = options.delete('min')

if sources.delete :mootools
sources = sources.concat(
[minifiable(MOOTOOLS_CORE_SOURCE), minifiable(MOOTOOLS_MORE_SOURCE), 'application', behaviours_url]
).uniq
elsif sources.delete :defaults
sources = [minifiable(MOOTOOLS_CORE_SOURCE), minifiable(MOOTOOLS_MORE_SOURCE), 'mootools_patch', 'application', behaviours_url].concat(sources)
end
replace_source(
:defaults,
[:mootools, 'application', :behaviours]
)

replace_source(
:mootools,
[minifiable(MOOTOOLS_CORE_SOURCE), minifiable(MOOTOOLS_MORE_SOURCE), 'mootools_patch']
)

replace_source(
:behaviours,
[behaviours_url]
)

rails_javascript_include_tag(*sources)
rails_javascript_include_tag(*@sources)
ensure
@sources, @min = nil
end

protected
def replace_source(original_source, replacement_sources)
replace_index = @sources.index(original_source)
unless replace_index.nil?
# insert after, preserving the index of the source we're replacing
@sources.insert(replace_index + 1, *replacement_sources)
@sources.delete_at(replace_index)
end
@sources
end

def minifiable(source)
source += '.min' if @min
source
Expand Down

0 comments on commit c345969

Please sign in to comment.