module Scrivito # @api public module DefaultCmsRoutingHelper # Returns the path for +target+ using the +CmsController+ routes. # +target+ can be an +Obj+ or a +Link+ or a +LinkList+. # If +target+ is a +Linklist+, it must be non-empty. The first +Link+ from the +LinkList+ will be used. # +options+ are optional and include url settings such as path parameters or protocol. # @return [String] # @api public def cms_path(target, options = {}) cms_path_or_url(target, "path", options) end # Returns the absolute URL for target using the +CmsController+ routes. # +target+ can be an +Obj+ or a +Link+ or a +LinkList+. # If +target+ is a +Linklist+, it must be non-empty. The first +Link+ from the +LinkList+ will be used. # +options+ are optional and include url settings such as path parameters or protocol. # @return [String] # @api public def cms_url(target, options = {}) cms_path_or_url(target, "url", options) end LINK_TO_EMPTY_LINKLIST = "#__empty_linklist" LINK_TO_EMPTY_BLOB = "#__empty_blob" def cms_path_or_url(target, path_or_url, options) if cms_with_editing_context?(target) cms_path_or_url_with_editing_context(target, path_or_url, options) else cms_path_or_url_without_editing_context(target, path_or_url, options) end end def cms_path_or_url_with_editing_context(target, path_or_url, options) cms_path_or_url_without_editing_context(target, path_or_url, options.merge(cms_editing_context.to_params)) end def cms_path_or_url_without_editing_context(target, path_or_url, options) if target.is_a?(Link) cms_path_or_url_for_links(target, path_or_url, options) elsif target.is_a?(Obj) cms_path_or_url_for_objs(target, path_or_url, options) elsif target.respond_to?(:first) if target.first.is_a?(Link) cms_path_or_url_for_links(target.first, path_or_url, options) else return LINK_TO_EMPTY_LINKLIST end else raise "cms_path or cms_url was called with an instance of #{target.class}. "+ "It must only be called with an Obj or a Link or a non-empty LinkList." end end def cms_path_or_url_for_links(link, path_or_url, options = {}) url = basic_url_or_path_for_link(link, path_or_url, Rack::Utils.parse_nested_query(link.query).merge(options)) url = url + "##{link.fragment}" if link.fragment.present? url end def cms_path_or_url_for_objs(obj, path_or_url, options = {}) permalink = obj.permalink dont_resolve_blobs = options.delete(:dont_resolve_blobs) if permalink __send__("cms_permalink_#{path_or_url}", options.merge(:permalink => permalink)) elsif obj.homepage? __send__("root_#{path_or_url}", options) else if obj.binary? && !dont_resolve_blobs if obj.body_data_url enforce_protocol_from_request(obj.body_data_url) else LINK_TO_EMPTY_BLOB end else __send__( "cms_id_#{path_or_url}", options.merge( id: obj.id, slug: obj.slug.presence ) ) end end end def cms_editing_context request.env[EditingContextMiddleware::ENVKEY] || EditingContext.new end def inplace_editing_allowed? cms_editing_context.authenticated_editor? end private def cms_with_editing_context?(target) inplace_editing_allowed? && (!target.is_a?(Link) || target.internal?) end def basic_url_or_path_for_link(link, path_or_url, options = {}) if link.internal? cms_path_or_url_without_editing_context(link.obj, path_or_url, options) else url = link.url options.delete(:dont_resolve_blobs) url = merge_options(url, options) if options.any? url end end def merge_options(url, options) parsed_url = URI.parse(url) query = Rack::Utils.parse_query(parsed_url.query) merged_query = query.merge(options.stringify_keys) parsed_url.query = merged_query.to_query parsed_url.to_s end def enforce_protocol_from_request(url) request.ssl? && !url.starts_with?('https') ? url.gsub(/^http/, 'https') : url end end end