2010-05-11 21:38:52 +00:00
|
|
|
class ThemeAsset
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2010-07-22 22:10:40 +00:00
|
|
|
|
|
|
|
## Extensions ##
|
2010-06-02 14:31:01 +00:00
|
|
|
include Models::Extensions::Asset::Vignette
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
## fields ##
|
2010-05-30 23:57:33 +00:00
|
|
|
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
|
2010-07-22 22:10:40 +00:00
|
|
|
field :plain_text
|
2010-05-11 21:38:52 +00:00
|
|
|
mount_uploader :source, ThemeAssetUploader
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
referenced_in :site
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
## callbacks ##
|
2010-07-20 10:15:53 +00:00
|
|
|
before_validation :sanitize_slug
|
|
|
|
before_validation :store_plain_text
|
2010-05-11 21:38:52 +00:00
|
|
|
before_save :set_slug
|
2010-07-22 22:10:40 +00:00
|
|
|
|
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? }
|
2010-05-30 23:57:33 +00:00
|
|
|
validates_uniqueness_of :slug, :scope => [:site_id, :content_type]
|
2010-05-11 21:38:52 +00:00
|
|
|
validates_integrity_of :source
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
## accessors ##
|
|
|
|
attr_accessor :performing_plain_text
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
## methods ##
|
2010-07-22 22:10:40 +00:00
|
|
|
|
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
|
2010-07-22 22:10:40 +00:00
|
|
|
end
|
2010-05-11 21:38:52 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-17 20:51:52 +00:00
|
|
|
def stylesheet_or_javascript?
|
|
|
|
self.stylesheet? || self.javascript?
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
def plain_text
|
2010-07-22 22:10:40 +00:00
|
|
|
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
|
2010-07-22 22:10:40 +00:00
|
|
|
end
|
2010-05-11 21:38:52 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
def plain_text=(source)
|
|
|
|
self.performing_plain_text = true if self.performing_plain_text.nil?
|
2010-07-22 22:10:40 +00:00
|
|
|
write_attribute(:plain_text, source)
|
2010-05-11 21:38:52 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
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-07-22 22:10:40 +00:00
|
|
|
|
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-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
def store_plain_text
|
|
|
|
return if self.plain_text.blank?
|
2010-07-22 22:10:40 +00:00
|
|
|
|
|
|
|
# replace /theme/<content_type>/<slug> occurences by the real amazon S3 url or local files
|
|
|
|
sanitized_source = self.plain_text.gsub(/(\/theme\/([a-z]+)\/([a-z_\-0-9]+)\.[a-z]{2,3})/) do |url|
|
|
|
|
content_type, slug = url.split('/')[2..-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
|
|
|
|
|
|
|
|
self.source = CarrierWave::SanitizedFile.new({
|
|
|
|
:tempfile => StringIO.new(sanitized_source),
|
2010-06-12 21:47:14 +00:00
|
|
|
:filename => "#{self.slug}.#{self.stylesheet? ? 'css' : 'js'}"
|
2010-05-11 21:38:52 +00:00
|
|
|
})
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
|
|
|
def shortcut_url # ex: /theme/stylesheets/application.css is a shortcut for a theme asset (content_type => stylesheet, slug => 'application')
|
|
|
|
File.join('/theme', self.content_type.pluralize, "#{self.slug}#{File.extname(self.source_filename)}")
|
|
|
|
rescue
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def to_liquid
|
|
|
|
{ :url => self.source.url }.merge(self.attributes)
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
protected
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
def sanitize_slug
|
|
|
|
self.slug.slugify!(:underscore => true) if self.slug.present?
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
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-07-22 22:10:40 +00:00
|
|
|
|
2010-05-11 21:38:52 +00:00
|
|
|
def extname_can_not_be_changed
|
|
|
|
return if self.new_record?
|
2010-07-22 22:10:40 +00:00
|
|
|
|
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
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|