module RailsConnector # @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_UNREACHABLE = "#__target_object_not_reachable" LINK_TO_EMPTY_LINKLIST = "#__empty_linklist" LINK_TO_EMPTY_BLOB = "#__empty_blob" def cms_path_or_url(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 = {}) return LINK_TO_UNREACHABLE if link.internal? && link.obj.nil? url = basic_url_or_path_for_link(link, path_or_url, options) url = url + "?#{link.query}" if link.query.present? && options.empty? 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 private def basic_url_or_path_for_link(link, path_or_url, options = {}) if link.internal? __send__("cms_#{path_or_url}", link.obj, options) else url = link.external_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