Sha256: d2284bf1143e3868fb6224c99ba31ab9398cbbef292945324f29c3cbbfbf5203

Contents?: true

Size: 1.3 KB

Versions: 19

Compression:

Stored size: 1.3 KB

Contents

class Comfy::Cms::Snippet < ActiveRecord::Base
  self.table_name = 'comfy_cms_snippets'
  
  cms_is_categorized
  cms_is_mirrored
  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 :site_id,
    :presence   => true
  validates :label,
    :presence   => true
  validates :identifier,
    :presence   => true,
    :uniqueness => { :scope => :site_id },
    :format     => { :with => /\A\w[\/a-z0-9_-]*\z/i }
    
  # -- Scopes ---------------------------------------------------------------
  default_scope -> { order('comfy_cms_snippets.position') }
  
  protected
  
  def assign_label
    self.label = self.label.blank?? self.identifier.try(:titleize) : self.label
  end
  
  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

19 entries across 19 versions & 1 rubygems

Version Path
comfortable_mexican_loveseat-0.1.1 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.1.0 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.23 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.22 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.21 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.20 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.19 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.18 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.17 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.16 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.15 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.14 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.13 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.12 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.11 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.10 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.9 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.8 app/models/comfy/cms/snippet.rb
comfortable_mexican_loveseat-0.0.7 app/models/comfy/cms/snippet.rb