Sha256: 2030ffe4e2887a1f8181b0263a2cc1fb3b95c100e5b05a477fb495a080c0316e

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module ActiveAdmin
  module Inputs
    module Filters
      class AjaxSelectInput < SelectInput
        DEFAULT_LIMIT = 5

        def pluck_column
          klass.reorder("#{method} asc").limit(collection_limit).uniq.pluck(method)
        end

        def collection_from_association
          super.limit(collection_limit)
        end

        def input_html_options
          super.merge(
            'data-limit' => collection_limit,
            'data-value-field' => value_field,
            'data-search-fields' => search_fields,
            'data-ordering' => ordering,
            'data-ransack' => ransack,
            'data-selected-value' => selected_value,
            'data-url' => url,
          )
        end

        def ajax_data
          options[:data] || {}
        end

        def collection_limit
          ajax_data[:limit] || DEFAULT_LIMIT
        end

        def value_field
          ajax_data[:value_field] || :id
        end

        def search_fields
          ajax_data[:search_fields] || raise(ArgumentError, 'search_fields in required')
        end

        def ordering
          ajax_data[:ordering] || "#{search_fields.first} ASC"
        end

        def ransack
          ajax_data[:ransack] || "#{search_fields.join('_or_')}_cont"
        end

        def url
          ajax_data[:url] || "#{method.to_s.pluralize}/filter"
        end

        def selected_value
          if @object
            @object.try(:send, input_name)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeadmin-ajax_filter-0.2.2 lib/active_admin/inputs/filters/ajax_select_input.rb