Sha256: 52295fae0991777a0bc108640db9855d778be083f00ac07f150c5cf78cbf4404

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Para
  module Component
    module CrudDecorator
      include Para::Component::BaseDecorator

      def path(options = {})
        options[:model] ||= model_singular_route_key
        find_path([:admin, self, :resources], options)
      end

      def relation_path(controller_or_resource, *nested_resources, **options)
        id_key = nested_resources.empty? ? :id : :resource_id

        if (id = extract_id_from(controller_or_resource))
          options[id_key] = id
        end

        route_key = route_key_for(options[id_key], options)
        options[:model] = model_singular_route_key

        data = [:admin, self, route_key].compact + nested_resources

        polymorphic_path(data, options)
      end

      private

      def extract_id_from(object)
        object.id if object.respond_to?(:persisted?) && object.persisted?
      end

      def route_key_for(id, options)
        if id || options[:action].presence && options[:action].to_sym == :new
          :resource
        else
          :resources
        end
      end

      def model_singular_route_key
        @model_singular_route_key ||= model.model_name.singular_route_key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
para-0.5.3 app/decorators/para/component/crud_decorator.rb