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
#
# == 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}.flatten.compact
includes.collect {|ajaxlib| javascript_include_tag ajaxlib.include_path}.join("\n")
end
private
def javascript_include_library(library, options)
library = library.to_sym
version = options.delete(:version)
local = options.delete(:local)
options[:source] ||= (local === true or (local.nil? and not Ajaxlibs::ProductionEnvironments.include?(RAILS_ENV))) ? :local : :remote
ajaxlib = Ajaxlibs::Library.by_name(library, options.merge({:version => version}))
@included_javascript_libraries ||= []
return if @included_javascript_libraries.include?(ajaxlib)
@included_javascript_libraries << ajaxlib
result = []
ajaxlib.requires.each do |required_library, required_version|
result << javascript_include_library(required_library, options.merge({:version => required_version}))
end
result << ajaxlib
end
end