2011-08-08 01:30:44 +00:00
|
|
|
class Theme < Stylesheet
|
|
|
|
embedded_in :design
|
2011-08-23 18:06:27 +00:00
|
|
|
field :name
|
2011-09-01 11:08:45 +00:00
|
|
|
mount_uploader :file_orig, AssetUploader
|
2011-08-23 18:06:27 +00:00
|
|
|
|
|
|
|
before_save :set_name
|
|
|
|
|
2011-09-01 11:08:45 +00:00
|
|
|
def parse_urls
|
2011-09-01 11:14:34 +00:00
|
|
|
orig_content = content = self.file.read.force_encoding("UTF-8")
|
2011-09-01 11:08:45 +00:00
|
|
|
self.remove_file!
|
|
|
|
self.remove_file_orig!
|
|
|
|
names = []
|
|
|
|
images = self.design.images
|
2011-09-02 11:19:34 +00:00
|
|
|
content.scan(/(?<=url)(.*?)(?=\))/){
|
|
|
|
css_name = $1.gsub(' ','').gsub('(','')
|
|
|
|
name = File.basename(css_name).gsub(/[\\\"]/, '')
|
2011-09-01 11:08:45 +00:00
|
|
|
file_name = images.detect{ |i| i.file_filename.eql?(name) }.file_url rescue nil
|
|
|
|
names << [css_name, file_name]
|
|
|
|
}
|
|
|
|
names.each do |name|
|
|
|
|
content.gsub!(name[0], name[1])
|
|
|
|
end
|
|
|
|
Dir.mktmpdir('f_path') { |dir|
|
|
|
|
orig_file_name = self.file_filename
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-08-23 18:06:27 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def set_name
|
2011-08-25 00:13:38 +00:00
|
|
|
self.name = File.basename(self.file_filename,".css")
|
2011-08-23 18:06:27 +00:00
|
|
|
end
|
|
|
|
|
2011-08-08 01:30:44 +00:00
|
|
|
end
|