Sha256: 420acff06c59de2a3e5b067819ab6f7dbec0e7b9ed9a14d88c8f07413f30157b

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module Effective
  class Page < ActiveRecord::Base
    acts_as_role_restricted
    acts_as_regionable
    acts_as_slugged

    has_many :menu_items, as: :menuable, dependent: :destroy

    self.table_name = EffectivePages.pages_table_name.to_s

    # structure do
    #   title             :string
    #   meta_description  :string

    #   draft             :boolean

    #   layout            :string
    #   template          :string

    #   slug              :string
    #   roles_mask        :integer

    #   timestamps
    # end

    validates :title, presence: true, length: { maximum: 255 }
    validates :meta_description, presence: true, length: { maximum: 150 }

    validates :layout, presence: true
    validates :template, presence: true

    scope :drafts, -> { where(draft: true) }
    scope :published, -> { where(draft: false) }
    scope :sorted, -> { order(:title) }
    scope :except_home, -> { where.not(title: 'Home') }

    def to_s
      title
    end

    def published?
      !draft?
    end

    def content
      region(:content).content
    end

    def content=(input)
      region(:content).content = input
    end

    # Returns a duplicated post object, or throws an exception
    def duplicate!
      Page.new(attributes.except('id', 'updated_at', 'created_at')).tap do |page|
        page.title = page.title + ' (Copy)'
        page.slug = page.slug + '-copy'
        page.draft = true

        regions.each do |region|
          page.regions.build(region.attributes.except('id', 'updated_at', 'created_at'))
        end

        page.save!
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_pages-2.1.0 app/models/effective/page.rb
effective_pages-2.0.9 app/models/effective/page.rb