Sha256: df9b2875040b2a66a995835536447924b1f584f061fca906f162d27b94a67ba5
Contents?: true
Size: 1.65 KB
Versions: 17
Compression:
Stored size: 1.65 KB
Contents
class Cms::Snippet < ActiveRecord::Base ComfortableMexicanSofa.establish_connection(self) self.table_name = 'cms_snippets' cms_is_categorized cms_is_mirrored cms_has_revisions_for :content attr_accessible :identifier, :label, :content, :category_ids # -- 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 :identifier, :presence => true, :uniqueness => { :scope => :site_id }, :format => { :with => /^\w[a-z0-9_-]*$/i } # -- Scopes --------------------------------------------------------------- default_scope order('cms_snippets.position') protected def assign_label self.label = self.label.blank?? self.identifier.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 do |p| Cms::Page.where(:id => p.id).update_all(:content => p.content(true)) end end def assign_position max = self.site.snippets.maximum(:position) self.position = max ? max + 1 : 0 end end
Version data entries
17 entries across 17 versions & 1 rubygems