Module: Locomotive::Concerns::ThemeAsset::PlainText

Extended by:
ActiveSupport::Concern
Included in:
ThemeAsset
Defined in:
app/models/locomotive/concerns/theme_asset/plain_text.rb

Instance Method Summary collapse

Instance Method Details

#escape_shortcut_urls(text) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 65

def escape_shortcut_urls(text)
  return if text.blank?

  text.gsub(/[("'](\/(stylesheets|javascripts|images|media|fonts|pdfs|others)\/(([^;.]+)\/)*([a-zA-Z_\-0-9]+)\.[a-z]{2,4})(\?[0-9]+)?[)"']/) do |path|

    sanitized_path = path.gsub(/[("')]/, '').gsub(/^\//, '').gsub(/\?[0-9]+$/, '')

    if asset = self.site.theme_assets.where(local_path: sanitized_path).first
      timestamp = self.updated_at.to_i
      "#{path.first}#{asset.source.url}?#{timestamp}#{path.last}"
    else
      path
    end
  end
end

#performing_plain_text?Boolean

Returns:



42
43
44
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 42

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

#plain_textObject



33
34
35
36
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 33

def plain_text
  # only for ruby >= 1.9.x. Forget about ruby 1.8
  @plain_text ||= (self.source.read.force_encoding('UTF-8') rescue nil)
end

#plain_text_nameObject



21
22
23
24
25
26
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 21

def plain_text_name
  if not @plain_text_name_changed
    @plain_text_name ||= self.safe_source_filename
  end
  @plain_text_name.gsub(/(\.[a-z0-9A-Z]+)$/, '') rescue nil
end

#plain_text_name=(name) ⇒ Object



28
29
30
31
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 28

def plain_text_name=(name)
  @plain_text_name_changed = true
  @plain_text_name = name
end

#plain_text_typeObject



38
39
40
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 38

def plain_text_type
  @plain_text_type || (stylesheet_or_javascript? ? self.content_type : nil)
end

#store_plain_textObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/locomotive/concerns/theme_asset/plain_text.rb', line 46

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