From 4db88282a15bcef9cb947d863185c0f8f2c881d9 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Tue, 13 Sep 2011 11:57:20 +0800 Subject: [PATCH] Modification in Design structure_css -> default_css --- app/controllers/admin/designs_controller.rb | 10 ++++-- app/controllers/admin/purchases_controller.rb | 27 +++++++--------- app/models/design/design.rb | 17 ++++++---- app/models/design/stylesheet.rb | 32 +++++++++++++++++++ app/models/design/theme.rb | 31 ------------------ app/views/admin/designs/_form.html.erb | 7 ++-- lib/parser.rb | 8 ++--- lib/tasks/dev.rake | 16 +++++----- 8 files changed, 79 insertions(+), 69 deletions(-) diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb index c52d653b..5930d1bf 100644 --- a/app/controllers/admin/designs_controller.rb +++ b/app/controllers/admin/designs_controller.rb @@ -101,12 +101,18 @@ class Admin::DesignsController < ApplicationController filename = File.basename(entry.to_s) temp_file = File.new(dir + '/' + filename, 'w+') temp_file.write (zip_file.read entry ).force_encoding('UTF-8') - design.default_css = temp_file + default_css = design.build_default_css + default_css.file = temp_file + default_css.file_filename = filename + default_css.to_save = true when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css filename = File.basename(entry.to_s) temp_file = File.new(dir + '/' + filename, 'w+') temp_file.write (zip_file.read entry ).force_encoding('UTF-8') - design.reset_css = temp_file + reset_css = design.build_reset_css + reset_css.file = temp_file + reset_css.file_filename = filename + reset_css.to_save = true when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html filename = File.basename(entry.to_s) temp_file = File.new(dir + '/' + filename, 'w+') diff --git a/app/controllers/admin/purchases_controller.rb b/app/controllers/admin/purchases_controller.rb index 9b341054..d8ffa0e2 100644 --- a/app/controllers/admin/purchases_controller.rb +++ b/app/controllers/admin/purchases_controller.rb @@ -58,24 +58,13 @@ class Admin::PurchasesController < ApplicationController design = Design.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json")) Dir.mktmpdir('f_path') { |dir| - title = design.layout.file_filename - temp = File.new(dir + '/' + title, 'w+') - temp.write orig_zip.read(zip_name + '/' + title) - design.layout.file = temp - design.layout.to_save = true - - title = design.default_css_filename - temp = File.new(dir + '/' + title, 'w+') - temp.write orig_zip.read(zip_name + '/' + title) - design.default_css = temp + build_file(orig_zip, zip_name, dir, design.layout) if design.layout + build_file(orig_zip, zip_name, dir, design.default_css) if design.default_css + build_file(orig_zip, zip_name, dir, design.reset_css) if design.reset_css ['themes', 'javascripts', 'images'].each do |type| design.send(type).each do |object| - title = object.file_filename - temp = File.new(dir + '/' + title, 'w+') - temp.write orig_zip.read(zip_name + '/' + type + '/' + title) - object.file = temp - object.to_save = true + build_file(orig_zip, zip_name, dir, object, type) end end } @@ -86,5 +75,13 @@ class Admin::PurchasesController < ApplicationController end end + + def build_file(orig_zip, zip_name, dir, object, type = nil) + title = object.file_filename + temp = File.new(dir + '/' + title, 'w+') + temp.write orig_zip.read(zip_name + '/' + (type ? (type + '/') : '') + title) + object.file = temp + object.to_save = true + end end \ No newline at end of file diff --git a/app/models/design/design.rb b/app/models/design/design.rb index 4d695710..7d7fac73 100644 --- a/app/models/design/design.rb +++ b/app/models/design/design.rb @@ -7,12 +7,11 @@ class Design field :intro field :version - mount_uploader :default_css, AssetUploader - mount_uploader :reset_css, AssetUploader - has_many :pages embeds_one :layout + embeds_one :default_css, :class_name => "Stylesheet" + embeds_one :reset_css, :class_name => "Stylesheet" embeds_many :stylesheets embeds_many :themes embeds_many :javascripts @@ -78,10 +77,14 @@ class Design self.layout.save self.layout.parse_urls end - self.themes.each do |theme| - to_parse_url = theme.to_save ? true : false - process_object(theme) - theme.parse_urls if to_parse_url + [[self.default_css], [self.reset_css], self.themes].each do |objects| + objects.each do |object| + if object + to_parse_url = object.to_save ? true : false + process_object(object) + object.parse_urls if to_parse_url + end + end end end diff --git a/app/models/design/stylesheet.rb b/app/models/design/stylesheet.rb index 8469613c..f26f1558 100644 --- a/app/models/design/stylesheet.rb +++ b/app/models/design/stylesheet.rb @@ -1,3 +1,35 @@ class Stylesheet < DesignFile embedded_in :design + mount_uploader :file_orig, AssetUploader + + 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 + content.scan(/(?<=url)(.*?)(?=\))/){ + css_name = $1.gsub(' ','').gsub('(','') + 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]) if 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 + end diff --git a/app/models/design/theme.rb b/app/models/design/theme.rb index 756b7e12..e6974181 100644 --- a/app/models/design/theme.rb +++ b/app/models/design/theme.rb @@ -1,40 +1,9 @@ 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.force_encoding("UTF-8") - self.remove_file! - self.remove_file_orig! - names = [] - images = self.design.images - content.scan(/(?<=url)(.*?)(?=\))/){ - css_name = $1.gsub(' ','').gsub('(','') - 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 diff --git a/app/views/admin/designs/_form.html.erb b/app/views/admin/designs/_form.html.erb index 9a857f43..2711faa0 100644 --- a/app/views/admin/designs/_form.html.erb +++ b/app/views/admin/designs/_form.html.erb @@ -30,9 +30,12 @@

<%= f.label "default_css", t('admin.default_css') %> <% if @design.default_css.blank? %> - <%= f.file_field :default_css %> + <%= fields_for 'design[default_css]' do |f| %> + <%= f.file_field :file %> + <%= f.hidden_field :to_save, :value => true %> + <% end %> <% else %> - <%= File.basename (@design.default_css.url) %> + <%= File.basename (@design.default_css.file.url) %> <% end %>

diff --git a/lib/parser.rb b/lib/parser.rb index cbac20b6..3eddbdca 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -65,8 +65,8 @@ module Parser end c.define_tag 'stylesheets' do |tag| res = '' - res << "" - res << " " + res << "" + res << " " theme = page.design.themes.detect{ |d| d.id == page.theme_id } res << "" if theme res @@ -175,8 +175,8 @@ module Parser end c.define_tag 'stylesheets' do |tag| res = '' - res << "" - res << "" + res << "" + res << "" theme = page.design.themes.detect{ |d| d.id == page.theme_id } res << "" if theme res diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 633ae2b6..36b6952a 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -69,7 +69,7 @@ namespace :dev do design = Design.new(:title => "Fraisier", :author => "Paul", :intro => "Strawberry cake") - design.default_css = File.open("#{Rails.root}/lib/fraisier/default.css") + design.build_default_css(:file => File.open("#{Rails.root}/lib/fraisier/default.css")) # image = design.images.build(:file => File.open("#{Rails.root}/lib/fraisier/img/buttons.gif")) # @@ -81,9 +81,9 @@ namespace :dev do design.build_layout design.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html") - design.layout.save - theme.save - theme_1.save + design.layout + theme + theme_1 # image.save # js.save @@ -93,7 +93,7 @@ namespace :dev do design_1 = Design.new(:title => "Bob", :author => "Me", :intro => "Moran") - design_1.default_css = File.open("#{Rails.root}/lib/fraisier/default.css") + design_1.build_default_css(:file => File.open("#{Rails.root}/lib/fraisier/default.css")) # image = design.images.build(:file => File.open("#{Rails.root}/lib/fraisier/img/buttons.gif")) # @@ -105,9 +105,9 @@ namespace :dev do design_1.build_layout design_1.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html") - design_1.layout.save - theme.save - theme_1.save + design_1.layout + theme + theme_1 # image.save # js.save