2011-07-12 08:02:41 +00:00
|
|
|
class Design
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
field :title
|
|
|
|
field :author
|
|
|
|
field :intro
|
|
|
|
field :version
|
|
|
|
|
2011-08-22 05:45:21 +00:00
|
|
|
has_many :pages
|
2011-09-05 08:06:17 +00:00
|
|
|
|
2011-08-08 09:51:37 +00:00
|
|
|
embeds_one :layout
|
2011-09-13 03:57:20 +00:00
|
|
|
embeds_one :default_css, :class_name => "Stylesheet"
|
|
|
|
embeds_one :reset_css, :class_name => "Stylesheet"
|
2011-09-09 07:40:13 +00:00
|
|
|
embeds_many :stylesheets
|
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-10-18 03:21:24 +00:00
|
|
|
embeds_many :custom_images, as: :design_image
|
2011-09-05 08:06:17 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2011-12-23 10:34:21 +00:00
|
|
|
# validate do
|
|
|
|
# errors.add('atttribute1', 'error for 1') if files_with_duplicate?
|
|
|
|
# errors.add('atttribute2', 'error for 1') if files_with_noname?
|
|
|
|
# end
|
|
|
|
|
|
|
|
def files_with_duplicate?
|
|
|
|
[self.javascripts, self.images,self.themes].each do |objects_hash|
|
|
|
|
ary = objects_hash.collect{ |k| k.file_filename}
|
|
|
|
ary.compact!
|
|
|
|
# debugger
|
|
|
|
return true if(ary.count!=ary.uniq.count)
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def files_with_noname?
|
|
|
|
[self.javascripts, self.images,self.themes].each do |objects_hash|
|
|
|
|
objects_hash.each{ |k|
|
|
|
|
return true if(k.file_filename.nil? && !k.to_save?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def new_files=(*attrs)
|
|
|
|
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
|
|
|
|
self.files=([items_ary, key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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-12-23 10:34:21 +00:00
|
|
|
# Update or create the attribute records
|
2011-07-14 00:48:42 +00:00
|
|
|
def files=(attrs)
|
2011-08-08 09:23:20 +00:00
|
|
|
case attrs.last
|
2011-09-08 10:12:04 +00:00
|
|
|
when 'layout'
|
|
|
|
files = self.layout.build
|
|
|
|
else
|
|
|
|
files = eval(attrs.last)
|
2011-08-08 09:23:20 +00:00
|
|
|
end
|
2011-07-14 00:48:42 +00:00
|
|
|
attrs[0].each do |a|
|
2011-12-23 10:34:21 +00:00
|
|
|
|
2011-09-08 10:12:04 +00:00
|
|
|
if a[:id].blank? && !a[:file].blank?
|
|
|
|
files.build(:file => a[:file], :to_save => true)
|
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-09-01 11:08:45 +00:00
|
|
|
|
2011-07-26 10:22:17 +00:00
|
|
|
def del_embedded_objects
|
2011-08-08 09:51:37 +00:00
|
|
|
[[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-09-01 11:08:45 +00:00
|
|
|
|
2011-07-19 10:31:53 +00:00
|
|
|
def procs_embedded_objects
|
2011-09-04 17:14:49 +00:00
|
|
|
[self.javascripts, self.images].each do |objects|
|
2011-07-14 00:48:42 +00:00
|
|
|
objects.each do |object|
|
2011-09-01 11:08:45 +00:00
|
|
|
process_object(object)
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-04 17:14:49 +00:00
|
|
|
if self.layout.to_save
|
2011-09-08 10:12:04 +00:00
|
|
|
self.layout.to_save = false
|
2011-09-04 17:14:49 +00:00
|
|
|
self.layout.save
|
|
|
|
end
|
2011-09-13 03:57:20 +00:00
|
|
|
[[self.default_css], [self.reset_css], self.themes].each do |objects|
|
|
|
|
objects.each do |object|
|
|
|
|
if object
|
|
|
|
to_parse_url = object.to_save ? true : false
|
|
|
|
process_object(object)
|
|
|
|
object.parse_urls if to_parse_url
|
|
|
|
end
|
|
|
|
end
|
2011-09-01 11:08:45 +00:00
|
|
|
end
|
2011-12-23 10:34:21 +00:00
|
|
|
self.valid?
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
2011-09-01 11:08:45 +00:00
|
|
|
|
|
|
|
def process_object(object)
|
|
|
|
if object.to_save
|
2011-09-09 05:44:43 +00:00
|
|
|
if object.file_filename.nil?
|
2011-12-23 10:34:21 +00:00
|
|
|
new_object = self.send(object._type.downcase.pluralize).build(object.attributes)
|
|
|
|
new_object.file = object.file
|
2011-09-09 05:44:43 +00:00
|
|
|
object.destroy
|
2011-10-18 03:21:24 +00:00
|
|
|
new_object.to_save = false
|
2011-09-09 05:44:43 +00:00
|
|
|
new_object.save
|
|
|
|
else
|
2011-10-18 03:21:24 +00:00
|
|
|
object.to_save = false
|
2011-09-09 05:44:43 +00:00
|
|
|
object.save
|
|
|
|
end
|
2011-09-01 11:08:45 +00:00
|
|
|
end
|
|
|
|
if object.to_destroy
|
|
|
|
object.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|