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-24 13:04:53 +00:00
|
|
|
after_save :update_templates
|
|
|
|
after_destroy :update_templates
|
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
|
2010-09-26 22:31:50 +00:00
|
|
|
# TODO: refactor it
|
2010-08-21 22:48:24 +00:00
|
|
|
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-08-24 13:04:53 +00:00
|
|
|
def update_templates
|
2010-10-04 09:16:35 +00:00
|
|
|
return unless (self.site rescue false) # not run if the site is being destroyed
|
|
|
|
|
2010-08-24 13:04:53 +00:00
|
|
|
pages = self.site.pages.any_in(:snippet_dependencies => [self.slug]).to_a
|
|
|
|
|
|
|
|
pages.each do |page|
|
|
|
|
self._change_snippet_inside_template(page.template.root)
|
|
|
|
|
2011-04-28 15:04:18 +00:00
|
|
|
page.send(:_serialize_template)
|
2010-10-04 09:16:35 +00:00
|
|
|
|
2011-04-28 15:04:18 +00:00
|
|
|
Page.without_callback(:save, :after, :update_template_descendants) do
|
|
|
|
page.save(:validate => false)
|
|
|
|
end
|
2010-08-24 13:04:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _change_snippet_inside_template(node)
|
2010-08-24 22:59:22 +00:00
|
|
|
case node
|
|
|
|
when Locomotive::Liquid::Tags::Snippet
|
|
|
|
node.refresh(self) if node.slug == self.slug
|
2010-09-26 22:31:50 +00:00
|
|
|
when Locomotive::Liquid::Tags::InheritedBlock
|
2011-02-16 09:37:26 +00:00
|
|
|
_change_snippet_inside_template(node.parent) if node.parent
|
|
|
|
end
|
|
|
|
# Walk the children of this entry if they're available.
|
|
|
|
if node.respond_to?(:nodelist)
|
|
|
|
(node.nodelist || []).each do |child|
|
|
|
|
self._change_snippet_inside_template(child)
|
2010-08-24 13:04:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|