Sha256: c524d083ab3fdec6836b3701c66d826467c81a0581c7cf7726a246d1b211798a

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 KB

Contents

module PagesCms
  class Page < ActiveRecord::Base
    belongs_to  :account
    belongs_to  :parent, class_name: 'Page'

    has_many    :children, class_name: 'Page', foreign_key: 'parent_id', dependent: :destroy
    has_many    :page_blocks, dependent: :destroy
    has_one     :sidebar, dependent: :destroy
    has_many    :sliders, dependent: :destroy

    accepts_nested_attributes_for :sidebar,     allow_destroy: true
    accepts_nested_attributes_for :sliders,     allow_destroy: true
    accepts_nested_attributes_for :page_blocks, allow_destroy: true

    validates :title, presence: true
    validates :slug,  presence: true

    # validate  :no_other_slug_in_account_space
    validate  :slug_has_no_spaces
    validate  :page_parent_of_itself

    before_validation :parameterize_slug

    private
      def parameterize_slug
        self.slug = title.parameterize
      end

      def slug_has_no_spaces
        if slug.index(/\s/) != nil
          errors.add(:slug, 'must have no whitespace')
        end
      end

      def page_parent_of_itself
        if self.parent_id != nil && self.parent_id == self.id
          errors.add(:base, 'Cannot be a sub-page of itself')
        end
      end

      # def no_other_slug_in_account_space
      #   unless self.account.pages.find_by_slug(self.slug).nil?
      #     errors.add(:base, 'A page already exists by this name!')
      #   end
      # end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pages_cms-2.1.1 app/models/pages_cms/page.rb
pages_cms-2.1.0 app/models/pages_cms/page.rb
pages_cms-2.0.1 app/models/pages_cms/page.rb
pages_cms-2.0.0 app/models/pages_cms/page.rb
pages_cms-1.2.3 app/models/pages_cms/page.rb
pages_cms-1.2.2 app/models/pages_cms/page.rb
pages_cms-1.2.1 app/models/pages_cms/page.rb
pages_cms-1.2.0 app/models/pages_cms/page.rb