From 46db173e0b121984c76aa23fc555980dfc33a3a7 Mon Sep 17 00:00:00 2001
From: Christophe Vilayphiou
Date: Thu, 26 Jan 2012 12:05:25 +0800
Subject: [PATCH] Modifications in the Design code: the uploader works but the
edit is broken
---
app/controllers/admin/designs_controller.rb | 3 -
app/models/design/design.rb | 129 ++++++------------
app/models/design/stylesheet.rb | 8 +-
app/views/admin/designs/_design_file.html.erb | 36 ++---
app/views/admin/designs/_form.html.erb | 16 ++-
.../admin/designs/_new_design_file.html.erb | 5 +
6 files changed, 75 insertions(+), 122 deletions(-)
create mode 100644 app/views/admin/designs/_new_design_file.html.erb
diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb
index 68fbb243..d4f54328 100644
--- a/app/controllers/admin/designs_controller.rb
+++ b/app/controllers/admin/designs_controller.rb
@@ -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
diff --git a/app/models/design/design.rb b/app/models/design/design.rb
index d499b2d8..f4dcb18b 100644
--- a/app/models/design/design.rb
+++ b/app/models/design/design.rb
@@ -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
diff --git a/app/models/design/stylesheet.rb b/app/models/design/stylesheet.rb
index 13fb154d..4b908122 100644
--- a/app/models/design/stylesheet.rb
+++ b/app/models/design/stylesheet.rb
@@ -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,9 +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
end
diff --git a/app/views/admin/designs/_design_file.html.erb b/app/views/admin/designs/_design_file.html.erb
index 9e494552..8b068d6c 100644
--- a/app/views/admin/designs/_design_file.html.erb
+++ b/app/views/admin/designs/_design_file.html.erb
@@ -1,24 +1,14 @@
-<%= f.label "field_name", t('admin.' + field_name) %>
-
- <% object.send(field_name).each do |t| %>
- - >
- <%= 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 %>
- <%= t('Delete') %>
- <% end %>
- <% if classes.include?('r_edit') %>
- <%= t('Edit') %>
- <% end %>
-
- <% end %>
-
-
-<%= fields_for "design[new_files][" + field_name + "][]", object.send(field_name).build, :index => nil do |f| %>
- <%= f.file_field :file, :class => "multi_files" %>
-
+<%= f.fields_for field_name, design_file do |f| %>
+ >
+ <%= design_file.file_identifier %>
+ <% if classes.include?('r_destroy') %>
+ <%= f.hidden_field :id %>
+ <%= f.hidden_field :file %>
+ <%= f.hidden_field :to_destroy %>
+ <%= t('Delete') %>
+ <% end %>
+ <% if classes.include?('r_edit') %>
+ <%= t('Edit') %>
+ <% end %>
+
<% end %>
\ No newline at end of file
diff --git a/app/views/admin/designs/_form.html.erb b/app/views/admin/designs/_form.html.erb
index fd579e5e..d787a58d 100644
--- a/app/views/admin/designs/_form.html.erb
+++ b/app/views/admin/designs/_form.html.erb
@@ -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 %>
- <%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "themes", :f => f, :classes => "r_destroy, r_edit" } %>
+ <%= f.label :themes, t('admin.themes') %>
+
+ <% @design.themes.each do |theme| %>
+ <%= render :partial => 'design_file', :object => theme, :locals => { :field_name => "themes", :f => f, :classes => "r_destroy, r_edit" } %>
+ <% end %>
+
+ <%= render :partial => 'new_design_file', :object => @design.themes.build, :locals => { :field_name => "themes", :f => f, :classes => "r_destroy" } %>
- <%= 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" } %>
- <%= 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" } %>
diff --git a/app/views/admin/designs/_new_design_file.html.erb b/app/views/admin/designs/_new_design_file.html.erb
new file mode 100644
index 00000000..bb3cc880
--- /dev/null
+++ b/app/views/admin/designs/_new_design_file.html.erb
@@ -0,0 +1,5 @@
+<%= f.fields_for field_name, new_design_file do |f| %>
+
+ <%= f.file_field :file, :class => "multi_files" %>
+
+<% end %>
\ No newline at end of file