Sha256: dc55d60b6390d45589f6d023835aa67766d8ae7e47f4c25627e4b7b0ec1189ed
Contents?: true
Size: 1.87 KB
Versions: 10
Compression:
Stored size: 1.87 KB
Contents
require 'addressable/uri' module Scrivito class LinkParser def initialize(host, port) @host = host @port = port end def parse(url) uri = Addressable::URI.parse(url) link_params = {} if obj = find_obj(uri) link_params[:obj] = obj else if application_uri?(uri) uri.path = '/' unless uri.path.present? uri.port = nil uri.host = nil uri.scheme = nil end link_params[:url] = uri.to_s end link_params[:query] = remove_editing_context_params(uri.query) link_params[:fragment] = uri.fragment Link.new(link_params) rescue Addressable::URI::InvalidURIError Link.new(url: url) end private def application_uri?(uri) uri.absolute? && internal_uri?(uri) || uri.relative? && uri.path.present? end def internal_uri?(uri) if uri.port.present? uri.host == @host && uri.port == Integer(@port) else uri.host == @host end end def find_obj(uri) return nil unless application_uri?(uri) && application_route?(uri) route_params = route(uri) if id = route_params[:id] Obj.find(id) elsif permalink = route_params[:permalink] Obj.find_by_permalink(permalink) end rescue Scrivito::ResourceNotFound end def application_route?(uri) route_params = route(uri) route_params && route_params[:controller] == 'scrivito/cms_dispatch' end def route(uri) Rails.application.routes.recognize_path(uri.to_s, method: :get) rescue ActionController::RoutingError end def remove_editing_context_params(query) if query params = Rack::Utils.parse_nested_query(query) params.delete('_scrivito_display_mode') params.delete('_scrivito_workspace_id') params.to_query end end end end
Version data entries
10 entries across 10 versions & 1 rubygems