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



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

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



37
38
39
# File 'lib/asset_library/helpers.rb', line 37

def helper
  @helper
end

Instance Method Details

#absolute_url(relative_url) ⇒ Object



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

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



90
91
92
# File 'lib/asset_library/helpers.rb', line 90

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

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



86
87
88
# File 'lib/asset_library/helpers.rb', line 86

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

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



81
82
83
84
# File 'lib/asset_library/helpers.rb', line 81

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



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

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



65
66
67
# File 'lib/asset_library/helpers.rb', line 65

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

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



69
70
71
# File 'lib/asset_library/helpers.rb', line 69

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

#url(asset) ⇒ Object



43
44
45
# File 'lib/asset_library/helpers.rb', line 43

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