Module: Motor::Assets

Defined in:
lib/motor/assets.rb

Constant Summary collapse

InvalidPathError =
Class.new(StandardError)
ASSETS_PATH =
Pathname.new(__dir__).join('../../ui/dist')
MANIFEST_PATH =
ASSETS_PATH.join('manifest.json')
DEV_SERVER_URL =
'http://localhost:9090/'
CACHE_STORE =
if Rails.env.production?
  ActiveSupport::Cache::MemoryStore.new(size: 5.megabytes)
else
  ActiveSupport::Cache::NullStore.new
end

Class Method Summary collapse

Class Method Details

.asset_path(path) ⇒ Object


32
33
34
# File 'lib/motor/assets.rb', line 32

def asset_path(path)
  Motor::Admin.routes.url_helpers.motor_asset_path(manifest[path])
end

.iconsObject


26
27
28
29
30
# File 'lib/motor/assets.rb', line 26

def icons
  manifest.select do |k, v|
    !k.ends_with?('.gz') && v.starts_with?('icons/') && v.exclude?('DS_Store')
  end.keys
end

.load_asset(filename, gzip: false) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/motor/assets.rb', line 36

def load_asset(filename, gzip: false)
  if Motor.development?
    load_from_dev_server(filename)
  else
    load_from_disk(filename, gzip: gzip)
  end
end

.load_from_dev_server(filename) ⇒ Object


56
57
58
59
60
# File 'lib/motor/assets.rb', line 56

def load_from_dev_server(filename)
  uri = URI(DEV_SERVER_URL + filename)

  Net::HTTP.get_response(uri).body
end

.load_from_disk(filename, gzip:) ⇒ Object

Raises:


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/motor/assets.rb', line 44

def load_from_disk(filename, gzip:)
  filename += '.gz' if gzip

  raise InvalidPathError if filename.include?('..')

  path = ASSETS_PATH.join(filename)

  raise InvalidPathError unless path.to_s.starts_with?(ASSETS_PATH.to_s)

  path.read
end

.manifestObject


20
21
22
23
24
# File 'lib/motor/assets.rb', line 20

def manifest
  CACHE_STORE.fetch('manifest') do
    JSON.parse(MANIFEST_PATH.read)
  end
end