Class: DynamicAssets::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_assets/reference.rb

Direct Known Subclasses

JavascriptReference, StylesheetReference

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Reference

Returns a new instance of Reference.



21
22
23
24
# File 'lib/dynamic_assets/reference.rb', line 21

def initialize(attrs = {})
  @name = attrs[:name]
  @member_names = attrs[:member_names]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/dynamic_assets/reference.rb', line 7

def name
  @name
end

Class Method Details

.new_for_type(type, attrs = {}) ⇒ Object

Class Methods



13
14
15
16
17
18
19
# File 'lib/dynamic_assets/reference.rb', line 13

def self.new_for_type(type, attrs = {})
  case type
  when :stylesheets then StylesheetReference
  when :javascripts then JavascriptReference
  else raise "unknown type: #{type}"
  end.new attrs
end

Instance Method Details

#content(context, for_signature = false) ⇒ Object



54
55
56
57
58
59
# File 'lib/dynamic_assets/reference.rb', line 54

def content(context, for_signature = false)
  @context = context
  s = combine_content for_signature
  s = minify s if DynamicAssets::Manager.minify? && !for_signature
  s
end

#formatsObject

Instance Methods



31
32
33
# File 'lib/dynamic_assets/reference.rb', line 31

def formats
  raise "subclasses of #{self.class} should implement this method to return an array of formats"
end

#member_namesObject



39
40
41
# File 'lib/dynamic_assets/reference.rb', line 39

def member_names
  @member_names ||= [name]
end

#member_names=(some_names) ⇒ Object



35
36
37
# File 'lib/dynamic_assets/reference.rb', line 35

def member_names=(some_names)
  @member_names = some_names
end

#member_rootObject



47
48
49
50
51
52
# File 'lib/dynamic_assets/reference.rb', line 47

def member_root
  return @member_root if @member_root

  possible_roots = ["#{Rails.root}/app/assets/#{type.to_s}", "#{Rails.root}/app/views/#{type.to_s}"]
  @member_root = File.find_existing(possible_roots) || possible_roots.first
end

#minify(content_string) ⇒ Object



72
73
74
# File 'lib/dynamic_assets/reference.rb', line 72

def minify(content_string)
  raise "subclasses of #{self.class} should implement this method"
end

#pathsObject



43
44
45
# File 'lib/dynamic_assets/reference.rb', line 43

def paths
  member_names.map { |member_name| path_for_member_name member_name }
end

#signature(context) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/dynamic_assets/reference.rb', line 61

def signature(context)
  # Note that the signature is based on the context at the time the
  # asset helper is called, which is different from the context at
  # the time of asset rendering.
  #
  # To force a change in signature, set or update the ASSET_VERSION
  # config variable.

  (ENV['ASSET_VERSION'] || "") + Digest::SHA1.hexdigest(content(context, true))
end