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')
default_css = design.build_default_css
default_css.file = temp_file
default_css.file_identifier = filename
default_css.to_save = true
when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css
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')
reset_css = design.build_reset_css
reset_css.file = temp_file
reset_css.file_identifier = filename
reset_css.to_save = true
when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html
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')
build_and_store = eval("design.#{type}").build
build_and_store.file = temp_file
build_and_store.file_identifier = filename
build_and_store.to_save = true
end
end

View File

@ -9,61 +9,36 @@ class Design
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
embeds_many :images
embeds_many :custom_images, :class_name => 'Image', as: :design_image
embeds_one :layout, :cascade_callbacks => true
embeds_one :default_css, :class_name => "Stylesheet", :cascade_callbacks => true
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 :custom_images, :class_name => 'Image', :cascade_callbacks => true
validates_presence_of :title
validates_presence_of :author
after_save :procs_embedded_objects
# after_destroy :del_embedded_objects
after_save :parse_css_for_images
# 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)
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
self.files=([items_ary, key])
end
end
def javascripts=(*attrs)
self.files = (attrs << 'javascripts')
end
def themes=(*attrs)
self.files = (attrs << 'themes')
end
def images=(*attrs)
self.files = (attrs << 'images')
end
# def javascripts=(*attrs)
# self.files = (attrs << 'javascripts')
# end
#
# def themes=(*attrs)
# self.files = (attrs << 'themes')
# end
#
# def images=(*attrs)
# self.files = (attrs << 'images')
# end
# Update or create the attribute records
def files=(attrs)
@ -89,52 +64,34 @@ class Design
protected
def del_embedded_objects
[[self.layout],self.themes, self.javascripts, self.images].each do |objects|
objects.each do |object|
object.destroy
end
end
end
def procs_embedded_objects
[self.javascripts, self.images].each do |objects|
objects.each do |object|
process_object(object)
end
def parse_css_for_images
self.images.each do |image|
image.save
end
if self.layout.to_save
self.layout.to_save = false
self.layout.save
if (self.default_css && self.default_css.changed)
self.default_css.parse_urls
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
[[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
self.themes.each do |theme|
if theme.changed
theme.parse_urls
attrs = theme.attributes
file = theme.file
file_orig = theme.file_orig
theme.destroy
t = self.themes.build(attrs)
t.file = file
t.file_orig = file_orig
t.save
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

View File

@ -4,8 +4,8 @@ class Stylesheet < DesignFile
def parse_urls
orig_content = content = self.file.read.force_encoding("UTF-8")
self.remove_file!
self.remove_file_orig!
# self.remove_file!
# self.remove_file_orig!
names = []
images = self.design.images
content.scan(/(?<=url)(.*?)(?=\))/){
@ -27,8 +27,6 @@ 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

View File

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

View File

@ -24,7 +24,7 @@
<% if @design.layout.blank? %>
<%= fields_for 'design[layout]' do |f| %>
<%= f.file_field :file %>
<%= f.hidden_field :to_save, :value => true %>
<%#= f.hidden_field :to_save, :value => true %>
<% end %>
<% else %>
<%= File.basename (@design.layout.file.url) %>
@ -35,20 +35,26 @@
<% if @design.default_css.blank? %>
<%= fields_for 'design[default_css]' do |f| %>
<%= f.file_field :file %>
<%= f.hidden_field :to_save, :value => true %>
<%#= f.hidden_field :to_save, :value => true %>
<% end %>
<% else %>
<%= File.basename (@design.default_css.file.url) rescue "" %>
<% end %>
</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>
<%= 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>
<%= 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>

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 %>