Sha256: f2f42d6db607bdac84ce333fd9d44fbd4024ea3f9a07933302a1d88feda8c467

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

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

module Administrate
  module Field
    class HasMany < Associative
      DEFAULT_LIMIT = 5

      def self.permitted_attribute(attribute)
        { "#{attribute.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.id]
        end
      end

      def selected_options
        data && data.map(&:id)
      end

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

      def permitted_attribute
        self.class.permitted_attribute(attribute)
      end

      def resources
        data.limit(limit)
      end

      def more_than_limit?
        data.count > limit
      end

      private

      def candidate_resources
        associated_class.all
      end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
administrate-0.3.0 lib/administrate/field/has_many.rb
administrate-0.2.2 lib/administrate/field/has_many.rb
administrate-0.2.1 lib/administrate/field/has_many.rb
administrate-0.2.0 lib/administrate/field/has_many.rb
administrate-0.2.0.rc1 lib/administrate/field/has_many.rb
administrate-0.1.5 lib/administrate/field/has_many.rb
administrate-0.1.4 lib/administrate/field/has_many.rb
administrate-0.1.3 lib/administrate/fields/has_many.rb
administrate-0.1.2 lib/administrate/fields/has_many.rb