This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/design/theme.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

class Theme < Stylesheet
embedded_in :design
field :name
mount_uploader :file_orig, AssetUploader
before_save :set_name
def parse_urls
orig_content = content = self.file.read
self.remove_file!
self.remove_file_orig!
names = []
images = self.design.images
content.scan(/(?<=url\()(.*?)(?=\))/){
css_name = $1
name = File.basename(css_name).gsub(/[\\\"]/, '')
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
protected
def set_name
self.name = File.basename(self.file_filename,".css")
end
end