Orbit/app/models/design.rb

65 lines
1.3 KiB
Ruby
Raw Normal View History

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-20 09:05:28 +00:00
before_save :cleanup_empty_fields
2011-07-19 10:31:53 +00:00
after_save :procs_embedded_objects
def javascripts=(*attrs)
self.files = (attrs << 'javascripts')
end
def stylesheets=(*attrs)
self.files = (attrs << 'stylesheets')
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].nil?)
files.build(:file => a[:file], :to_save => true)
end
end
end
protected
2011-07-20 09:05:28 +00:00
def cleanup_empty_fields
end
2011-07-19 10:31:53 +00:00
def procs_embedded_objects
[self.stylesheets, self.javascripts, self.images].each do |objects|
objects.each do |object|
2011-07-20 09:05:28 +00:00
if object.file.blank?
object.to_save = false
end
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
end
end
end
end