lib/roda/plugins/assets.rb in roda-2.0.0 vs lib/roda/plugins/assets.rb in roda-2.1.0

- old
+ new

@@ -50,10 +50,14 @@ # # You can add attributes to the tags by using an options hash: # # <%= assets(:css, :media => 'print') %> # + # The assets method will respect the application's :add_script_name option, + # if it set it will automatically prefix the path with the SCRIPT_NAME for + # the request. + # # == Asset Groups # # The asset plugin supports groups for the cases where you have different # css/js files for your front end and back end. To use asset groups, you # pass a hash for the :css and/or :js options: @@ -207,24 +211,24 @@ # :headers :: A hash of additional headers for both js and css rendered files # :js_dir :: Directory name containing your javascript source, inside :path (default: 'js') # :js_headers :: A hash of additional headers for your rendered javascript files # :js_opts :: Template options to pass to the render plugin (via :template_opts) when rendering javascript assets # :js_route :: Route under :prefix for javascript assets (default: :js_dir) - # :path :: Path to your asset source directory (default: 'assets') + # :path :: Path to your asset source directory (default: 'assets'). Relative + # paths will be considered relative to the application's :root option. # :prefix :: Prefix for assets path in your URL/routes (default: 'assets') # :precompiled :: Path to the compiled asset metadata file. If the file exists, will use compiled # mode using the metadata in the file. If the file does not exist, will use # non-compiled mode, but will write the metadata to the file if compile_assets is called. - # :public :: Path to your public folder, in which compiled files are placed (default: 'public') + # :public :: Path to your public folder, in which compiled files are placed (default: 'public'). Relative + # paths will be considered relative to the application's :root option. module Assets DEFAULTS = { :compiled_name => 'app'.freeze, :js_dir => 'js'.freeze, :css_dir => 'css'.freeze, - :path => 'assets'.freeze, :prefix => 'assets'.freeze, - :public => 'public'.freeze, :concat_only => false, :compiled => false, :add_suffix => false, :group_subdirs => true, :compiled_css_dir => nil, @@ -267,10 +271,12 @@ else app.opts[:assets] = opts.dup app.opts[:assets][:orig_opts] = opts end opts = app.opts[:assets] + opts[:path] = File.expand_path(opts[:path]||"assets", app.opts[:root]).freeze + opts[:public] = File.expand_path(opts[:public]||"public", app.opts[:root]).freeze # Combine multiple values into a path, ignoring trailing slashes j = lambda do |*v| opts.values_at(*v). reject{|s| s.to_s.empty?}. @@ -463,34 +469,36 @@ else EMPTY_STRING end if type == :js - tag_start = "<script type=\"text/javascript\" #{attrs} src=\"/" + tag_start = "<script type=\"text/javascript\" #{attrs} src=\"" tag_end = JS_END else - tag_start = "<link rel=\"stylesheet\" #{attrs} href=\"/" + tag_start = "<link rel=\"stylesheet\" #{attrs} href=\"" tag_end = CSS_END end + url_prefix = request.script_name if self.class.opts[:add_script_name] + # Create a tag for each individual file if compiled = o[:compiled] if dirs && !dirs.empty? key = dirs.join(DOT) ckey = "#{stype}.#{key}" if ukey = compiled[ckey] - "#{tag_start}#{o[:"compiled_#{stype}_prefix"]}.#{key}.#{ukey}.#{stype}#{tag_end}" + "#{tag_start}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{key}.#{ukey}.#{stype}#{tag_end}" end elsif ukey = compiled[stype] - "#{tag_start}#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}#{tag_end}" + "#{tag_start}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}#{tag_end}" end else asset_dir = o[type] if dirs && !dirs.empty? dirs.each{|f| asset_dir = asset_dir[f]} prefix = "#{dirs.join(SLASH)}/" if o[:group_subdirs] end - Array(asset_dir).map{|f| "#{tag_start}#{o[:"#{stype}_prefix"]}#{prefix}#{f}#{o[:"#{stype}_suffix"]}#{tag_end}"}.join(NEWLINE) + Array(asset_dir).map{|f| "#{tag_start}#{url_prefix}/#{o[:"#{stype}_prefix"]}#{prefix}#{f}#{o[:"#{stype}_suffix"]}#{tag_end}"}.join(NEWLINE) end end # Render the asset with the given filename. When assets are compiled, # or when the file is already of the given type (no rendering necessary),