Sha256: 506599f6e05f1a0ee522db530e1af243fcdca2d90c3473e563aa844143210438

Contents?: true

Size: 1.7 KB

Versions: 26

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

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

      def self.current_route(context)
        controller = controller(context)
        # Sólo si hay un controller de verdá
        return if controller.instance_of? ApplicationController

        req = controller.request
        Rails.application.routes.recognize_path(req.path, method: req.env['REQUEST_METHOD'])
      end

      def self.namespace(context)
        return Current.namespace if Current.namespace.present?

        route = current_route(context)
        return if route.blank?

        parts = route[:controller].split('/')
        return unless parts.length > 1

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

    def pg_current_route
      NamespaceDeductor.current_route(self)
    end

    def pg_namespace
      NamespaceDeductor.namespace(self)
    end

    def namespaced_path(object, options = {})
      target = [pg_namespace, object]

      if options[:prefix]
        target.prepend options[:prefix]
        options.delete(:prefix)
      end

      if options[:suffix]
        target.append options[:suffix]
        options.delete(:suffix)
      end

      polymorphic_url(target.flatten.compact, options.merge(only_path: true))
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
pg_rails-7.6.17 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.16 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.15 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.14 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.13 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.12 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.11 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.10 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.9 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.8 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.7 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.6 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.5 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.4 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.3 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.2 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.1 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.6.0 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.5.7 pg_engine/app/helpers/pg_engine/route_helper.rb
pg_rails-7.5.6 pg_engine/app/helpers/pg_engine/route_helper.rb