2010-08-21 22:48:24 +00:00
|
|
|
class Snippet
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :name
|
|
|
|
field :slug
|
|
|
|
field :template
|
|
|
|
|
|
|
|
## associations ##
|
|
|
|
referenced_in :site
|
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_validation :normalize_slug
|
|
|
|
|
2010-08-21 23:56:44 +00:00
|
|
|
# TODO: after_save callback to let pages embedding this snippet know about the changes the user has just made.
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
## validations ##
|
|
|
|
validates_presence_of :site, :name, :slug, :template
|
|
|
|
validates_uniqueness_of :slug, :scope => :site_id
|
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def normalize_slug
|
|
|
|
self.slug = self.name.clone if self.slug.blank? && self.name.present?
|
|
|
|
self.slug.slugify!(:without_extension => true, :downcase => true) if self.slug.present?
|
|
|
|
end
|
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|