Modification in Design structure_css -> default_css

This commit is contained in:
chris2tof 2011-09-13 11:57:20 +08:00
parent 07ccacb7d7
commit 4db88282a1
8 changed files with 79 additions and 69 deletions

View File

@ -101,12 +101,18 @@ class Admin::DesignsController < ApplicationController
filename = File.basename(entry.to_s) filename = File.basename(entry.to_s)
temp_file = File.new(dir + '/' + filename, 'w+') temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.write (zip_file.read entry ).force_encoding('UTF-8') 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 when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css
filename = File.basename(entry.to_s) filename = File.basename(entry.to_s)
temp_file = File.new(dir + '/' + filename, 'w+') temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.write (zip_file.read entry ).force_encoding('UTF-8') 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 when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html
filename = File.basename(entry.to_s) filename = File.basename(entry.to_s)
temp_file = File.new(dir + '/' + filename, 'w+') temp_file = File.new(dir + '/' + filename, 'w+')

View File

@ -58,24 +58,13 @@ class Admin::PurchasesController < ApplicationController
design = Design.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json")) design = Design.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json"))
Dir.mktmpdir('f_path') { |dir| Dir.mktmpdir('f_path') { |dir|
title = design.layout.file_filename build_file(orig_zip, zip_name, dir, design.layout) if design.layout
temp = File.new(dir + '/' + title, 'w+') build_file(orig_zip, zip_name, dir, design.default_css) if design.default_css
temp.write orig_zip.read(zip_name + '/' + title) build_file(orig_zip, zip_name, dir, design.reset_css) if design.reset_css
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
['themes', 'javascripts', 'images'].each do |type| ['themes', 'javascripts', 'images'].each do |type|
design.send(type).each do |object| design.send(type).each do |object|
title = object.file_filename build_file(orig_zip, zip_name, dir, object, type)
temp = File.new(dir + '/' + title, 'w+')
temp.write orig_zip.read(zip_name + '/' + type + '/' + title)
object.file = temp
object.to_save = true
end end
end end
} }
@ -86,5 +75,13 @@ class Admin::PurchasesController < ApplicationController
end end
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 end

View File

@ -7,12 +7,11 @@ class Design
field :intro field :intro
field :version field :version
mount_uploader :default_css, AssetUploader
mount_uploader :reset_css, AssetUploader
has_many :pages has_many :pages
embeds_one :layout embeds_one :layout
embeds_one :default_css, :class_name => "Stylesheet"
embeds_one :reset_css, :class_name => "Stylesheet"
embeds_many :stylesheets embeds_many :stylesheets
embeds_many :themes embeds_many :themes
embeds_many :javascripts embeds_many :javascripts
@ -78,10 +77,14 @@ class Design
self.layout.save self.layout.save
self.layout.parse_urls self.layout.parse_urls
end end
self.themes.each do |theme| [[self.default_css], [self.reset_css], self.themes].each do |objects|
to_parse_url = theme.to_save ? true : false objects.each do |object|
process_object(theme) if object
theme.parse_urls if to_parse_url to_parse_url = object.to_save ? true : false
process_object(object)
object.parse_urls if to_parse_url
end
end
end end
end end

View File

@ -1,3 +1,35 @@
class Stylesheet < DesignFile class Stylesheet < DesignFile
embedded_in :design 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 end

View File

@ -1,40 +1,9 @@
class Theme < Stylesheet class Theme < Stylesheet
embedded_in :design embedded_in :design
field :name field :name
mount_uploader :file_orig, AssetUploader
before_save :set_name 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 protected
def set_name def set_name

View File

@ -30,9 +30,12 @@
<p> <p>
<%= f.label "default_css", t('admin.default_css') %> <%= f.label "default_css", t('admin.default_css') %>
<% if @design.default_css.blank? %> <% 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 %> <% else %>
<%= File.basename (@design.default_css.url) %> <%= File.basename (@design.default_css.file.url) %>
<% end %> <% end %>
</p> </p>
<p> <p>

View File

@ -65,8 +65,8 @@ module Parser
end end
c.define_tag 'stylesheets' do |tag| c.define_tag 'stylesheets' do |tag|
res = '' res = ''
res << "<link href='#{page.design.reset_css.url}' rel='stylesheet' type='text/css' />" res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />"
res << "<link href='#{page.design.default_css.url}' rel='stylesheet' type='text/css' /> " res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> "
theme = page.design.themes.detect{ |d| d.id == page.theme_id } theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res res
@ -175,8 +175,8 @@ module Parser
end end
c.define_tag 'stylesheets' do |tag| c.define_tag 'stylesheets' do |tag|
res = '' res = ''
res << "<link href='#{page.design.reset_css.url}' rel='stylesheet' type='text/css' />" res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />"
res << "<link href='#{page.design.default_css.url}' rel='stylesheet' type='text/css' />" res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' />"
theme = page.design.themes.detect{ |d| d.id == page.theme_id } theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res res

View File

@ -69,7 +69,7 @@ namespace :dev do
design = Design.new(:title => "Fraisier", :author => "Paul", :intro => "Strawberry cake") 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")) # 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.build_layout
design.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html") design.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html")
design.layout.save design.layout
theme.save theme
theme_1.save theme_1
# image.save # image.save
# js.save # js.save
@ -93,7 +93,7 @@ namespace :dev do
design_1 = Design.new(:title => "Bob", :author => "Me", :intro => "Moran") 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")) # 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.build_layout
design_1.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html") design_1.layout.file = File.open("#{Rails.root}/lib/fraisier/layout.html")
design_1.layout.save design_1.layout
theme.save theme
theme_1.save theme_1
# image.save # image.save
# js.save # js.save