33 lines
690 B
Ruby
33 lines
690 B
Ruby
class Snippet
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
## fields ##
|
|
field :name
|
|
field :slug
|
|
field :template
|
|
|
|
## associations ##
|
|
referenced_in :site
|
|
|
|
## callbacks ##
|
|
before_validation :normalize_slug
|
|
|
|
# TODO: after_save callback to let pages embedding this snippet know about the changes the user has just made.
|
|
|
|
## 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
|
|
|
|
|
|
end
|