Class: Locomotive::Mounter::Models::ThemeAsset
- 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
-
#filepath ⇒ Object
other accessors ##.
-
#size ⇒ Object
other accessors ##.
-
#uri ⇒ Object
other accessors ##.
Attributes inherited from Base
#_id, #created_at, #mounting_point, #updated_at
Instance Method Summary collapse
-
#content ⇒ String
Content of the asset.
-
#content! ⇒ String
Get a fresh version of the content of the asset.
-
#filename ⇒ String
Name of the file without any precompiled extensions (.sass, .scss, …etc).
-
#folder ⇒ Object
fields ##.
-
#javascript? ⇒ Boolean
Is the asset a javascript ?.
-
#mime_type ⇒ String
Return the mime type of the file based on the Mime::Types lib.
-
#path ⇒ String
Return the virtual path of the asset.
-
#precompiled? ⇒ Boolean
Tell if the asset can be precompiled.
-
#priority ⇒ Integer
Give the priority of the asset depending of its type.
-
#short_path ⇒ String
Return the path without the leading javascripts, stylesheets, fonts font.
-
#stylesheet? ⇒ Boolean
Is the asset a stylesheet ?.
- #stylesheet_or_javascript? ⇒ Boolean
-
#to_params ⇒ Hash
Return the params used for the API.
- #to_s ⇒ Object
Methods inherited from Base
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
#filepath ⇒ Object
other accessors ##
19 20 21 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19 def filepath @filepath end |
#size ⇒ Object
other accessors ##
19 20 21 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19 def size @size end |
#uri ⇒ Object
other accessors ##
19 20 21 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 19 def uri @uri end |
Instance Method Details
#content ⇒ String
Content of the asset. Pre-compile it if needed.
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.
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 |
#filename ⇒ String
Name of the file without any precompiled extensions (.sass, .scss, …etc)
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 |
#folder ⇒ Object
fields ##
16 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 16 field :folder |
#javascript? ⇒ Boolean
Is the asset a javascript ?
79 80 81 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 79 def javascript? File.extname(self.filename) == '.js' end |
#mime_type ⇒ String
Return the mime type of the file based on the Mime::Types lib.
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 |
#path ⇒ String
Return 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.
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 |
#priority ⇒ Integer
Give the priority of the asset depending of its type. Javascripts and stylesheets are low priority.
92 93 94 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 92 def priority self.stylesheet_or_javascript? ? 100 : 0 end |
#short_path ⇒ String
Return the path without the leading javascripts, stylesheets, fonts font. This is needed by Sprockets. Only relevant for javascripts / stylesshets files.
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 ?
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
83 84 85 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 83 def stylesheet_or_javascript? self.stylesheet? || self.javascript? end |
#to_params ⇒ Hash
Return the params used for the API.
136 137 138 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 136 def to_params { folder: self.folder } end |
#to_s ⇒ Object
140 141 142 |
# File 'lib/locomotive/mounter/models/theme_asset.rb', line 140 def to_s self.path end |