Sha256: f8d3aac590d78224d8c237ac2db465f37a6081eacf604e822ab68d03b029d127

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

module Para
  module Component
    module BaseDecorator
      include Rails.application.routes.mounted_helpers
      include ActionDispatch::Routing::PolymorphicRoutes

      def path(namespace: nil, **options)
        find_path([:admin, self, namespace].compact, options)
      end

      def relation_path(controller_or_resource, options = {})
        if Hash === controller_or_resource
          options = controller_or_resource
          controller_or_resource = nil
        end

        components = [:admin, self, controller_or_resource].compact
        find_path(components, options)
      end

      private

      # Try to find a polymorphic path for the given arguments
      #
      # If no route exist, we try all the existing engines too
      # This is quite dirty but for now should work as desired
      # The only problem is if we have engines that declare the same routes
      #
      def find_path(path, options)
        safe_polymorphic_path(path, options).tap do |result|
          raise result.class, result.message if Exception === result
        end
      end

      def safe_polymorphic_path(path, options)
        polymorphic_path(path, options)
      rescue => exception
        exception
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
para-0.7.1 app/decorators/para/component/base_decorator.rb
para-0.7.0 app/decorators/para/component/base_decorator.rb
para-0.6.9 app/decorators/para/component/base_decorator.rb
para-0.6.8 app/decorators/para/component/base_decorator.rb
para-0.6.7 app/decorators/para/component/base_decorator.rb