Sha256: 0b1ef4f6e4691254f8d1685b25ba8b96d4443e241b6f826f464fe916b86be16c

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class PageNode
  include Model
  include Model::OrderedTree
  include Model::Timestamps

  before_destroy :destroy_children

  belongs_to :page_type
  has_many :pages, dependent: :destroy
  # embeds_many :pages, cascade_callbacks: true

  validates_presence_of :page_type

  accepts_nested_attributes_for :pages,
    reject_if: :all_blank,
    allow_destroy: true

  before_update :touch_ancestors
  before_update :touch_pages
  before_update :denormalize_position
  before_destroy :touch_ancestors

  # scope :with_content, ->(lang) { where( :'pages.lang' => lang ) }
  # scope :published, ->(lang) { elem_match( pages: { lang: lang, :publication_state => :published } ) }
  # scope :published_and_hidden, ->(lang) {
  #   elem_match( pages: { lang: lang, :publication_state.in => [:published, :hidden] } )
  # }

  # Returns content for the specified +lang+.
  #
  def page( lang )
    pages.where( lang: lang.to_sym ).first
  end

private

  # Touches parents (if present)
  #
  def touch_ancestors
    ancestors.update_all updated_at: Time.now
  end

  # Touches pages (if present)
  #
  def touch_pages
    pages.update_all updated_at: Time.now
  end

  # Denormalize +position+ to owned Page(s).
  #
  def denormalize_position
    pages.update_all( position: position )
  end

end # class Page

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aerogel-pages-1.4.14 db/model/page_node.rb