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) %> -
-<%= 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| %> +- <%= render :partial => 'design_file', :locals => { :object => @design, :field_name => "themes", :f => f, :classes => "r_destroy, r_edit" } %> + <%= f.label :themes, t('admin.themes') %> +
- <%= 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