Sha256: 7a6a0ae658a3c10f74324bd997ef8793e49e4938a10b11ba7454e18ee20c3a19

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module Headmin
  module Filter
    class AssociationView < ViewModel
      def base_options
        keys = %i[name label form]
        options = to_h.slice(*keys)
        default_base_options.merge(options)
      end

      def input_options
        keys = %i[form]
        options = to_h.slice(*keys)
        default_input_options.merge(options)
      end

      def collection
        @collection || association_model.all.map { |record| [record.to_s, record.id] }
      end

      def association_model
        reflection.klass
      end

      private

      def id
        "#{name}_value"
      end

      def name
        @name || attribute
      end

      def label
        @label || I18n.t("attributes.#{attribute}", default: association_model.model_name.human(count: collection? ? 2 : 1))
      end

      def reflection
        form.object.class.reflect_on_association(attribute)
      end

      def collection?
        reflection.collection?
      end

      def default_base_options
        {
          label: label,
          name: attribute,
          display_values: collection,
          filter: Headmin::Filter::Association.new(name, @params),
          allowed_operators: Headmin::Filter::Association::OPERATORS
        }
      end

      def default_input_options
        {
          label: false,
          wrapper: false,
          name: nil,
          id: id,
          data: {
            action: "change->filter#updateHiddenValue",
            filter_target: "value",
            filter_row_target: "original"
          },
          collection: collection,
          selected: selected,
          class: "form-select"
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
headmin-0.5.4 app/models/headmin/filter/association_view.rb