Sha256: a9c694a142ce5b3273d8f3b1cad3faa8b35d3a606030bdc1dcb170b053c2d314

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 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

    accepts_nested_attributes_for :sidebar,     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-0.2.3 app/models/pages_cms/page.rb
pages_cms-0.2.2 app/models/pages_cms/page.rb
pages_cms-0.2.1 app/models/pages_cms/page.rb
pages_cms-0.2.0 app/models/pages_cms/page.rb
pages_cms-0.1.1 app/models/pages_cms/page.rb
pages_cms-0.1.0 app/models/pages_cms/page.rb
pages_cms-0.0.3 app/models/pages_cms/page.rb
pages_cms-0.0.2 app/models/pages_cms/page.rb
pages_cms-0.0.1 app/models/pages_cms/page.rb