engine/app/models/theme_asset.rb

135 lines
3.6 KiB
Ruby
Raw Normal View History

2010-05-11 21:38:52 +00:00
class ThemeAsset
include Locomotive::Mongoid::Document
## Extensions ##
include Models::Extensions::Asset::Vignette
2010-05-11 21:38:52 +00:00
## fields ##
field :slug
field :content_type
2010-05-11 21:38:52 +00:00
field :width, :type => Integer
field :height, :type => Integer
field :size, :type => Integer
field :plain_text
2010-05-11 21:38:52 +00:00
mount_uploader :source, ThemeAssetUploader
2010-05-11 21:38:52 +00:00
## associations ##
referenced_in :site
2010-09-15 16:09:07 +00:00
## indexes ##
index :site_id
index [[:content_type, Mongo::ASCENDING], [:slug, Mongo::ASCENDING], [:site_id, Mongo::ASCENDING]]
2010-05-11 21:38:52 +00:00
## callbacks ##
before_validation :sanitize_slug
before_validation :store_plain_text
2010-09-23 23:00:13 +00:00
# before_validation :escape_shortcut_urls
before_save :set_slug
2010-05-11 21:38:52 +00:00
## validations ##
validate :extname_can_not_be_changed
2010-05-12 00:16:39 +00:00
validates_presence_of :site, :source
2010-05-11 21:38:52 +00:00
validates_presence_of :slug, :if => Proc.new { |a| a.new_record? && a.performing_plain_text? }
validates_uniqueness_of :slug, :scope => [:site_id, :content_type]
2010-05-11 21:38:52 +00:00
validates_integrity_of :source
2010-05-11 21:38:52 +00:00
## accessors ##
attr_accessor :performing_plain_text
2010-05-11 21:38:52 +00:00
## methods ##
2010-07-17 20:51:52 +00:00
%w{movie image stylesheet javascript font}.each do |type|
2010-05-11 21:38:52 +00:00
define_method("#{type}?") do
self.content_type == type
end
2010-05-11 21:38:52 +00:00
end
2010-07-17 20:51:52 +00:00
def stylesheet_or_javascript?
self.stylesheet? || self.javascript?
end
2010-05-11 21:38:52 +00:00
def plain_text
if self.stylesheet_or_javascript?
self.plain_text = self.source.read if read_attribute(:plain_text).blank?
read_attribute(:plain_text)
2010-05-11 21:38:52 +00:00
else
nil
end
2010-05-11 21:38:52 +00:00
end
2010-05-11 21:38:52 +00:00
def plain_text=(source)
self.performing_plain_text = true if self.performing_plain_text.nil?
write_attribute(:plain_text, source)
2010-05-11 21:38:52 +00:00
end
2010-05-11 21:38:52 +00:00
def performing_plain_text?
2010-07-17 22:01:00 +00:00
return true if !self.new_record? && self.stylesheet_or_javascript? && self.errors.empty?
2010-05-11 21:38:52 +00:00
!(self.performing_plain_text.blank? || self.performing_plain_text == 'false' || self.performing_plain_text == false)
end
2010-05-11 21:38:52 +00:00
def store_plain_text
2010-09-23 23:00:13 +00:00
return if !self.stylesheet_or_javascript? || self.plain_text.blank?
2010-09-23 23:00:13 +00:00
sanitized_source = self.escape_shortcut_urls(self.plain_text)
2010-09-23 23:00:13 +00:00
if self.source.nil?
self.source = CarrierWave::SanitizedFile.new({
:tempfile => StringIO.new(sanitized_source),
:filename => "#{self.slug}.#{self.stylesheet? ? 'css' : 'js'}"
})
else
self.source.file.instance_variable_set(:@file, StringIO.new(sanitized_source))
end
2010-05-11 21:38:52 +00:00
end
2010-09-23 23:00:13 +00:00
def shortcut_url # ex: /stylesheets/application.css is a shortcut for a theme asset (content_type => stylesheet, slug => 'application')
File.join(self.content_type.pluralize, "#{self.slug}#{File.extname(self.source_filename)}")
rescue
''
end
def to_liquid
{ :url => self.source.url }.merge(self.attributes)
end
2010-05-11 21:38:52 +00:00
protected
2010-09-23 23:00:13 +00:00
def escape_shortcut_urls(text) # replace /<content_type>/<slug> occurences by the real amazon S3 url or local files
return if text.blank?
text.gsub(/(\/(stylesheets|javascripts|images)\/([a-z_\-0-9]+)\.[a-z]{2,3})/) do |url|
content_type, slug = url.split('/')[1..-1]
content_type = content_type.singularize
slug = slug.split('.').first
if asset = self.site.theme_assets.where(:content_type => content_type, :slug => slug).first
asset.source.url
else
url
end
end
end
2010-05-11 21:38:52 +00:00
def sanitize_slug
2010-09-23 23:00:13 +00:00
self.slug.parameterize! if self.slug.present?
2010-05-11 21:38:52 +00:00
end
2010-05-11 21:38:52 +00:00
def set_slug
if self.slug.blank?
self.slug = File.basename(self.source_filename, File.extname(self.source_filename))
self.sanitize_slug
end
end
2010-05-11 21:38:52 +00:00
def extname_can_not_be_changed
return if self.new_record?
2010-05-11 21:38:52 +00:00
if File.extname(self.source.file.original_filename) != File.extname(self.source_filename)
self.errors.add(:source, :extname_changed)
end
end
end