Class: Locomotive::Mounter::Models::ThemeAsset

Inherits:
Base
  • Object
show all
Defined in:
lib/locomotive/mounter/models/theme_asset.rb

Constant Summary collapse

PRECOMPILED_CSS_TYPES =
%w(sass scss less)
PRECOMPILED_JS_TYPES =
%w(coffee)
PRECOMPILED_FILE_TYPES =
PRECOMPILED_CSS_TYPES + PRECOMPILED_JS_TYPES

Instance Attribute Summary collapse

Attributes inherited from Base

#_id, #created_at, #mounting_point, #updated_at

Instance Method Summary collapse

Methods inherited from Base

#initialize, #persisted?

Methods included from Fields

#attributes, #attributes_with_translations, #initialize, #localized_field?, #to_hash, #to_yaml, #translated_in, #translated_in?, #write_attributes

Constructor Details

This class inherits a constructor from Locomotive::Mounter::Models::Base

Instance Attribute Details

#filepathObject

other accessors ##



17
18
19
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 17

def filepath
  @filepath
end

#sizeObject

other accessors ##



17
18
19
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 17

def size
  @size
end

#uriObject

other accessors ##



17
18
19
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 17

def uri
  @uri
end

Instance Method Details

#contentString

Content of the asset. Pre-compile it if needed.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 97

def content
  return @raw if @raw

  if self.uri
    @raw = HTTParty.get(self.uri.to_s).body
  elsif self.precompiled?
    template = Tilt.new(self.filepath)
    @raw = template.render
  else
    @raw = File.read(self.filepath)
  end
end

#content!String

Get a fresh version of the content of the asset. Pre-compile it if needed.



114
115
116
117
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 114

def content!
  @raw = nil # force to reload it
  self.content
end

#filenameString

Name of the file without any precompiled extensions (.sass, .scss, …etc)



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 25

def filename
  return @filename if @filename

  if self.uri
    @filename = File.basename(self.uri.path)
  else
    regexps   = PRECOMPILED_FILE_TYPES.map { |ext| "\.#{ext}" }.join('|')

    @filename = File.basename(self.filepath).gsub(/#{regexps}/, '')
  end
end

#folderObject

fields ##



14
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 14

field :folder

#javascript?Boolean

Is the asset a javascript ?



66
67
68
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 66

def javascript?
  File.extname(self.filename) == '.js'
end

#mime_typeString

Return the mime type of the file based on the Mime::Types lib.



49
50
51
52
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 49

def mime_type
  type = MIME::Types.type_for(self.filename)
  type.empty? ? nil : type.first
end

#pathString

Return the virtual path of the asset



41
42
43
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 41

def path
  File.join(self.folder, self.filename)
end

#precompiled?Boolean

Tell if the asset can be precompiled. For instance, less, sass, scss and coffeescript assets have to be precompiled.



88
89
90
91
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 88

def precompiled?
  @extname ||= File.extname(self.filepath)[1..-1]
  PRECOMPILED_FILE_TYPES.include?(@extname)
end

#priorityInteger

Give the priority of the asset depending of its type. Javascripts and stylesheets are low priority.



79
80
81
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 79

def priority
  self.stylesheet_or_javascript? ? 100 : 0
end

#stylesheet?Boolean

Is the asset a stylesheet ?



58
59
60
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 58

def stylesheet?
  File.extname(self.filename) == '.css'
end

#stylesheet_or_javascript?Boolean



70
71
72
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 70

def stylesheet_or_javascript?
  self.stylesheet? || self.javascript?
end

#to_paramsHash

Return the params used for the API.



123
124
125
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 123

def to_params
  { folder: self.folder }
end

#to_sObject



127
128
129
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 127

def to_s
  self.path
end