app/helpers/requirejs_helper.rb in requirejs-rails-1.0.0 vs app/helpers/requirejs_helper.rb in requirejs-rails-1.0.1

- old
+ new

@@ -1,15 +1,16 @@ require "requirejs/error" -require "requirejs/rails/view_proxy" +require "requirejs/rails/view" module RequirejsHelper - if defined?(Sass::Rails::VERSION) - sass_rails_version_pattern = Regexp.new("\\A(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\z") + def self.included(clazz) + clazz.class_eval do + extend Forwardable - SASS_RAILS_3_COMPATIBILITY = sass_rails_version_pattern.match(Sass::Rails::VERSION)[1].to_i < 4 - else - SASS_RAILS_3_COMPATIBILITY = false + # Delegate all JavaScript path queries to the specially modified internal view. + def_delegators :view, :javascript_path + end end # EXPERIMENTAL: Additional priority settings appended to # any user-specified priority setting by requirejs_include_tag. # Used for JS test suite integration. @@ -46,30 +47,43 @@ run_config[:priority] ||= [] run_config[:priority].concat _priority end if Rails.application.config.assets.digest - modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m } + assets_precompiled = !Rails.application.config.assets.compile + modules = requirejs.build_config["modules"].map {|m| requirejs.module_name_for m} + user_paths = requirejs.build_config["paths"] || {} # Generate digestified paths from the modules spec paths = {} - modules.each { |m| paths[m] = javascript_path(m).sub /\.js$/, '' } - if run_config.has_key? 'paths' + modules.each do |module_name| + script_path = if !assets_precompiled + # If modules haven't been precompiled, search for them based on their user-defined paths before using the + # module name. + user_paths[module_name] || module_name + else + # If modules have been precompiled, the script path is just the module name. + module_name + end + + paths[module_name] = javascript_path(script_path).gsub(/\.js$/, "") + end + + if run_config.has_key? "paths" # Add paths for assets specified by full URL (on a CDN) - run_config['paths'].each do |k, v| + run_config["paths"].each do |k, v| paths[k] = v if v.is_a?(Array) || v =~ /^(https?:)?\/\// - end end # Override user paths, whose mappings are only relevant in dev mode # and in the build_config. - run_config['paths'] = paths + run_config["paths"] = paths end - run_config['baseUrl'] = base_url(name) + run_config["baseUrl"] = base_url(name) html.concat(content_tag(:script) do script = "require.config(#{run_config.to_json});" # Pass an array to `require`, since it's a top-level module about to be loaded asynchronously (see @@ -83,30 +97,10 @@ html.html_safe end end - def javascript_path(source, options = {}) - if defined?(super) - if !SASS_RAILS_3_COMPATIBILITY - super - else - super(source) - end - else - view_proxy.javascript_path(source, options) - end - end - - def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) - if defined?(super) && respond_to?(:output_buffer) && respond_to?(:output_buffer=) - super - else - view_proxy.content_tag(name, content_or_options_with_block, options, escape, &block) - end - end - private def once_guard if defined?(controller) && controller.requirejs_included raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page." @@ -127,9 +121,9 @@ uri = URI.parse(js_asset_path) asset_host = uri.host && js_asset_path.sub(uri.request_uri, '') [asset_host, Rails.application.config.relative_url_root, Rails.application.config.assets.prefix].join end - def view_proxy - @view_proxy ||= Requirejs::Rails::ViewProxy.new + def view + @view ||= Requirejs::Rails::View.new end end