Sha256: 5033742436a15977ac707d9297cf0d59b0a567010f59018f6717f939b4c6abe6

Contents?: true

Size: 1.62 KB

Versions: 47

Compression:

Stored size: 1.62 KB

Contents

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

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

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

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

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

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

        options[:action] = action_option_for(options, nested: nested)

        find_path(data, options)
      end

      def page_container_class
        history? ? 'col-md-8' : super
      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

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

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
para-0.7.3.p5 app/decorators/para/component/crud_decorator.rb
para-0.7.3.p4 app/decorators/para/component/crud_decorator.rb
para-0.7.3.p3 app/decorators/para/component/crud_decorator.rb
para-0.7.3.p2 app/decorators/para/component/crud_decorator.rb
para-0.7.3.p1 app/decorators/para/component/crud_decorator.rb
para-0.7.3 app/decorators/para/component/crud_decorator.rb
para-0.7.2 app/decorators/para/component/crud_decorator.rb