79 lines
1.6 KiB
Ruby
79 lines
1.6 KiB
Ruby
class Design
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
|
|
field :title
|
|
field :author
|
|
field :intro
|
|
|
|
field :version
|
|
|
|
validates_presence_of :title
|
|
validates_presence_of :author
|
|
|
|
mount_uploader :layout, DesignFileUploader
|
|
mount_uploader :structure_css, DesignFileUploader
|
|
|
|
embeds_many :themes
|
|
embeds_many :javascripts
|
|
embeds_many :images
|
|
|
|
after_save :procs_embedded_objects
|
|
after_destroy :del_embedded_objects
|
|
|
|
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_model records
|
|
def files=(attrs)
|
|
files = eval(attrs.last)
|
|
attrs[0].each do |a|
|
|
if(a[:id]=='' && !a[:file].nil? )
|
|
if(a[:file]!='')
|
|
files.build(:file => a[:file], :to_save => true)
|
|
end
|
|
else
|
|
files.each do |file|
|
|
if file.id.to_s == a[:id]
|
|
file.to_destroy = a[:to_destroy]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
protected
|
|
def del_embedded_objects
|
|
[self.themes, self.javascripts, self.images].each do |objects|
|
|
objects.each do |object|
|
|
object.destroy
|
|
end
|
|
end
|
|
end
|
|
def procs_embedded_objects
|
|
[self.themes, self.javascripts, self.images].each do |objects|
|
|
objects.each do |object|
|
|
if object.file.blank?
|
|
object.to_save = false
|
|
end
|
|
if object.to_save
|
|
object.to_save = false
|
|
object.save
|
|
end
|
|
if object.to_destroy
|
|
object.destroy
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|