Class: AssetManager
- Inherits:
-
Object
- Object
- AssetManager
- 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
-
#add(asset_type, *assets) ⇒ Object
Add assets to the list of required assets.
-
#append(asset_type, *assets) ⇒ Object
Append assets to the front of the list of assets.
-
#auto_add(asset_type, request_params) ⇒ Object
Automaticly add assets based the controller/action params, if they exsist.
-
#deny(asset_type, *assets) ⇒ Object
Prevent an asset from being required.
-
#initialize ⇒ AssetManager
constructor
A new instance of AssetManager.
-
#required(asset_type) ⇒ Object
Returns a list of assets that have been required.
- #required_with_extra(asset_type, extra) ⇒ Object
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 33 34 35 36 37 38 39 40 |
# File 'lib/asset_manager.rb', line 29 def auto_add(asset_type, request_params) files, prefix = [], '' (request_params[:controller] + '/' + request_params[:action]).split('/').each do |path| path = prefix + path asset_path = "#{path}.#{asset_type}" file_path = "#{ASSET_DIR[asset_type]}/#{asset_path}" files << path if File.exists?(file_path) prefix = path + '/' end self.add(asset_type, files) end |
#deny(asset_type, *assets) ⇒ Object
Prevent an asset from being required
43 44 45 |
# File 'lib/asset_manager.rb', line 43 def deny(asset_type, *assets) @denied[asset_type] << assets end |
#required(asset_type) ⇒ Object
Returns a list of assets that have been required
48 49 50 51 52 |
# File 'lib/asset_manager.rb', line 48 def required(asset_type) files = extract(asset_type) files = process_shortcuts(asset_type, files) files end |
#required_with_extra(asset_type, extra) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/asset_manager.rb', line 54 def required_with_extra(asset_type, extra) 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 end |