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
tmp/**/*
public/uploads/**/*
uploads/**/*
.DS_Store

View File

@ -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."

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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