Sha256: ecde01eed28d1ba40fa579e8ea604782aff20f6918a3ba65831de3d43c509962

Contents?: true

Size: 892 Bytes

Versions: 18

Compression:

Stored size: 892 Bytes

Contents

module Avo
  module Fields
    class BooleanField < Field
      def initialize(name, **args, &block)
        @defaults = {
          sortable: true,
          component: 'boolean-field',
          computable: true,
        }

        super(name, **args, &block)

        @true_value = args[:true_value].present? ? args[:true_value] : true
        @false_value = args[:false_value].present? ? args[:false_value] : false
      end

      def hydrate_field(fields, model, resource, view)
        {
          value: resolve_attribute(fields[:value]),
          true_value: @true_value,
          false_value: @false_value,
        }
      end

      def resolve_attribute(value)
        value.present? ? value.in?(truthy_values) : value
      end

      def truthy_values
        ['true', @true_value]
      end

      def falsy_values
        ['false', @false_value]
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
avo-0.4.10 lib/avo/app/fields/boolean_field.rb
avo-0.4.9 lib/avo/app/fields/boolean_field.rb
avo-0.4.8 lib/avo/app/fields/boolean_field.rb
avo-0.4.7 lib/avo/app/fields/boolean_field.rb
avo-0.4.6 lib/avo/app/fields/boolean_field.rb
avo-0.4.5 lib/avo/app/fields/boolean_field.rb
avo-0.4.4 lib/avo/app/fields/boolean_field.rb
avo-0.4.3 lib/avo/app/fields/boolean_field.rb
avo-0.4.2 lib/avo/app/fields/boolean_field.rb
avo-0.4.1 lib/avo/app/fields/boolean_field.rb
avo-0.3.2 lib/avo/app/fields/boolean_field.rb
avo-0.3.1 lib/avo/app/fields/boolean_field.rb
avo-0.2.5 lib/avo/app/fields/boolean_field.rb
avo-0.2.4 lib/avo/app/fields/boolean_field.rb
avo-0.2.3 lib/avo/app/fields/boolean_field.rb
avo-0.2.2 lib/avo/app/fields/boolean_field.rb
avo-0.2.1 lib/avo/app/fields/boolean_field.rb
avo-0.2.0 lib/avo/app/fields/boolean_field.rb