Sha256: 2d7a8a404df3ba1eb5ca38dd91521e9ea94fb8638d9ff3ca97d213ba7df76c21

Contents?: true

Size: 1.68 KB

Versions: 36

Compression:

Stored size: 1.68 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, *nested_resources, **options)
        nested = nested_resources.any?

        if controller_or_resource.is_a?(Hash)
          options = controller_or_resource
          controller_or_resource = nil
        end

        controller_or_resource = nil if controller_or_resource.is_a?(ActiveRecord::Base)

        options[:action] = action_option_for(options, nested: nested)
        components = [:admin, self, controller_or_resource, *nested_resources].compact

        find_path(components, options)
      end

      def page_container_class
        'col-xs-12'
      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 if result.is_a?(Exception)
        end
      end

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

      def action_option_for(options, nested: false)
        if !nested && options[:action].try(:to_sym) == :show
          nil
        else
          options[:action]
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
para-0.12.4 app/decorators/para/component/base_decorator.rb
para-0.12.3 app/decorators/para/component/base_decorator.rb
para-0.12.2 app/decorators/para/component/base_decorator.rb
para-0.12.1 app/decorators/para/component/base_decorator.rb
para-0.12.0 app/decorators/para/component/base_decorator.rb
para-0.11.4 app/decorators/para/component/base_decorator.rb
para-0.11.3 app/decorators/para/component/base_decorator.rb
para-0.11.2 app/decorators/para/component/base_decorator.rb
para-0.11.1 app/decorators/para/component/base_decorator.rb
para-0.11.0 app/decorators/para/component/base_decorator.rb
para-0.10.0 app/decorators/para/component/base_decorator.rb
para-0.9.4 app/decorators/para/component/base_decorator.rb
para-0.9.3.3 app/decorators/para/component/base_decorator.rb
para-0.9.3.2 app/decorators/para/component/base_decorator.rb
para-0.9.3.1 app/decorators/para/component/base_decorator.rb
para-0.9.2 app/decorators/para/component/base_decorator.rb
para-0.9.0 app/decorators/para/component/base_decorator.rb
para-0.8.15 app/decorators/para/component/base_decorator.rb
para-0.8.14 app/decorators/para/component/base_decorator.rb
para-0.8.13 app/decorators/para/component/base_decorator.rb