Sha256: 3c23f5e792efdee3b74ab1f1f7549fa36db9e74f8b9a9c67b801175118565753

Contents?: true

Size: 1.43 KB

Versions: 12

Compression:

Stored size: 1.43 KB

Contents

class Cms::Snippet < ActiveRecord::Base
  
  ComfortableMexicanSofa.establish_connection(self)
  
  set_table_name :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_cached_page_content
  after_destroy :clear_cached_page_content
  
  # -- Validations ----------------------------------------------------------
  validates :site_id,
    :presence   => true
  validates :label,
    :presence   => true
  validates :slug,
    :presence   => true,
    :uniqueness => { :scope => :site_id },
    :format     => { :with => /^\w[a-z0-9_-]*$/i }
    
  # -- Scopes ---------------------------------------------------------------
  default_scope order(:position)
  
protected
  
  def assign_label
    self.label = self.label.blank?? self.slug.try(:titleize) : self.label
  end
  
  # Note: This might be slow. We have no idea where the snippet is used, so
  # gotta reload every single page. Kinda sucks, but might be ok unless there
  # are hundreds of pages.
  def clear_cached_page_content
    site.pages.all.each{ |page| page.save }
  end
  
  def assign_position
    max = self.site.snippets.maximum(:position)
    self.position = max ? max + 1 : 0
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.5.11 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.10 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.9 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.8 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.7 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.6 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.5 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.4 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.3 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.2 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.1 app/models/cms/snippet.rb
comfortable_mexican_sofa-1.5.0 app/models/cms/snippet.rb