From c3ada903651c52747f8994d785744ab11f23911a Mon Sep 17 00:00:00 2001 From: chris2tof Date: Thu, 14 Jul 2011 08:48:42 +0800 Subject: [PATCH] CarrierWave workaround: the files have to be saved manually --- .gitignore | 3 ++ app/controllers/admin/designs_controller.rb | 7 +-- app/models/design.rb | 55 +++++++-------------- app/models/design_file.rb | 2 + app/models/image.rb | 4 +- app/models/javascript.rb | 4 +- app/models/stylesheet.rb | 2 - app/views/admin/designs/_form.html.erb | 16 +++--- 8 files changed, 39 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index d1369322..61042672 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ db/*.sqlite3 log/*.log tmp/**/* public/uploads/**/* +uploads/**/* + +.DS_Store diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb index 44e0662c..ba7af6a1 100644 --- a/app/controllers/admin/designs_controller.rb +++ b/app/controllers/admin/designs_controller.rb @@ -6,11 +6,7 @@ class Admin::DesignsController < ApplicationController end def new - @designs = Design.new - @designs.stylesheets.build - @designs.javascripts.build - @designs.images.build - + @design = Design.new end def update @@ -32,7 +28,6 @@ class Admin::DesignsController < ApplicationController end def create - debugger @design = Design.new(params[:design]) if @design.save flash[:notice] = "Successfully created design and tasks." diff --git a/app/models/design.rb b/app/models/design.rb index 52bca8da..c6fdac73 100644 --- a/app/models/design.rb +++ b/app/models/design.rb @@ -17,55 +17,38 @@ class Design embeds_many :javascripts embeds_many :images - after_update :destroy_attrs + after_save :save_embedded_objects def javascripts=(*attrs) - self.attribute_models=(attrs<<'javascripts') + self.files = (attrs << 'javascripts') end - + def stylesheets=(*attrs) - self.attribute_models=(attrs<<'stylesheets') + self.files = (attrs << 'stylesheets') end - + def images=(*attrs) - self.attribute_models=(attrs<<'images') + self.files = (attrs << 'images') end - + # Update or create the attribute_model records - def attribute_models=(attrs) - #attribute_models = eval(attributes[:type]) - # if attributes.original_filename.blank? - attribute_models=eval(attrs.last) - a=attribute_models.build() - a.file=attrs[0] - # else - # attribute_model = attribute_models.detect {|a| a.id.to_s == attributes[:id].to_s } - # attribute_model.update_attributes(attributes) - #end - end - - def is_built_in? - self.built_in - end - - def is_disabled? - self.disabled - end - - def get_enabled_attribute_models - self.attribute_models.excludes('disabled' => true) + def files=(attrs) + files = eval(attrs.last) + attrs[0].each do |a| + files.build(:file => a[:file], :to_save => true) + end end protected - # Destroy the i18n_variable for each attribute_models if marked to destroy - def destroy_attrs -=begin - attribute_models.each do |a| - if a.should_destroy? - a.destroy_i18n_variable + def save_embedded_objects + [self.stylesheets, self.javascripts, self.images].each do |objects| + objects.each do |object| + if object.to_save + object.to_save = false + object.save + end end end -=end end end diff --git a/app/models/design_file.rb b/app/models/design_file.rb index 47226445..9871d74c 100644 --- a/app/models/design_file.rb +++ b/app/models/design_file.rb @@ -2,5 +2,7 @@ class DesignFile include Mongoid::Document mount_uploader :file, AssetUploader + + field :to_save, :type => Boolean end diff --git a/app/models/image.rb b/app/models/image.rb index bbd0eaf5..98abeda4 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -1,3 +1,3 @@ -class Image <%= f.label "layout", t('admin.layout') %> -<% if @design.nil? %> +<% if @design.layout.blank? %> <%= f.file_field :layout %> <% else%> <%= File.basename (Design.all.last.layout.url) %> @@ -25,17 +25,21 @@

<%= f.label "stylesheet", t('admin.stylesheet') %> -<%= fields_for 'design[stylesheets]' do |f| %> - <%= f.file_field :file %> -<% end%> +<%= fields_for "design[stylesheets][]", @design, :index => nil do |f| %> + <%= f.file_field :file %> +<% end %>

<%= f.label "javascript", t('admin.') %> -<%= f.file_field :javaascripts %> +<%= fields_for "design[javascripts][]", @design, :index => nil do |f| %> + <%= f.file_field :file %> +<% end %>

<%= f.label "image", t('admin.') %> -<%= f.file_field :images %> +<%= fields_for "design[images][]", @design, :index => nil do |f| %> + <%= f.file_field :file %> +<% end %>