Class: Locomotive::ThemeAsset

Inherits:
Object
  • Object
show all
Includes:
Extensions::Asset::Types, Mongoid::Document
Defined in:
app/models/locomotive/theme_asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#performing_plain_textObject

accessors ##



45
46
47
# File 'app/models/locomotive/theme_asset.rb', line 45

def performing_plain_text
  @performing_plain_text
end

#plain_textObject

accessors ##



45
46
47
# File 'app/models/locomotive/theme_asset.rb', line 45

def plain_text
  @plain_text
end

#plain_text_nameObject

accessors ##



45
46
47
# File 'app/models/locomotive/theme_asset.rb', line 45

def plain_text_name
  @plain_text_name
end

#plain_text_typeObject

accessors ##



45
46
47
# File 'app/models/locomotive/theme_asset.rb', line 45

def plain_text_type
  @plain_text_type
end

Class Method Details

.all_grouped_by_folder(site) ⇒ Object



107
108
109
110
# File 'app/models/locomotive/theme_asset.rb', line 107

def self.all_grouped_by_folder(site)
  assets = site.theme_assets.order_by(:slug.asc)
  assets.group_by { |a| a.folder.split('/').first.to_sym }
end

.checksumsObject



112
113
114
115
116
117
118
# File 'app/models/locomotive/theme_asset.rb', line 112

def self.checksums
  {}.tap do |hash|
    self.only(:local_path, :checksum).each do |asset|
      hash[asset.local_path] = asset.checksum
    end
  end
end

Instance Method Details

#local_path(short = false) ⇒ Object

fields ##



10
# File 'app/models/locomotive/theme_asset.rb', line 10

field :local_path

#performing_plain_text?Boolean

Returns:



84
85
86
# File 'app/models/locomotive/theme_asset.rb', line 84

def performing_plain_text?
  Boolean.set(self.performing_plain_text) || false
end

#siteObject

validations ##



21
# File 'app/models/locomotive/theme_asset.rb', line 21

belongs_to :site, class_name: 'Locomotive::Site', validate: false, autosave: false

#store_plain_textObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/locomotive/theme_asset.rb', line 88

def store_plain_text
  return if self.persisted? && !self.stylesheet_or_javascript?

  self.content_type ||= @plain_text_type if self.performing_plain_text?

  data = self.performing_plain_text? ? self.plain_text : self.source.read

  return if !self.stylesheet_or_javascript? || self.plain_text_name.blank? || data.blank?

  sanitized_source = self.escape_shortcut_urls(data)

  self.source = ::CarrierWave::SanitizedFile.new({
    tempfile: StringIO.new(sanitized_source),
    filename: "#{self.plain_text_name}.#{self.stylesheet? ? 'css' : 'js'}"
  })

  @plain_text = sanitized_source # no need to reset the plain_text instance variable to have the last version
end

#stylesheet_or_javascript?Boolean

methods ##

Returns:



50
51
52
# File 'app/models/locomotive/theme_asset.rb', line 50

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

#to_liquidObject



120
121
122
# File 'app/models/locomotive/theme_asset.rb', line 120

def to_liquid
  { url: self.source.url }.merge(self.attributes).stringify_keys
end