Sha256: 6423f9c5c2d6d2fbf3f0b1f638f30a7a4b140eea5ebf00146dac17d914b79708

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Refinery
  class PagePart < Refinery::Core::BaseModel

    belongs_to :page, :foreign_key => :refinery_page_id, :touch => true

    before_validation :set_default_slug

    validates :title, :presence => true
    validates :slug, :presence => true, :uniqueness => {:scope => :refinery_page_id}
    alias_attribute :content, :body

    translates :body

    def to_param
      "page_part_#{slug.downcase.gsub(/\W/, '_')}"
    end

    def body=(value)
      super

      normalise_text_fields
    end

    def slug_matches?(other_slug)
      slug.present? && (# protecting against the problem that occurs when have two nil slugs
        slug == other_slug.to_s ||
        parameterized_slug == parameterize(other_slug.to_s)
      )
    end

    def title_matches?(other_title)
      Refinery.deprecate("Refinery::PagePart#title_matches?", when: "3.1", replacement: "slug_matches?")
      slug_matches?(other_title.to_s.parameterize.underscore)
    end

    protected

    def normalise_text_fields
      if body? && body !~ %r{^<}
        self.body = "<p>#{body.gsub("\r\n\r\n", "</p><p>").gsub("\r\n", "<br/>")}</p>"
      end
    end

    private
    def parameterize(string)
      string.downcase.gsub(" ", "_")
    end

    def parameterized_slug
      parameterize(slug)
    end

    def set_default_slug
      self.slug = title.to_s.parameterize.underscore.presence if self.slug.blank?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
refinerycms-pages-3.0.6 app/models/refinery/page_part.rb