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
|
included do
|
||||||
|
|
||||||
|
## fields ##
|
||||||
field :redirect, :type => Boolean, :default => false
|
field :redirect, :type => Boolean, :default => false
|
||||||
|
|
||||||
field :redirect_url, :type => String
|
field :redirect_url, :type => String
|
||||||
|
|
||||||
|
## validations ##
|
||||||
validates_presence_of :redirect_url, :if => :redirect
|
validates_presence_of :redirect_url, :if => :redirect
|
||||||
|
|
||||||
validates_format_of :redirect_url, :with => Locomotive::Regexps::URL, :allow_blank => true
|
validates_format_of :redirect_url, :with => Locomotive::Regexps::URL, :allow_blank => true
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -17,12 +17,38 @@ module Locomotive
|
|||||||
## callbacks ##
|
## callbacks ##
|
||||||
before_validation :set_slug_if_templatized
|
before_validation :set_slug_if_templatized
|
||||||
before_validation :ensure_target_klass_name_security
|
before_validation :ensure_target_klass_name_security
|
||||||
|
|
||||||
|
## scopes ##
|
||||||
|
scope :templatized, :where => { :templatized => true }
|
||||||
end
|
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
|
def target_klass
|
||||||
target_klass_name.constantize
|
target_klass_name.constantize
|
||||||
end
|
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
|
def target_entry_name
|
||||||
if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
|
if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
|
||||||
@content_type ||= self.site.content_types.find($1)
|
@content_type ||= self.site.content_types.find($1)
|
||||||
@ -32,6 +58,12 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
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)
|
def fetch_target_entry(permalink)
|
||||||
target_klass.find_by_permalink(permalink)
|
target_klass.find_by_permalink(permalink)
|
||||||
end
|
end
|
||||||
|
@ -17,6 +17,7 @@ module Locomotive
|
|||||||
field :title, :localize => true
|
field :title, :localize => true
|
||||||
field :slug, :localize => true
|
field :slug, :localize => true
|
||||||
field :fullpath, :localize => true
|
field :fullpath, :localize => true
|
||||||
|
field :handle
|
||||||
field :raw_template, :localize => true
|
field :raw_template, :localize => true
|
||||||
field :locales, :type => Array
|
field :locales, :type => Array
|
||||||
field :published, :type => Boolean, :default => false
|
field :published, :type => Boolean, :default => false
|
||||||
@ -40,6 +41,7 @@ module Locomotive
|
|||||||
## 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_uniqueness_of :handle, :allow_blank => true
|
||||||
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 ##
|
||||||
@ -48,6 +50,7 @@ module Locomotive
|
|||||||
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
||||||
scope :published, :where => { :published => true }
|
scope :published, :where => { :published => true }
|
||||||
scope :fullpath, lambda { |fullpath| { :where => { :fullpath => fullpath } } }
|
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)
|
scope :minimal_attributes, :only => %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at)
|
||||||
|
|
||||||
## methods ##
|
## methods ##
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
module Locomotive
|
module Locomotive
|
||||||
class PagePresenter < BasePresenter
|
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
|
def escaped_raw_template
|
||||||
h(self.source.raw_template)
|
h(self.source.raw_template)
|
||||||
@ -12,7 +12,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def included_methods
|
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
|
end
|
||||||
|
|
||||||
def localized_fullpaths
|
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.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 :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?}" }
|
= 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:
|
hints:
|
||||||
page:
|
page:
|
||||||
|
handle: "Unique identifier to retrieve this page within an external controller instance"
|
||||||
published: "Only authenticated accounts can view unpublished pages."
|
published: "Only authenticated accounts can view unpublished pages."
|
||||||
cache_strategy: "Cache the page for better performance. The 'Simple' choice is a good compromise."
|
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."
|
templatized: "Use the page as a template for a model you defined."
|
||||||
|
@ -28,40 +28,36 @@ module Locomotive
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module InstanceMethods
|
def set_content_type(*args)
|
||||||
|
value = :other
|
||||||
|
|
||||||
def set_content_type(*args)
|
content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type
|
||||||
value = :other
|
|
||||||
|
|
||||||
content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type
|
self.class.content_types.each_pair do |type, rules|
|
||||||
|
rules.each do |rule|
|
||||||
self.class.content_types.each_pair do |type, rules|
|
case rule
|
||||||
rules.each do |rule|
|
when String then value = type if content_type == rule
|
||||||
case rule
|
when Regexp then value = type if (content_type =~ rule) == 0
|
||||||
when String then value = type if content_type == rule
|
|
||||||
when Regexp then value = type if (content_type =~ rule) == 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
model.content_type = value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_size(*args)
|
model.content_type = value
|
||||||
model.size = file.size
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def set_width_and_height
|
def set_size(*args)
|
||||||
if model.image?
|
model.size = file.size
|
||||||
magick = ::Magick::Image.read(current_path).first
|
end
|
||||||
model.width, model.height = magick.columns, magick.rows
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def image?(file)
|
def set_width_and_height
|
||||||
model.image?
|
if model.image?
|
||||||
|
magick = ::Magick::Image.read(current_path).first
|
||||||
|
model.width, model.height = magick.columns, magick.rows
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def image?(file)
|
||||||
|
model.image?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -26,17 +26,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def locomotive_page
|
def locomotive_page
|
||||||
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
|
path = self.locomotive_page_path
|
||||||
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
|
|
||||||
|
|
||||||
if page = current_site.pages.any_in(:fullpath => [*path]).first
|
if page = current_site.pages.any_in(:fullpath => [*path]).first
|
||||||
if not page.published? and current_locomotive_account.nil?
|
if not page.published? and current_locomotive_account.nil?
|
||||||
@ -55,6 +45,22 @@ module Locomotive
|
|||||||
page || not_found_page
|
page || not_found_page
|
||||||
end
|
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
|
def locomotive_context
|
||||||
assigns = {
|
assigns = {
|
||||||
'site' => current_site,
|
'site' => current_site,
|
||||||
|
Loading…
Reference in New Issue
Block a user