Sha256: 1635b425fd1a916212b44db0ac08570127f84dadf99b6be5c84955844bd882a5

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require_relative "associative"
require "administrate/page/collection"
require "administrate/order"

module Administrate
  module Field
    class HasMany < Associative
      DEFAULT_LIMIT = 5

      def self.permitted_attribute(attr, _options = nil)
        { "#{attr.to_s.singularize}_ids".to_sym => [] }
      end

      def associated_collection
        Administrate::Page::Collection.new(associated_dashboard)
      end

      def attribute_key
        permitted_attribute.keys.first
      end

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

      def selected_options
        return if data.empty?

        data.map { |object| object.send(primary_key) }
      end

      def limit
        options.fetch(:limit, DEFAULT_LIMIT)
      end

      def permitted_attribute
        self.class.permitted_attribute(attribute)
      end

      def resources(page = 1)
        resources = order.apply(data).page(page).per(limit)
        includes.any? ? resources.includes(*includes) : resources
      end

      def more_than_limit?
        data.count(:all) > limit
      end

      def data
        @data ||= associated_class.none 
      end

      private

      def includes
        associated_dashboard.association_includes
      end

      def candidate_resources
        if options.key?(:includes)
          includes = options.fetch(:includes)
          associated_class.includes(*includes).all
        else
          associated_class.all
        end
      end

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

      def order
        @_order ||= Administrate::Order.new(sort_by, direction)
      end

      def sort_by
        options[:sort_by]
      end

      def direction
        options[:direction]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

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