Class: AssetManager

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

Constant Summary collapse

ASSET_DIR =
{
  :js => ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR,
  :css => ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
}
SHORTCUTS =
{
  :css => {},
  :js => { :defaults => ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES }
}

Instance Method Summary collapse

Constructor Details

#initialize ⇒ AssetManager

Returns a new instance of AssetManager.



13
14
15
16
# File 'lib/asset_manager.rb', line 13

def initialize
  @denied = { :css => [], :js => [] }
  @required = { :css => [], :js => [] }
end

Instance Method Details

#add(asset_type, *assets) ⇒ Object

Add assets to the list of required assets



19
20
21
# File 'lib/asset_manager.rb', line 19

def add(asset_type, *assets)
  @required[asset_type] << assets
end

#append(asset_type, *assets) ⇒ Object

Append assets to the front of the list of assets



24
25
26
# File 'lib/asset_manager.rb', line 24

def append(asset_type, *assets)
  @required[asset_type] = assets + @required[asset_type]
end

#auto_add(asset_type, request_params) ⇒ Object

Automaticly add assets based the controller/action params, if they exsist



29
30
31
32
# File 'lib/asset_manager.rb', line 29

def auto_add(asset_type, request_params)
  files = self.auto_find(asset_type, request_params)
  self.add(asset_type, files)
end

#deny(asset_type, *assets) ⇒ Object

Prevent an asset from being required



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

def deny(asset_type, *assets)
  @denied[asset_type] << assets
end

#required(asset_type) ⇒ Object

Returns a list of assets that have been required



40
41
42
43
44
# File 'lib/asset_manager.rb', line 40

def required(asset_type)
  files = extract(asset_type)
  files = process_shortcuts(asset_type, files)
  files
end

#required_with_extra(asset_type, extra, auto, request_params = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/asset_manager.rb', line 46

def required_with_extra(asset_type, extra, auto, request_params = nil)
  files = []
  self.required(asset_type).each do |file|
    path = "#{file}-#{extra}"
    asset_path = "#{path}.#{asset_type}"
    file_path = "#{ASSET_DIR[asset_type]}/#{asset_path}"
    files << path if File.exists?(file_path)
  end
  files += auto_find(asset_type, request_params) if auto
  files
end