Sha256: fa962483c40e413dde6efadf6017f85c2d3375dc911d0be316c3891397ee0624

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

module Headmin
  module Filter
    class BooleanView < 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

      private

      def value
        # If the value is nil we want to display the first option (which is true)
        @value.nil? ? @value = true : ""
      end

      def id
        "#{name}_value"
      end

      def name
        @name || attribute
      end

      def label
        @label || I18n.t("attributes.#{attribute}", default: name.to_s)
      end

      def default_base_options
        {
          label: label,
          name: attribute,
          filter: Headmin::Filter::Boolean.new(name, @params),
          display_values: [[I18n.t("headmin.filters.values.true"), true], [I18n.t("headmin.filters.values.false"), false]],
          allowed_operators: Headmin::Filter::Boolean::OPERATORS
        }
      end

      def default_input_options
        {
          label: false,
          wrapper: false,
          id: id,
          collection: [[I18n.t("headmin.filters.values.true"), 1], [I18n.t("headmin.filters.values.false"), 0]],
          selected: value ? 1 : 0,
          data: {
            action: "change->filter#updateHiddenValue",
            filter_target: "value",
            filter_row_target: "original"
          }
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
headmin-0.5.3 app/models/headmin/filter/boolean_view.rb
headmin-0.5.2 app/models/headmin/filter/boolean_view.rb
headmin-0.5.1 app/models/headmin/filter/boolean_view.rb
headmin-0.5.0 app/models/headmin/filter/boolean_view.rb