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}.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) source = (options[:local] === true or RAILS_ENV != 'production') ? :local : :remote ajaxlib = Ajaxlibs::Library.by_name(library, :version => version, :source => source) @included_javascript_libraries ||= [] return if @included_javascript_libraries.include?(ajaxlib) @included_javascript_libraries << ajaxlib result = [] if ajaxlib.requires ajaxlib.requires.each do |required_library, required_version| result << javascript_include_library(required_library, :version => required_version) end end result << ajaxlib end end