Module: Eldr::Assets::Helpers

Defined in:
lib/eldr/assets/helpers.rb

Constant Summary collapse

APPEND_ASSET_EXTENSIONS =
%w(js css)
ABSOLUTE_URL_PATTERN =
%r{^(https?://)}

Instance Method Summary collapse

Instance Method Details

#asset_path(kind, source = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eldr/assets/helpers.rb', line 30

def asset_path(kind, source = nil)
  kind, source = source, kind if source.nil?
  source = asset_normalize_extension(kind, URI.escape(source.to_s))

  return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//

  source      = File.join(asset_folder_name(kind).to_s, source)
  timestamp   = asset_timestamp(source)
  result_path = uri_root_path(source)

  "#{result_path}#{timestamp}"
end

#css(*sources) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/eldr/assets/helpers.rb', line 11

def css(*sources)
  options = {
    rel: 'stylesheet',
    type: 'text/css'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << tag(:link, { href: asset_path(:css, source) }.update(options))
  end
end

#js(*sources) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/eldr/assets/helpers.rb', line 21

def js(*sources)
  options = {
    type: 'text/javascript'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << (:script, nil, { src: asset_path(:js, source) }.update(options))
  end
end

#uri_root_path(*paths) ⇒ Object



43
44
45
46
47
# File 'lib/eldr/assets/helpers.rb', line 43

def uri_root_path(*paths)
  root_uri   = self.configuration.uri_root
  root_uri ||= ENV['ROOT_URI'] if ENV.include? 'ROOT_URI'
  File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
end