This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/page.rb

74 lines
2.7 KiB
Ruby

class Page < Item
include Impressionist::Impressionable
is_impressionable :counter_cache => { :column_name => :view_count }
field :content
field :app_frontend_url
field :theme_id, :type => BSON::ObjectId, :default => nil
field :category, :default => ''
field :tag, :default => ''
field :view_count, :type => Integer, :default => 0
field :page_title, localize: true
field :frontend_style
field :frontend_field , :type => Array
field :frontend_data_count
belongs_to :design
belongs_to :module_app
has_many :page_parts, :autosave => true, :dependent => :destroy
has_many :page_contexts, :autosave => true, :dependent => :destroy
has_many :page_metas, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :page_parts, :allow_destroy => true
before_save :create_parts, :delete_empty_frontend_field
# embeds_many :custom_images, :class_name => 'Image', as: :design_image
def generate_parts
page_design = self.design
page_design.layout.layout_parts.each do |layout_part|
current_part = self.page_parts.detect{|page_part| page_part.name.eql?(layout_part.name)}
current_part = self.page_parts.build(:name => layout_part.name) unless current_part
current_part.save if current_part.new_record?
end
end
protected
def create_parts
if self.new_record? || self.design_id_changed?
page_design = self.design
parent = self.parent
menu_part = parent.page_parts.detect{|page_part| page_part.kind.eql?('public_r_tag') && page_part.public_r_tag.eql?('sub_menu') && page_part.public_r_tag_object_id.eql?(parent.id.to_s)} if parent
page_design.layout.layout_parts.each do |layout_part|
current_part = self.page_parts.detect{|page_part| page_part.name.eql?(layout_part.name)}
current_part = self.page_parts.build(:name => layout_part.name) unless current_part
if menu_part && current_part.name.eql?(menu_part.name)
if current_part.new_record?
current_part.kind = menu_part.kind
current_part.public_r_tag = menu_part.public_r_tag
current_part.public_r_tag_object_id = menu_part.public_r_tag_object_id
else
current_part.update_attributes(:kind => menu_part.kind, :public_r_tag => menu_part.public_r_tag, :public_r_tag_object_id => menu_part.public_r_tag_object_id)
end
end
end
end
end
def delete_empty_frontend_field
if self.frontend_field
self.frontend_field.reject! { |ff| (ff[0].blank? || ff[1].blank?) }
# self.frontend_field.each do | ff |
# if ff[0].blank? || ff[1].blank?
# self.frontend_field.delete("") if self.frontend_field
# self.frontend_field.delete(ff)
# end
# end
end
end
end