Sha256: 5f9d55d7fea17d14a752afcc95a9ebe9f01b6c00edf567ff22e78654a40f8315

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

# -*- encoding : utf-8 -*-
module Reactor
  module Support
    class LinkMatcher
      def initialize(url)
        @url = url
      end

      def recognized?
        match = match_url
        (match[:action] == "index") &&
          (match[:controller] == "rails_connector/cms_dispatch") &&
          ((match[:id].present? && Obj.exists?(match[:id].to_i)) ||
          (match[:permalink].present? && Obj.exists?(:permalink => match[:permalink])))
      rescue ActionController::RoutingError
        return false
      end

      def rewrite_url
        match = match_url

        if match[:permalink].present?
          append_fragment_and_query Obj.find_by_permalink(match[:permalink]).path 
        elsif match[:id].present?
          append_fragment_and_query Obj.find(match[:id].to_i).path
        end
      end

      private
      def match_url
        return {} if @url.match(/\A[a-z0-9]*:/) # ignore fully qualified urls
        relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
        url = @url.clone
        url.gsub!(/^#{Regexp.escape(relative_url_root)}/, '') if relative_url_root.present?
        Rails.application.routes.recognize_path(url)
      end

      def append_fragment_and_query(obj_path)
        uri = URI.parse(@url)
        obj_path += "?#{uri.query}" if uri.query
        obj_path += "##{uri.fragment}" if uri.fragment
        obj_path
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infopark_reactor-1.11.0.beta2 lib/reactor/support/link_matcher.rb
infopark_reactor-1.10.0.beta lib/reactor/support/link_matcher.rb
infopark_reactor-1.9.1 lib/reactor/support/link_matcher.rb
infopark_reactor-1.9.0.beta2 lib/reactor/support/link_matcher.rb
infopark_reactor-1.9.0.beta lib/reactor/support/link_matcher.rb
infopark_reactor-1.8.4 lib/reactor/support/link_matcher.rb