orbit-basic/app/models/design/design.rb

105 lines
2.2 KiB
Ruby
Raw Normal View History

class Design
include Mongoid::Document
include Mongoid::Timestamps
field :title
field :author
field :intro
field :version
mount_uploader :default_css, AssetUploader
mount_uploader :reset_css, AssetUploader
has_many :pages
embeds_one :layout
2011-09-09 07:40:13 +00:00
embeds_many :stylesheets
embeds_many :themes
embeds_many :javascripts
embeds_many :images
validates_presence_of :title
validates_presence_of :author
2011-07-19 10:31:53 +00:00
after_save :procs_embedded_objects
2011-08-20 14:54:07 +00:00
# after_destroy :del_embedded_objects
2011-08-08 09:23:20 +00:00
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)
2011-08-08 09:23:20 +00:00
case attrs.last
when 'layout'
files = self.layout.build
else
files = eval(attrs.last)
2011-08-08 09:23:20 +00:00
end
attrs[0].each do |a|
if a[:id].blank? && !a[:file].blank?
files.build(:file => a[:file], :to_save => true)
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]
end
end
end
end
end
protected
2011-07-26 10:22:17 +00:00
def del_embedded_objects
[[self.layout],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
[self.javascripts, self.images].each do |objects|
objects.each do |object|
process_object(object)
end
end
if self.layout.to_save
self.layout.to_save = false
self.layout.save
self.layout.parse_urls
end
self.themes.each do |theme|
to_parse_url = theme.to_save ? true : false
process_object(theme)
theme.parse_urls if to_parse_url
end
end
def process_object(object)
if object.to_save
if object.file_filename.nil?
new_object = self.send(object._type.downcase.pluralize).build
new_object.file = object.file
object.destroy
new_object.save
else
object.save
end
end
if object.to_destroy
object.destroy
end
end
end