module Ajaxlibs::IncludesHelper
# Returns an html script tag for each javascript library name provided.
# By default, javascript files are loaded locally for development and test environment,
# and through Google CDN on production environment. Basic dependencies are automatically handled.
#
# == Options
# * version : specify the version to use for each library
# * local : if true, always serve file locally, if false, use Google CDN
# * remote : if false, always serve file locally, if true, use Google CDN
#
# == Exceptions
# * Ajaxlibs::Exception::LibraryNotFound : raised if one or more of the given library is not available
# * Ajaxlibs::Exception::VersionNotFound : raised if given version is not available for this/these library/libraries
#
# == Examples
# * Simple library load, under the development environment
# ajaxlibs_include :jquery
#
#
# ajaxlibs_include :jquery, :jqueryui
#
#
#
# * Same examples as above, this time in production
# ajaxlibs_include :jquery
#
#
# ajaxlibs_include :jquery, :jqueryui
#
#
#
# * Specifying version
# ajaxlibs_include :prototype, :version => '1.6.0.3'
#
#
# * Automatic dependencies
# ajaxlibs_include :scriptaculous
#
#
#
def ajaxlibs_include(*args)
options = (Hash === args.last) ? args.pop : {}
includes = args.collect {|library| javascript_include_library library, options}.compact.join("\n")
if options[:local] === false or RAILS_ENV == 'production'
<<-EOB
#{javascript_tag includes}
EOB
else
includes
end
end
private
def javascript_include_library(library, options)
library = library.to_sym
@included_javascript_libraries ||= []
return if @included_javascript_libraries.include?(library)
version = options.delete(:version)
ajaxlib = Ajaxlibs::Library.by_name(library)
result = []
# Handle dependencies between libraries
if ajaxlib.requires and !@included_javascript_libraries.include?(ajaxlib.requires.to_sym)
result << javascript_include_library(ajaxlib.requires, options)
end
@included_javascript_libraries << library
# Javascript load code
if options[:local] === true or RAILS_ENV != 'production'
result << javascript_include_tag(ajaxlib.local_path(version))
else
result << ajaxlib.google_cdn_load_code(version)
end
result.join("\n")
end
end