Sha256: 053789269c0f7984e8d301d8740703f12ae9177b8e142adad84bb9a2bc7452b5

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

class Comfy::Cms::Snippet < ActiveRecord::Base
  self.table_name = 'comfy_cms_snippets'

  include Comfy::Cms::WithCategories

  cms_has_revisions_for :content

  # -- Relationships -----------------------------------------------------------
  belongs_to :site

  # -- Callbacks ---------------------------------------------------------------
  before_validation :assign_label
  before_create :assign_position
  after_save    :clear_page_content_cache
  after_destroy :clear_page_content_cache

  # -- Validations -------------------------------------------------------------
  validates :label,
    presence:   true
  validates :identifier,
    presence:   true,
    uniqueness: {scope: :site_id},
    format:     {with: /\A\w[a-z0-9_-]*\z/i}

protected

  def assign_label
    self.label = self.label.blank?? self.identifier.try(:titleize) : self.label
  end

  # When snippet is changed or removed we need to blow away all page caches as
  # we don't know where it was used.
  def clear_page_content_cache
    Comfy::Cms::Page.where(id: site.pages.pluck(:id)).update_all(content_cache: nil)
  end

  def assign_position
    max = self.site.snippets.maximum(:position)
    self.position = max ? max + 1 : 0
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-2.0.2 app/models/comfy/cms/snippet.rb
comfortable_mexican_sofa-2.0.1 app/models/comfy/cms/snippet.rb
comfortable_mexican_sofa-2.0.0 app/models/comfy/cms/snippet.rb