page_datum.rb 840 Bytes
module Kanjai
  class PageDatum < ActiveRecord::Base
    belongs_to :page

    has_many :page_contents

    validates :title, :url, :page_id, :lang, presence: true

    serialize :template_content, Array

    after_save :delete_not_exist_content


    def delete_not_exist_content
      self.template_content ||= []
      exist_id = []
      self.template_content.each do |row|
        row['attributes'] ||= {}

        if row['attributes']['id'].present?
          exist_id << row['attributes']['id']
        end

        row["cells"].each do |cell|
          exist_id << cell['id']
        end
      end

      self.page_contents.where('structure_id not in (?)', exist_id).each do |item|
        item.destroy
      end

    end

    def last_modified
      updated_at.utc
    end

    def etag_cache_key
      cache_key
    end

  end
end