2011-08-08 01:30:44 +00:00
|
|
|
class Layout < DesignFile
|
2011-08-10 09:28:10 +00:00
|
|
|
include Parser
|
2011-09-01 11:08:45 +00:00
|
|
|
|
|
|
|
mount_uploader :file_orig, AssetUploader
|
|
|
|
|
|
|
|
attr_reader :content
|
|
|
|
|
2011-09-01 01:12:41 +00:00
|
|
|
embeds_one :menu
|
2011-08-08 01:30:44 +00:00
|
|
|
embedded_in :design
|
2011-08-18 08:27:04 +00:00
|
|
|
embeds_many :layout_parts
|
2011-09-01 11:08:45 +00:00
|
|
|
|
|
|
|
after_save :parse_layout
|
2011-05-18 02:00:00 +00:00
|
|
|
|
2011-08-08 04:29:32 +00:00
|
|
|
def content
|
|
|
|
self.file.read
|
|
|
|
end
|
2011-08-10 09:28:10 +00:00
|
|
|
|
2011-05-18 02:00:00 +00:00
|
|
|
def self.exist_one?
|
|
|
|
Layout.count > 0
|
|
|
|
end
|
2011-08-10 09:28:10 +00:00
|
|
|
|
|
|
|
def parse_layout
|
|
|
|
parse_layout_layout_part(self)
|
|
|
|
end
|
2011-09-01 11:08:45 +00:00
|
|
|
|
|
|
|
def parse_urls
|
|
|
|
orig_content = content = self.file.read.force_encoding("UTF-8")
|
|
|
|
self.remove_file!
|
|
|
|
self.remove_file_orig!
|
|
|
|
names = []
|
|
|
|
images = self.design.images
|
2011-09-02 11:19:34 +00:00
|
|
|
html_name = nil
|
|
|
|
content.scan(/(?<=\<img)(.*?)(?=\/\>)/){
|
|
|
|
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
|
|
|
|
html_name = $1
|
|
|
|
}
|
|
|
|
name = File.basename(html_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 << [html_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
|
|
|
|
|
|
|
|
Layout.without_callback(:save) do
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2011-03-08 09:25:46 +00:00
|
|
|
end
|