Sha256: 039d565612375643cac21343e35fa211086642dc4309ee04d3e31fa6a36f0912

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require_relative "associative"

module Administrate
  module Field
    class BelongsTo < Associative
      def self.permitted_attribute(attr, options = {})
        resource_class = options[:resource_class]
        if resource_class
          foreign_key_for(resource_class, attr)
        else
          Administrate.warn_of_missing_resource_class
          :"#{attr}_id"
        end
      end

      def self.eager_load?
        true
      end

      def permitted_attribute
        foreign_key
      end

      def associated_resource_options
        candidate_resources.map do |resource|
          [
            display_candidate_resource(resource),
            resource.send(association_primary_key)
          ]
        end
      end

      def selected_option
        data&.send(association_primary_key)
      end

      def include_blank_option
        options.fetch(:include_blank, true)
      end

      private

      def candidate_resources
        scope =
          if options[:scope]
            options[:scope].arity.positive? ? options[:scope].call(self) : options[:scope].call
          else
            associated_class.all
          end

        order = options.delete(:order)
        order ? scope.reorder(order) : scope
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
administrate-1.0.0.beta3 lib/administrate/field/belongs_to.rb
administrate-1.0.0.beta2 lib/administrate/field/belongs_to.rb