CarrierWave workaround: the files have to be saved manually

This commit is contained in:
chris2tof 2011-07-14 08:48:42 +08:00
parent f73f09ffcf
commit c3ada90365
8 changed files with 39 additions and 54 deletions

3
.gitignore vendored
View File

@ -4,3 +4,6 @@ db/*.sqlite3
log/*.log log/*.log
tmp/**/* tmp/**/*
public/uploads/**/* public/uploads/**/*
uploads/**/*
.DS_Store

View File

@ -6,11 +6,7 @@ class Admin::DesignsController < ApplicationController
end end
def new def new
@designs = Design.new @design = Design.new
@designs.stylesheets.build
@designs.javascripts.build
@designs.images.build
end end
def update def update
@ -32,7 +28,6 @@ class Admin::DesignsController < ApplicationController
end end
def create def create
debugger
@design = Design.new(params[:design]) @design = Design.new(params[:design])
if @design.save if @design.save
flash[:notice] = "Successfully created design and tasks." flash[:notice] = "Successfully created design and tasks."

View File

@ -17,55 +17,38 @@ class Design
embeds_many :javascripts embeds_many :javascripts
embeds_many :images embeds_many :images
after_update :destroy_attrs after_save :save_embedded_objects
def javascripts=(*attrs) def javascripts=(*attrs)
self.attribute_models=(attrs<<'javascripts') self.files = (attrs << 'javascripts')
end end
def stylesheets=(*attrs) def stylesheets=(*attrs)
self.attribute_models=(attrs<<'stylesheets') self.files = (attrs << 'stylesheets')
end end
def images=(*attrs) def images=(*attrs)
self.attribute_models=(attrs<<'images') self.files = (attrs << 'images')
end end
# Update or create the attribute_model records # Update or create the attribute_model records
def attribute_models=(attrs) def files=(attrs)
#attribute_models = eval(attributes[:type]) files = eval(attrs.last)
# if attributes.original_filename.blank? attrs[0].each do |a|
attribute_models=eval(attrs.last) files.build(:file => a[:file], :to_save => true)
a=attribute_models.build() end
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)
end end
protected protected
# Destroy the i18n_variable for each attribute_models if marked to destroy def save_embedded_objects
def destroy_attrs [self.stylesheets, self.javascripts, self.images].each do |objects|
=begin objects.each do |object|
attribute_models.each do |a| if object.to_save
if a.should_destroy? object.to_save = false
a.destroy_i18n_variable object.save
end
end end
end end
=end
end end
end end

View File

@ -3,4 +3,6 @@ class DesignFile
mount_uploader :file, AssetUploader mount_uploader :file, AssetUploader
field :to_save, :type => Boolean
end end

View File

@ -1,3 +1,3 @@
class Image<DesignFile class Image < DesignFile
embedded_in :design
end end

View File

@ -1,3 +1,3 @@
class Javascript<DesignFile class Javascript < DesignFile
embedded_in :design
end end

View File

@ -1,5 +1,3 @@
class Stylesheet < DesignFile class Stylesheet < DesignFile
mount_uploader :file, AssetUploader
embedded_in :design embedded_in :design
end end

View File

@ -15,7 +15,7 @@
<p> <p>
<%= f.label "layout", t('admin.layout') %> <%= f.label "layout", t('admin.layout') %>
<% if @design.nil? %> <% if @design.layout.blank? %>
<%= f.file_field :layout %> <%= f.file_field :layout %>
<% else%> <% else%>
<%= File.basename (Design.all.last.layout.url) %> <%= File.basename (Design.all.last.layout.url) %>
@ -25,17 +25,21 @@
<p> <p>
<%= f.label "stylesheet", t('admin.stylesheet') %> <%= f.label "stylesheet", t('admin.stylesheet') %>
<%= fields_for 'design[stylesheets]' do |f| %> <%= fields_for "design[stylesheets][]", @design, :index => nil do |f| %>
<%= f.file_field :file %> <%= f.file_field :file %>
<% end%> <% end %>
</p> </p>
<p> <p>
<%= f.label "javascript", t('admin.') %> <%= f.label "javascript", t('admin.') %>
<%= f.file_field :javaascripts %> <%= fields_for "design[javascripts][]", @design, :index => nil do |f| %>
<%= f.file_field :file %>
<% end %>
</p> </p>
<p> <p>
<%= f.label "image", t('admin.') %> <%= f.label "image", t('admin.') %>
<%= f.file_field :images %> <%= fields_for "design[images][]", @design, :index => nil do |f| %>
<%= f.file_field :file %>
<% end %>
</p> </p>