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

98 lines
2.3 KiB
Ruby
Raw Normal View History

class Design
include Mongoid::Document
include Mongoid::Timestamps
field :title
field :author
field :intro
field :version
has_many :pages
embeds_one :layout, :cascade_callbacks => true
embeds_one :default_css, :class_name => "Stylesheet", :cascade_callbacks => true
embeds_one :reset_css, :class_name => "Stylesheet", :cascade_callbacks => true
embeds_many :themes, :cascade_callbacks => true
embeds_many :javascripts, :cascade_callbacks => true
embeds_many :images, :cascade_callbacks => true
embeds_many :custom_images, :class_name => 'Image', :cascade_callbacks => true
validates_presence_of :title
validates_presence_of :author
after_save :parse_css_for_images
2011-08-08 09:23:20 +00:00
2011-12-23 10:34:21 +00:00
def new_files=(*attrs)
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
self.files=([items_ary, key])
end
end
# def javascripts=(*attrs)
# self.files = (attrs << 'javascripts')
# end
#
# def themes=(*attrs)
# self.files = (attrs << 'themes')
# end
#
# def images=(*attrs)
# self.files = (attrs << 'images')
# end
2011-12-23 10:34:21 +00:00
# Update or create the attribute 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|
2011-12-23 10:34:21 +00:00
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
def parse_css_for_images
self.images.each do |image|
image.save
end
if (self.default_css && self.default_css.changed)
self.default_css.parse_urls
attrs = self.default_css.attributes
file = self.default_css.file
file_orig = self.default_css.file_orig
self.default_css.destroy
d = self.build_default_css(attrs)
d.file = file
d.file_orig = file_orig
d.save
end
self.themes.each do |theme|
if theme.changed
theme.parse_urls
attrs = theme.attributes
file = theme.file
file_orig = theme.file_orig
theme.destroy
t = self.themes.build(attrs)
t.file = file
t.file_orig = file_orig
t.save
end
end
end
end