create a liquid drop for site (include a method to get the home page)

This commit is contained in:
dinedine 2010-07-22 02:18:14 +02:00
parent d632a5363c
commit a03b631a71
4 changed files with 66 additions and 64 deletions

View File

@ -1,63 +1,63 @@
class Page
include Locomotive::Mongoid::Document
## Extensions ##
## Extensions ##
include Models::Extensions::Page::Tree
include Models::Extensions::Page::Parts
include Models::Extensions::Page::Render
include Models::Extensions::Page::Templatized
## fields ##
field :title
field :slug
field :fullpath
field :published, :type => Boolean, :default => false
field :cache_strategy, :default => 'none'
## associations ##
belongs_to_related :site
belongs_to_related :layout
embeds_many :parts, :class_name => 'PagePart'
## callbacks ##
before_validation :normalize_slug
before_save { |p| p.fullpath = p.fullpath(true) }
before_destroy :do_not_remove_index_and_404_pages
## validations ##
validates_presence_of :site, :title, :slug
validates_uniqueness_of :slug, :scope => [:site_id, :parent_id]
validates_exclusion_of :slug, :in => Locomotive.config.reserved_slugs, :if => Proc.new { |p| p.depth == 0 }
## named scopes ##
named_scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.lastest_items_nb
named_scope :index, :where => { :slug => 'index', :depth => 0 }
named_scope :not_found, :where => { :slug => '404', :depth => 0 }
named_scope :published, :where => { :published => true }
## behaviours ##
liquify_template :joined_parts
## methods ##
def index?
self.slug == 'index' && self.depth.to_i == 0
end
def not_found?
self.slug == '404' && self.depth.to_i == 0
end
def index_or_not_found?
self.index? || self.not_found?
end
def fullpath(force = false)
if read_attribute(:fullpath).present? && !force
return read_attribute(:fullpath)
end
if self.index? || self.not_found?
self.slug
else
@ -66,34 +66,34 @@ class Page
File.join slugs
end
end
def url
"http://#{self.site.domains.first}/#{self.fullpath}.html"
end
def with_cache?
self.cache_strategy != 'none'
end
def to_liquid(options = {})
def to_liquid
Locomotive::Liquid::Drops::Page.new(self)
end
protected
def do_not_remove_index_and_404_pages
return if (self.site rescue nil).nil?
if self.index? || self.not_found?
self.errors[:base] << I18n.t('errors.messages.protected_page')
end
self.errors.empty?
end
def normalize_slug
self.slug = self.title.clone if self.slug.blank? && self.title.present?
def normalize_slug
self.slug = self.title.clone if self.slug.blank? && self.title.present?
self.slug.slugify!(:without_extension => true) if self.slug.present?
end
end

View File

@ -1,14 +1,14 @@
class Site
include Locomotive::Mongoid::Document
## fields ##
field :name
field :subdomain
field :domains, :type => Array, :default => []
field :meta_keywords
field :meta_description
## associations ##
has_many_related :pages
has_many_related :layouts
@ -17,76 +17,80 @@ class Site
has_many_related :asset_collections
has_many_related :content_types
embeds_many :memberships
## validations ##
validates_presence_of :name, :subdomain
validates_uniqueness_of :subdomain
validates_exclusion_of :subdomain, :in => Locomotive.config.reserved_subdomains
validates_format_of :subdomain, :with => Locomotive::Regexps::SUBDOMAIN, :allow_blank => true
validate :domains_must_be_valid_and_unique
validate :domains_must_be_valid_and_unique
## callbacks ##
after_create :create_default_pages!
before_save :add_subdomain_to_domains
after_destroy :destroy_in_cascade!
## named scopes ##
named_scope :match_domain, lambda { |domain| { :where => { :domains => domain } } }
named_scope :match_domain_with_exclusion_of, lambda { |domain, site| { :where => { :domains => domain, :_id.ne => site.id } } }
## methods ##
def accounts
Account.criteria.in(:_id => self.memberships.collect(&:account_id))
end
def admin_memberships
self.memberships.find_all { |m| m.admin? }
end
def add_subdomain_to_domains
self.domains ||= []
self.domains ||= []
(self.domains << "#{self.subdomain}.#{Locomotive.config.default_domain}").uniq!
end
def domains_without_subdomain
(self.domains || []) - ["#{self.subdomain}.#{Locomotive.config.default_domain}"]
end
def domains_with_subdomain
((self.domains || []) + ["#{self.subdomain}.#{Locomotive.config.default_domain}"]).uniq
end
def to_liquid
Locomotive::Liquid::Drops::Site.new(self)
end
protected
def domains_must_be_valid_and_unique
return if self.domains.empty?
self.domains_without_subdomain.each do |domain|
if not self.class.match_domain_with_exclusion_of(domain, self).empty?
self.errors.add(:domains, :domain_taken, :value => domain)
end
if not domain =~ Locomotive::Regexps::DOMAIN
self.errors.add(:domains, :invalid_domain, :value => domain)
end
end
end
def create_default_pages!
%w{index 404}.each do |slug|
self.pages.create({
:slug => slug,
:title => I18n.t("attributes.defaults.pages.#{slug}.title"),
:slug => slug,
:title => I18n.t("attributes.defaults.pages.#{slug}.title"),
:body => I18n.t("attributes.defaults.pages.#{slug}.body")
})
end
end
def destroy_in_cascade!
%w{pages layouts snippets theme_assets asset_collections content_types}.each do |association|
self.send(association).destroy_all
end
end
end

View File

@ -1,26 +1,24 @@
module Locomotive
module Liquid
module Drops
module Locomotive
module Liquid
module Drops
class Page < Base
# liquid_attributes << :title << :slug
def title
@source.templatized? ? @context['content_instance'].highlighted_field_value : @source.title
end
def slug
@source.templatized? ? @source.content_type.slug.singularize : @source.slug
end
def children
@children ||= liquify(*@source.children)
end
def fullpath
@fullpath ||= @source.fullpath
end
end
end
end

View File

@ -1,14 +1,14 @@
module Locomotive
module Liquid
module Drops
module Locomotive
module Liquid
module Drops
class Site < Base
liquid_attributes << :name << :meta_keywords << :meta_description
def index
@index ||= @source.pages.index.first
end
end
end
end