2011-07-12 08:02:41 +00:00
|
|
|
class Design
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
field :title
|
|
|
|
field :author
|
|
|
|
field :intro
|
|
|
|
|
|
|
|
field :version
|
|
|
|
|
|
|
|
validates_presence_of :title
|
|
|
|
validates_presence_of :author
|
|
|
|
|
2011-08-08 04:29:32 +00:00
|
|
|
embeds_one :layout
|
2011-08-08 01:30:44 +00:00
|
|
|
mount_uploader :structure_css, DesignFileUploader
|
2011-07-12 08:02:41 +00:00
|
|
|
|
2011-08-08 01:30:44 +00:00
|
|
|
embeds_many :themes
|
2011-07-12 08:02:41 +00:00
|
|
|
embeds_many :javascripts
|
|
|
|
embeds_many :images
|
|
|
|
|
2011-07-19 10:31:53 +00:00
|
|
|
after_save :procs_embedded_objects
|
2011-07-26 10:22:17 +00:00
|
|
|
after_destroy :del_embedded_objects
|
2011-07-12 08:02:41 +00:00
|
|
|
|
|
|
|
def javascripts=(*attrs)
|
2011-07-14 00:48:42 +00:00
|
|
|
self.files = (attrs << 'javascripts')
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
2011-07-14 00:48:42 +00:00
|
|
|
|
2011-08-08 01:30:44 +00:00
|
|
|
def themes=(*attrs)
|
|
|
|
self.files = (attrs << 'themes')
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
2011-07-14 00:48:42 +00:00
|
|
|
def images=(*attrs)
|
|
|
|
self.files = (attrs << 'images')
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
2011-07-14 00:48:42 +00:00
|
|
|
# Update or create the attribute_model records
|
|
|
|
def files=(attrs)
|
|
|
|
files = eval(attrs.last)
|
|
|
|
attrs[0].each do |a|
|
2011-07-29 09:49:10 +00:00
|
|
|
if(a[:id]=='' && !a[:file].nil? )
|
|
|
|
if(a[:file]!='')
|
|
|
|
files.build(:file => a[:file], :to_save => true)
|
|
|
|
end
|
2011-07-22 07:54:38 +00:00
|
|
|
else
|
|
|
|
files.each do |file|
|
|
|
|
if file.id.to_s == a[:id]
|
2011-07-26 10:22:17 +00:00
|
|
|
file.to_destroy = a[:to_destroy]
|
2011-07-22 07:54:38 +00:00
|
|
|
end
|
|
|
|
end
|
2011-07-22 03:10:14 +00:00
|
|
|
end
|
2011-07-14 00:48:42 +00:00
|
|
|
end
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2011-07-26 10:22:17 +00:00
|
|
|
def del_embedded_objects
|
2011-08-08 01:30:44 +00:00
|
|
|
[self.themes, self.javascripts, self.images].each do |objects|
|
2011-07-26 10:22:17 +00:00
|
|
|
objects.each do |object|
|
|
|
|
object.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-19 10:31:53 +00:00
|
|
|
def procs_embedded_objects
|
2011-08-08 01:30:44 +00:00
|
|
|
[self.themes, self.javascripts, self.images].each do |objects|
|
2011-07-14 00:48:42 +00:00
|
|
|
objects.each do |object|
|
2011-07-20 09:05:28 +00:00
|
|
|
if object.file.blank?
|
|
|
|
object.to_save = false
|
|
|
|
end
|
2011-07-14 00:48:42 +00:00
|
|
|
if object.to_save
|
|
|
|
object.to_save = false
|
|
|
|
object.save
|
|
|
|
end
|
2011-07-19 10:31:53 +00:00
|
|
|
if object.to_destroy
|
|
|
|
object.destroy
|
|
|
|
end
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|