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
|
|
|
|
|
|
|
|
mount_uploader :layout, DesignFileUploader
|
|
|
|
|
|
|
|
embeds_many :stylesheets
|
|
|
|
embeds_many :javascripts
|
|
|
|
embeds_many :images
|
|
|
|
|
2011-07-19 10:31:53 +00:00
|
|
|
after_save :procs_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-07-12 08:02:41 +00:00
|
|
|
def stylesheets=(*attrs)
|
2011-07-14 00:48:42 +00:00
|
|
|
self.files = (attrs << 'stylesheets')
|
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|
|
|
|
|
files.build(:file => a[:file], :to_save => true)
|
|
|
|
end
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-07-19 10:31:53 +00:00
|
|
|
def procs_embedded_objects
|
2011-07-14 00:48:42 +00:00
|
|
|
[self.stylesheets, self.javascripts, self.images].each do |objects|
|
|
|
|
objects.each do |object|
|
|
|
|
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
|