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
CSS_JS_SHORT_PATH_REGEXP =
/^(javascripts|stylesheets|fonts)\/(.*)$/

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 ##



19
20
21
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19

def filepath
  @filepath
end

#sizeObject

other accessors ##



19
20
21
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19

def size
  @size
end

#uriObject

other accessors ##



19
20
21
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19

def uri
  @uri
end

Instance Method Details

#contentString

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

Returns:

  • (String)

    The content of the asset



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 110

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.

Returns:

  • (String)

    The content of the asset



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

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

#filenameString

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

Returns:

  • (String)

    Name of the file



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

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 ##



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

field :folder

#javascript?Boolean

Is the asset a javascript ?

Returns:

  • (Boolean)

    True if the filename ends with .js



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

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

#mime_typeString

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

Returns:

  • (String)

    The mime type of the file or nil if unknown.



62
63
64
65
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 62

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

#pathString

Return the virtual path of the asset

Returns:

  • (String)

    The virtual path of the asset



43
44
45
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 43

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.

Returns:

  • (Boolean)

    True if it has to be precompiled



101
102
103
104
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 101

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.

Returns:

  • (Integer)

    The priority (0 -> high, 100 -> lower)



92
93
94
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 92

def priority
  self.stylesheet_or_javascript? ? 100 : 0
end

#short_pathString

Return the path without the leading javascripts, stylesheets, fonts font. This is needed by Sprockets. Only relevant for javascripts / stylesshets files.

Returns:

  • (String)

    The path of the asset without the first folder



53
54
55
56
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 53

def short_path
  self.path =~ /^(javascripts|stylesheets|fonts)\/(.*)$/
  $2
end

#stylesheet?Boolean

Is the asset a stylesheet ?

Returns:

  • (Boolean)

    True if the filename ends with .css



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

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

#stylesheet_or_javascript?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 83

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

#to_paramsHash

Return the params used for the API.

Returns:

  • (Hash)

    The params



136
137
138
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 136

def to_params
  { folder: self.folder }
end

#to_sObject



140
141
142
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 140

def to_s
  self.path
end