Orbit/app/models/design/layout.rb

62 lines
1.4 KiB
Ruby

class Layout < DesignFile
include Parser
mount_uploader :file_orig, AssetUploader
attr_reader :content
embeds_one :menu
embedded_in :design
embeds_many :layout_parts
after_save :parse_layout
def content
self.file.read.force_encoding("UTF-8")
end
def self.exist_one?
Layout.count > 0
end
def parse_layout
debugger
parse_layout_contents(self)
end
def parse_urls
orig_content = content = self.content
self.remove_file!
self.remove_file_orig!
names = []
images = self.design.images
html_name = nil
content.scan(/(?<=\<img)(.*?)(?=\/\>)/){
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
html_name = $1
}
name = File.basename(html_name).gsub(/[\\\"]/, '')
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, :after, :parse_layout) do
self.save
end
}
end
end