Modifications in the Design code: the uploader works but the edit is broken

This commit is contained in:
Christophe Vilayphiou 2012-01-26 12:05:25 +08:00
parent 783eae4597
commit 46db173e0b
6 changed files with 75 additions and 122 deletions

View File

@ -103,7 +103,6 @@ class Admin::DesignsController < ApplicationController
temp_file.write (zip_file.read entry ).force_encoding('UTF-8') temp_file.write (zip_file.read entry ).force_encoding('UTF-8')
default_css = design.build_default_css default_css = design.build_default_css
default_css.file = temp_file default_css.file = temp_file
default_css.file_identifier = filename
default_css.to_save = true 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)
@ -111,7 +110,6 @@ class Admin::DesignsController < ApplicationController
temp_file.write (zip_file.read entry ).force_encoding('UTF-8') temp_file.write (zip_file.read entry ).force_encoding('UTF-8')
reset_css = design.build_reset_css reset_css = design.build_reset_css
reset_css.file = temp_file reset_css.file = temp_file
reset_css.file_identifier = filename
reset_css.to_save = true 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)
@ -135,7 +133,6 @@ class Admin::DesignsController < ApplicationController
temp_file.write (zip_file.read entry).force_encoding('UTF-8') temp_file.write (zip_file.read entry).force_encoding('UTF-8')
build_and_store = eval("design.#{type}").build build_and_store = eval("design.#{type}").build
build_and_store.file = temp_file build_and_store.file = temp_file
build_and_store.file_identifier = filename
build_and_store.to_save = true build_and_store.to_save = true
end end
end end

View File

@ -9,61 +9,36 @@ class Design
has_many :pages has_many :pages
embeds_one :layout embeds_one :layout, :cascade_callbacks => true
embeds_one :default_css, :class_name => "Stylesheet" embeds_one :default_css, :class_name => "Stylesheet", :cascade_callbacks => true
embeds_one :reset_css, :class_name => "Stylesheet" embeds_one :reset_css, :class_name => "Stylesheet", :cascade_callbacks => true
embeds_many :stylesheets embeds_many :themes, :cascade_callbacks => true
embeds_many :themes embeds_many :javascripts, :cascade_callbacks => true
embeds_many :javascripts embeds_many :images, :cascade_callbacks => true
embeds_many :images embeds_many :custom_images, :class_name => 'Image', :cascade_callbacks => true
embeds_many :custom_images, :class_name => 'Image', as: :design_image
validates_presence_of :title validates_presence_of :title
validates_presence_of :author validates_presence_of :author
after_save :procs_embedded_objects after_save :parse_css_for_images
# after_destroy :del_embedded_objects
# validate do
# errors.add('atttribute1', 'error for 1') if files_with_duplicate?
# errors.add('atttribute2', 'error for 1') if files_with_noname?
# end
def files_with_duplicate?
[self.javascripts, self.images,self.themes].each do |objects_hash|
ary = objects_hash.collect{ |k| k.file_identifier}
ary.compact!
# debugger
return true if(ary.count!=ary.uniq.count)
end
false
end
def files_with_noname?
[self.javascripts, self.images,self.themes].each do |objects_hash|
objects_hash.each{ |k|
return true if(k.file_identifier.nil? && !k.to_save?)
}
end
false
end
def new_files=(*attrs) def new_files=(*attrs)
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
self.files=([items_ary, key]) self.files=([items_ary, key])
end end
end end
def javascripts=(*attrs) # def javascripts=(*attrs)
self.files = (attrs << 'javascripts') # self.files = (attrs << 'javascripts')
end # end
#
def themes=(*attrs) # def themes=(*attrs)
self.files = (attrs << 'themes') # self.files = (attrs << 'themes')
end # end
#
def images=(*attrs) # def images=(*attrs)
self.files = (attrs << 'images') # self.files = (attrs << 'images')
end # end
# Update or create the attribute records # Update or create the attribute records
def files=(attrs) def files=(attrs)
@ -89,52 +64,34 @@ class Design
protected protected
def del_embedded_objects def parse_css_for_images
[[self.layout],self.themes, self.javascripts, self.images].each do |objects| self.images.each do |image|
objects.each do |object| image.save
object.destroy
end
end
end
def procs_embedded_objects
[self.javascripts, self.images].each do |objects|
objects.each do |object|
process_object(object)
end
end end
if self.layout.to_save if (self.default_css && self.default_css.changed)
self.layout.to_save = false self.default_css.parse_urls
self.layout.save attrs = self.default_css.attributes
file = self.default_css.file
file_orig = self.default_css.file_orig
self.default_css.destroy
d = self.build_default_css(attrs)
d.file = file
d.file_orig = file_orig
d.save
end end
[[self.default_css], [self.reset_css], self.themes].each do |objects| self.themes.each do |theme|
objects.each do |object| if theme.changed
if object theme.parse_urls
to_parse_url = object.to_save ? true : false attrs = theme.attributes
process_object(object) file = theme.file
object.parse_urls if to_parse_url file_orig = theme.file_orig
end theme.destroy
t = self.themes.build(attrs)
t.file = file
t.file_orig = file_orig
t.save
end end
end end
self.valid?
end
def process_object(object)
if object.to_save
if object.file_identifier.nil?
new_object = self.send(object._type.downcase.pluralize).build(object.attributes)
new_object.file = object.file
object.destroy
new_object.to_save = false
new_object.save
else
object.to_save = false
object.save
end
end
if object.to_destroy
object.destroy
end
end end
end end

View File

@ -4,8 +4,8 @@ class Stylesheet < DesignFile
def parse_urls def parse_urls
orig_content = content = self.file.read.force_encoding("UTF-8") orig_content = content = self.file.read.force_encoding("UTF-8")
self.remove_file! # self.remove_file!
self.remove_file_orig! # self.remove_file_orig!
names = [] names = []
images = self.design.images images = self.design.images
content.scan(/(?<=url)(.*?)(?=\))/){ content.scan(/(?<=url)(.*?)(?=\))/){
@ -27,9 +27,7 @@ class Stylesheet < DesignFile
temp_file = File.new(dir + '/' + orig_file_name, 'w+') temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write content.force_encoding("UTF-8") temp_file.write content.force_encoding("UTF-8")
self.file = temp_file self.file = temp_file
}
self.save
}
end end
end end

View File

@ -1,24 +1,14 @@
<%= f.label "field_name", t('admin.' + field_name) %> <%= f.fields_for field_name, design_file do |f| %>
<ul> <li class="<%= classes %>" <%="path='#{design_file.file.url}' filename='#{design_file.file_identifier}'" %> >
<% object.send(field_name).each do |t| %> <%= design_file.file_identifier %>
<li class="<%= classes %>" <%="path='#{t.file.url}' filename='#{t.file_identifier}'" %> > <% if classes.include?('r_destroy') %>
<%= t.file_identifier %> <%= f.hidden_field :id %>
<% if classes.include?('r_destroy') %> <%= f.hidden_field :file %>
<%= fields_for "design[" + field_name + "][]", t, :index => nil do |f| %> <%= f.hidden_field :to_destroy %>
<%= f.hidden_field :id %> <a class="remove_mark" href="#"><%= t('Delete') %></a>
<%= f.hidden_field :file %> <% end %>
<%= f.hidden_field :to_destroy %> <% if classes.include?('r_edit') %>
<% end %> <a class="edit_mark" href="#"><%= t('Edit') %></a>
<a class="remove_mark" href="#"><%= t('Delete') %></a> <% end %>
<% end %> </li>
<% if classes.include?('r_edit') %>
<a class="edit_mark" href="#"><%= t('Edit') %></a>
<% end %>
</li>
<% end %>
</ul>
<p class="new_file">
<%= fields_for "design[new_files][" + field_name + "][]", object.send(field_name).build, :index => nil do |f| %>
<%= f.file_field :file, :class => "multi_files" %>
</p>
<% end %> <% end %>

View File

@ -24,7 +24,7 @@
<% if @design.layout.blank? %> <% if @design.layout.blank? %>
<%= fields_for 'design[layout]' do |f| %> <%= fields_for 'design[layout]' do |f| %>
<%= f.file_field :file %> <%= f.file_field :file %>
<%= f.hidden_field :to_save, :value => true %> <%#= f.hidden_field :to_save, :value => true %>
<% end %> <% end %>
<% else %> <% else %>
<%= File.basename (@design.layout.file.url) %> <%= File.basename (@design.layout.file.url) %>
@ -35,20 +35,26 @@
<% if @design.default_css.blank? %> <% if @design.default_css.blank? %>
<%= fields_for 'design[default_css]' do |f| %> <%= fields_for 'design[default_css]' do |f| %>
<%= f.file_field :file %> <%= f.file_field :file %>
<%= f.hidden_field :to_save, :value => true %> <%#= f.hidden_field :to_save, :value => true %>
<% end %> <% end %>
<% else %> <% else %>
<%= File.basename (@design.default_css.file.url) rescue "" %> <%= File.basename (@design.default_css.file.url) rescue "" %>
<% end %> <% end %>
</p> </p>
<p> <p>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "themes", :f => f, :classes => "r_destroy, r_edit" } %> <%= f.label :themes, t('admin.themes') %>
<ul>
<% @design.themes.each do |theme| %>
<%= render :partial => 'design_file', :object => theme, :locals => { :field_name => "themes", :f => f, :classes => "r_destroy, r_edit" } %>
<% end %>
</ul>
<%= render :partial => 'new_design_file', :object => @design.themes.build, :locals => { :field_name => "themes", :f => f, :classes => "r_destroy" } %>
</p> </p>
<p> <p>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "javascripts", :f => f, :classes => "r_destroy, r_edit" } %> <%#= render :partial => 'design_file', :locals => { :object => @design, :field_name => "javascripts", :f => f, :classes => "r_destroy, r_edit" } %>
</p> </p>
<p> <p>
<%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "images", :f => f, :classes => "r_destroy, r_snapshot" } %> <%#= render :partial => 'design_file', :locals => { :object => @design, :field_name => "images", :f => f, :classes => "r_destroy, r_snapshot" } %>
</p> </p>

View File

@ -0,0 +1,5 @@
<%= f.fields_for field_name, new_design_file do |f| %>
<p class="new_file">
<%= f.file_field :file, :class => "multi_files" %>
</p>
<% end %>