Sha256: 6c4db5965ca8be96900e29c482c8ac27389e54ff3ebde981d612e15dbb55c61c

Contents?: true

Size: 1.45 KB

Versions: 9

Compression:

Stored size: 1.45 KB

Contents

module PagesCms
  class Page < ActiveRecord::Base
    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, uniqueness: true

    validate  :slug_has_no_spaces
    validate  :page_parent_of_itself

    before_validation :parameterize_slug

    private
      def parameterize_slug
        if parent_id == nil
          self.slug = title.parameterize
        else
          self.slug = "#{self.parent.slug}-#{title.parameterize}"
        end
      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 not_sub_of_sub
        if self.parent_id != nil
          if self.parent.parent_id != nil
            errors.add(:base, 'Cannot be a subpage of a subpage')
          end
        end
      end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pages_cms-1.1.0 app/models/pages_cms/page.rb
pages_cms-1.0.8 app/models/pages_cms/page.rb
pages_cms-1.0.6 app/models/pages_cms/page.rb
pages_cms-1.0.5 app/models/pages_cms/page.rb
pages_cms-1.0.4 app/models/pages_cms/page.rb
pages_cms-1.0.3 app/models/pages_cms/page.rb
pages_cms-1.0.2 app/models/pages_cms/page.rb
pages_cms-1.0.1 app/models/pages_cms/page.rb
pages_cms-1.0.0 app/models/pages_cms/page.rb