Class: Releaf::AssetsResolver

Inherits:
Object
  • Object
show all
Defined in:
app/lib/releaf/assets_resolver.rb

Constant Summary collapse

NONCOMPILED_PATTERN =
/(javascripts|stylesheets)\/(controllers\/(.*?))\..*/
COMPILED_PATTERN =
/controllers\/(.*?)\.(js|css)$/
TYPE_EXTENSION_MAP =
{
  "stylesheets" => "css",
  "javascripts" => "js",
}

Class Method Summary collapse

Class Method Details

.assetsObject


43
44
45
46
47
48
49
# File 'app/lib/releaf/assets_resolver.rb', line 43

def self.assets
  if compiled_assets?
    @@compiled_assets ||= assets_hash(compiled_assets)
  else
    assets_hash(noncompiled_assets)
  end
end

.assets_hash(assets) ⇒ Object


37
38
39
40
41
# File 'app/lib/releaf/assets_resolver.rb', line 37

def self.assets_hash(assets)
  assets.inject({}) do|hash, asset|
    hash.update(asset => asset)
  end
end

.base_assetsObject


10
11
12
# File 'app/lib/releaf/assets_resolver.rb', line 10

def self.base_assets
  ["releaf/application"]
end

.compiled_assetsObject


26
27
28
29
30
31
# File 'app/lib/releaf/assets_resolver.rb', line 26

def self.compiled_assets
  Rails.application.assets_manifest.files.map do|_, asset|
    match = asset["logical_path"].match(COMPILED_PATTERN)
    asset["logical_path"] if match
  end.compact.uniq
end

.compiled_assets?Boolean

Returns:

  • (Boolean)

33
34
35
# File 'app/lib/releaf/assets_resolver.rb', line 33

def self.compiled_assets?
  Rails.application.assets.nil?
end

.controller_assets(controller, type) ⇒ Object


14
15
16
17
# File 'app/lib/releaf/assets_resolver.rb', line 14

def self.controller_assets(controller, type)
  asset_path = "controllers/#{controller}.#{TYPE_EXTENSION_MAP[type.to_s]}"
  base_assets + [assets[asset_path]].compact
end

.noncompiled_assetsObject


19
20
21
22
23
24
# File 'app/lib/releaf/assets_resolver.rb', line 19

def self.noncompiled_assets
  Rails.application.assets.each_file.map do|file|
    match = file.to_s.match(NONCOMPILED_PATTERN)
    "#{match[2]}.#{TYPE_EXTENSION_MAP[match[1]]}" if match
  end.compact
end