From 6f9363ef5432d8e863fbe650b929ec5d8c51b941 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Fri, 6 Apr 2012 17:31:22 +0800 Subject: [PATCH] Fix bugs in upload design and missing menu --- app/models/design/design.rb | 7 +++---- app/models/design/stylesheet.rb | 1 + lib/parsers/parser_common.rb | 4 ++-- lib/parsers/parser_layout.rb | 19 ++++++++----------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/app/models/design/design.rb b/app/models/design/design.rb index 7fbacedc..2a25dd11 100644 --- a/app/models/design/design.rb +++ b/app/models/design/design.rb @@ -1,6 +1,7 @@ class Design include Mongoid::Document include Mongoid::Timestamps + include ParserLayout field :title field :author @@ -14,7 +15,7 @@ class Design embeds_one :reset_css, :class_name => "Stylesheet", :cascade_callbacks => true embeds_many :themes, :cascade_callbacks => true embeds_many :javascripts, :cascade_callbacks => true - embeds_many :images, :cascade_callbacks => true + embeds_many :images, :as => :design_image, :cascade_callbacks => true # embeds_many :custom_images, :class_name => 'Image', :cascade_callbacks => true validates_presence_of :title @@ -65,9 +66,6 @@ class Design protected def parse_css_for_images - self.images.each do |image| - image.save - end if (self.default_css && self.default_css.changed) self.default_css.parse_urls end @@ -76,6 +74,7 @@ class Design theme.parse_urls end end + parse_body_for_images(self) end end diff --git a/app/models/design/stylesheet.rb b/app/models/design/stylesheet.rb index 4b908122..bb2ec85e 100644 --- a/app/models/design/stylesheet.rb +++ b/app/models/design/stylesheet.rb @@ -27,6 +27,7 @@ class Stylesheet < DesignFile temp_file = File.new(dir + '/' + orig_file_name, 'w+') temp_file.write content.force_encoding("UTF-8") self.file = temp_file + self.save } end diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb index bd05a4e8..93ea2608 100644 --- a/lib/parsers/parser_common.rb +++ b/lib/parsers/parser_common.rb @@ -2,7 +2,7 @@ module ParserCommon def menu_level(page, current, menu, edit = false) res = '' - if current <= menu.levels + if menu.levels > 0 && current <= menu.levels if current != 0 res << "
" item = rand(100000) @@ -84,7 +84,7 @@ module ParserCommon body.css('.page_image').each do |page_image| # image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } # image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image - image = page.design.images.detect{|image| image.name.eql?(page_image['name']) } unless image + image = page.design.images.detect{|image| image.name.eql?(File.basename(page_image['src'])) } unless image if image res = " content['name']) end - body.css('.page_image').each do |image| - image = layout.design.images.detect{ |i| i.file_identifier.eql?(parse_html_image(image.to_html)) } - image.update_attributes(:name => image['name'], :html_id => image['id'], :html_class => image['class']) if image - end - body.css('.page_menu').each do |menu| layout.build_menu(:levels => 0, :values => {}) unless layout.menu layout.menu.levels = i = menu['level'].to_i @@ -22,12 +17,14 @@ module ParserLayout end - def parse_html_image(html) - html.scan(/(?<=\)/){ - $1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){ - return File.basename($1).gsub(/[\\\"]/, '') - } - } + def parse_body_for_images(design) + body = Nokogiri::HTML(design.layout.body) + + body.css('.page_image').each do |page_image| + image = design.images.where( file: File.basename(page_image['src']))[0] + image.update_attributes(:name => File.basename(page_image['src']), :html_id => page_image['id'], :html_class => page_image['class']) if image + end + end