Class: AssetLibrary::Helpers::Priv

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_library/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper) ⇒ Priv

Returns a new instance of Priv.



41
42
43
# File 'lib/asset_library/helpers.rb', line 41

def initialize(helper)
  @helper = helper
end

Instance Attribute Details

#helperObject

Don’t pollute helper’s class’s namespace with all our methods; put them here instead



39
40
41
# File 'lib/asset_library/helpers.rb', line 39

def helper
  @helper
end

Instance Method Details

#absolute_url(relative_url) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/asset_library/helpers.rb', line 49

def absolute_url(relative_url)
  host = helper.__send__(:compute_asset_host, relative_url) if helper.respond_to?(:compute_asset_host, true)

  host = nil if host == '' # Rails sets '' by default

  if host && !(host =~ %r{^[-a-z]+://})
    controller = helper.instance_variable_get(:@controller)
    request = controller && controller.respond_to?(:request) && controller.request
    host = request && "#{request.protocol}#{host}"
  end

  if host
    "#{host}#{relative_url}"
  else
    relative_url
  end
end

#attributes_from_hash(options = {}) ⇒ Object



92
93
94
# File 'lib/asset_library/helpers.rb', line 92

def attributes_from_hash(options = {})
  options.to_a.collect{|k, v| "#{k}=\"#{v}\""}.join(" ")
end

#content_tag(name, content, options = {}) ⇒ Object



88
89
90
# File 'lib/asset_library/helpers.rb', line 88

def (name, content, options = {})        
  "<#{name} #{attributes_from_hash(options)}>#{content}</#{name}>"
end

#import_style_tag(assets, html_options = {}) ⇒ Object



83
84
85
86
# File 'lib/asset_library/helpers.rb', line 83

def import_style_tag(assets, html_options = {})
  imports = assets.collect{ |a| "@import \"#{url(a)}\";" }
  (:style, "\n#{imports.join("\n")}\n", html_options.merge(:type => "text/css"))
end

#import_styles_tag(assets, html_options = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/asset_library/helpers.rb', line 75

def import_styles_tag(assets, html_options = {})
  a = []
  assets.each_slice(30) do |subset|
    a << import_style_tag(subset, html_options)
  end
  a.join("\n")
end

#script_tag(asset) ⇒ Object



67
68
69
# File 'lib/asset_library/helpers.rb', line 67

def script_tag(asset)
  (:script, "", {:type => "text/javascript", :src => url(asset)})
end

#style_tag(asset, html_options = {}) ⇒ Object



71
72
73
# File 'lib/asset_library/helpers.rb', line 71

def style_tag(asset, html_options = {})
  "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{url(asset)}\" #{attributes_from_hash(html_options)}/>"
end

#url(asset) ⇒ Object



45
46
47
# File 'lib/asset_library/helpers.rb', line 45

def url(asset)
  absolute_url(asset.relative_url)
end