Sha256: d5c8bc29782a243f2fb656e336697195d337c257e842afdeec1df80b3f6c4899

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require_relative "associative"

module Administrate
  module Field
    class Polymorphic < BelongsTo
      def associated_resource_grouped_options
        classes.map do |klass|
          [klass.to_s, candidate_resources_for(klass).map do |resource|
            [display_candidate_resource(resource), resource.to_global_id]
          end]
        end
      end

      def self.permitted_attribute(attr, _options = nil)
        { attr => %i{type value} }
      end

      def permitted_attribute
        { attribute => %i{type value} }
      end

      def selected_global_id
        data ? data.to_global_id : nil
      end

      protected

      def associated_dashboard(klass = data.class)
        "#{klass.name}Dashboard".constantize.new
      end

      def classes
        options.fetch(:classes) || []
      end

      private

      def order
        @_order ||= options.delete(:order)
      end

      def candidate_resources_for(klass)
        order ? klass.order(order) : klass.all
      end

      def display_candidate_resource(resource)
        associated_dashboard(resource.class).display_resource(resource)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
semantic-administrate-0.1.1 lib/administrate/field/polymorphic.rb
administrate-0.9.0 lib/administrate/field/polymorphic.rb