Sha256: 249be602d3049941b96afd055cd51bf9a70569c9b0b9f4fffadb0a579cee0c35

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module PgEngine
  module RouteHelper
    class NamespaceDeductor
      def self.request(context)
        if context.respond_to?(:request) && context.request
          # Controllers
          context.request
        elsif context.respond_to?(:helpers) && context.helpers
          # Decorators
          context.helpers.request
        elsif context.respond_to?(:template) && context.template
          # FormBuilders
          context.template.request
        end
      end

      def self.namespace(context)
        req = request(context)
        route = Rails.application.routes.recognize_path(req.path)
        parts = route[:controller].split('/')
        return unless parts.length > 1

        parts.first.to_sym
      rescue ActionController::RoutingError
        nil
      end
    end

    def pg_namespace
      NamespaceDeductor.namespace(self)
    end

    def namespaced_path(object, prefix: nil, suffix: nil)
      target = [pg_namespace, object]
      target.prepend prefix if prefix
      target.append suffix if suffix
      target = target.flatten.compact
      polymorphic_url(target, only_path: true)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pg_rails-7.0.8.pre.alpha pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.0.7 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.0.6 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.0.5 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.0.4 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.0.3 pg_engine/app/helpers/pg_engine/route_helper.rb