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

View File

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

View File

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

View File

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