Orbit/app/models/design/stylesheet.rb

46 lines
1.3 KiB
Ruby

class Stylesheet < DesignFile
# belongs_to :design
mount_uploader :file_orig, AssetUploader
def parse_urls
orig_content = content = self.file.read.force_encoding("UTF-8")
# self.remove_file!
# self.remove_file_orig!
names = {}
case self._type
when 'Theme'
images = self.design.images
when 'CssDefault'
images = self.css.images
end
content.scan(/(?<=url)(.*?)(?=\))/){
css_name = $1.gsub(' ','').gsub('(','')
unless names.has_key?(css_name)
name = File.basename(css_name).gsub(/[\\\"]/, '')
image = images.detect{ |i| i.file_identifier.eql?(name) } rescue nil
if image
image.update_attribute(:in_css, true)
file_name = image.file_url
names.merge!({css_name => file_name})
end
end
}
names.each_pair do |key, value|
content.gsub!(key, value)
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_identifier
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write orig_content.force_encoding("UTF-8")
self.file_orig = temp_file
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write content.force_encoding("UTF-8")
self.file = temp_file
self.save
}
end
end