remove deprecated warning about ActiveSupport::Concern + add the handle property to Page in order to retrieve them more easily (very useful to display a page from an external controller for instance
This commit is contained in:
parent
ca380de115
commit
3b5d04238f
@ -7,12 +7,12 @@ module Locomotive
|
||||
|
||||
included do
|
||||
|
||||
## fields ##
|
||||
field :redirect, :type => Boolean, :default => false
|
||||
|
||||
field :redirect_url, :type => String
|
||||
|
||||
## validations ##
|
||||
validates_presence_of :redirect_url, :if => :redirect
|
||||
|
||||
validates_format_of :redirect_url, :with => Locomotive::Regexps::URL, :allow_blank => true
|
||||
|
||||
end
|
||||
|
@ -17,12 +17,38 @@ module Locomotive
|
||||
## callbacks ##
|
||||
before_validation :set_slug_if_templatized
|
||||
before_validation :ensure_target_klass_name_security
|
||||
|
||||
## scopes ##
|
||||
scope :templatized, :where => { :templatized => true }
|
||||
end
|
||||
|
||||
# Returns the class specified by the target_klass_name property
|
||||
#
|
||||
# @example
|
||||
#
|
||||
# page.target_klass_name = 'Locomotive::Entry12345'
|
||||
# page.target_klass = Locomotive::Entry12345
|
||||
#
|
||||
# @return [ Class ] The target class
|
||||
#
|
||||
def target_klass
|
||||
target_klass_name.constantize
|
||||
end
|
||||
|
||||
# Gives the name which can be used in a liquid template in order
|
||||
# to reference an entry. It uses the slug property if the target klass
|
||||
# is a Locomotive content type or the class name itself for the other classes.
|
||||
#
|
||||
# @example
|
||||
#
|
||||
# page.target_klass_name = 'Locomotive::Entry12345' # related to the content type Articles
|
||||
# page.target_entry_name = 'article'
|
||||
#
|
||||
# page.target_klass_name = 'OurProduct'
|
||||
# page.target_entry_name = 'our_product'
|
||||
#
|
||||
# @return [ String ] The name in lowercase and underscored
|
||||
#
|
||||
def target_entry_name
|
||||
if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
|
||||
@content_type ||= self.site.content_types.find($1)
|
||||
@ -32,6 +58,12 @@ module Locomotive
|
||||
end
|
||||
end
|
||||
|
||||
# Finds the entry both specified by the target klass and identified by the permalink
|
||||
#
|
||||
# @param [ String ] permalink The permalink of the entry
|
||||
#
|
||||
# @return [ Object ] The document
|
||||
#
|
||||
def fetch_target_entry(permalink)
|
||||
target_klass.find_by_permalink(permalink)
|
||||
end
|
||||
|
@ -17,6 +17,7 @@ module Locomotive
|
||||
field :title, :localize => true
|
||||
field :slug, :localize => true
|
||||
field :fullpath, :localize => true
|
||||
field :handle
|
||||
field :raw_template, :localize => true
|
||||
field :locales, :type => Array
|
||||
field :published, :type => Boolean, :default => false
|
||||
@ -40,6 +41,7 @@ module Locomotive
|
||||
## validations ##
|
||||
validates_presence_of :site, :title, :slug
|
||||
validates_uniqueness_of :slug, :scope => [:site_id, :parent_id]
|
||||
validates_uniqueness_of :handle, :allow_blank => true
|
||||
validates_exclusion_of :slug, :in => Locomotive.config.reserved_slugs, :if => Proc.new { |p| p.depth == 0 }
|
||||
|
||||
## named scopes ##
|
||||
@ -48,6 +50,7 @@ module Locomotive
|
||||
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
||||
scope :published, :where => { :published => true }
|
||||
scope :fullpath, lambda { |fullpath| { :where => { :fullpath => fullpath } } }
|
||||
scope :handle, lambda { |handle| { :where => { :handle => handle } } }
|
||||
scope :minimal_attributes, :only => %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at)
|
||||
|
||||
## methods ##
|
||||
|
@ -1,7 +1,7 @@
|
||||
module Locomotive
|
||||
class PagePresenter < BasePresenter
|
||||
|
||||
delegate :title, :slug, :fullpath, :raw_template, :published, :listed, :templatized, :redirect, :redirect_url, :template_changed, :cache_strategy, :to => :source
|
||||
delegate :title, :slug, :fullpath, :handle, :raw_template, :published, :listed, :templatized, :redirect, :redirect_url, :template_changed, :cache_strategy, :to => :source
|
||||
|
||||
def escaped_raw_template
|
||||
h(self.source.raw_template)
|
||||
@ -12,7 +12,7 @@ module Locomotive
|
||||
end
|
||||
|
||||
def included_methods
|
||||
super + %w(title slug fullpath raw_template published listed templatized redirect redirect_url cache_strategy template_changed editable_elements localized_fullpaths)
|
||||
super + %w(title slug fullpath handle raw_template published listed templatized redirect redirect_url cache_strategy template_changed editable_elements localized_fullpaths)
|
||||
end
|
||||
|
||||
def localized_fullpaths
|
||||
|
@ -30,6 +30,8 @@
|
||||
|
||||
= f.inputs :name => :advanced_options, :id => 'advanced-options', :class => "inputs foldable #{'folded' if inputs_folded?(@page)}" do
|
||||
|
||||
= f.input :handle
|
||||
|
||||
= f.input :templatized, :as => :'Locomotive::Toggle', :style => "#{'display: none' if @page.redirect?}"
|
||||
|
||||
= f.input :target_klass_name, :as => :select, :collection => options_for_target_klass_name, :include_blank => false, :wrapper_html => { :style => "#{'display: none' unless @page.templatized?}" }
|
||||
|
@ -60,6 +60,7 @@ en:
|
||||
|
||||
hints:
|
||||
page:
|
||||
handle: "Unique identifier to retrieve this page within an external controller instance"
|
||||
published: "Only authenticated accounts can view unpublished pages."
|
||||
cache_strategy: "Cache the page for better performance. The 'Simple' choice is a good compromise."
|
||||
templatized: "Use the page as a template for a model you defined."
|
||||
|
@ -28,8 +28,6 @@ module Locomotive
|
||||
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
||||
def set_content_type(*args)
|
||||
value = :other
|
||||
|
||||
@ -63,8 +61,6 @@ module Locomotive
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -26,17 +26,7 @@ module Locomotive
|
||||
end
|
||||
|
||||
def locomotive_page
|
||||
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
|
||||
path = path.split('?').first # take everything before the query string or the lookup fails
|
||||
path.gsub!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '') # remove the page extension
|
||||
path.gsub!(/^\//, '') # remove the leading slash
|
||||
|
||||
path = 'index' if path.blank? || path == '_edit'
|
||||
|
||||
if path != 'index'
|
||||
dirname = File.dirname(path).gsub(/^\.$/, '') # also look for templatized page path
|
||||
path = [path, File.join(dirname, 'content_type_template').gsub(/^\//, '')]
|
||||
end
|
||||
path = self.locomotive_page_path
|
||||
|
||||
if page = current_site.pages.any_in(:fullpath => [*path]).first
|
||||
if not page.published? and current_locomotive_account.nil?
|
||||
@ -55,6 +45,22 @@ module Locomotive
|
||||
page || not_found_page
|
||||
end
|
||||
|
||||
def locomotive_page_path
|
||||
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
|
||||
path = path.split('?').first # take everything before the query string or the lookup fails
|
||||
path.gsub!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '') # remove the page extension
|
||||
path.gsub!(/^\//, '') # remove the leading slash
|
||||
|
||||
path = 'index' if path.blank? || path == '_edit'
|
||||
|
||||
if path != 'index'
|
||||
dirname = File.dirname(path).gsub(/^\.$/, '') # also look for templatized page path
|
||||
path = [path, File.join(dirname, 'content_type_template').gsub(/^\//, '')]
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
|
||||
def locomotive_context
|
||||
assigns = {
|
||||
'site' => current_site,
|
||||
|
Loading…
Reference in New Issue
Block a user