Sha256: c7ce8838d8bf7d3b0ae3f7885db1ee9bc461462f0d763f82e5f9f5ffb150d569

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

require 'json'

module Avo
  module Fields
    class KeyValueField < BaseField
      def initialize(name, **args, &block)
        @defaults = {
          partial_name: 'key-value-field',
        }

        super(name, **args, &block)

        hide_on :index

        @key_label = args[:key_label].present? ? args[:key_label].to_s : 'Key'
        @value_label = args[:value_label].present? ? args[:value_label].to_s : 'Value'
        @action_text = args[:action_text].present? ? args[:action_text].to_s : 'Add row'
        @delete_text = args[:delete_text].present? ? args[:delete_text].to_s : 'Delete row'

        @disable_editing_keys = args[:disable_editing_keys].present? ? args[:disable_editing_keys] : false
        # disabling editing keys also disables adding rows (doesn't take into account the value of disable_adding_rows)
        if args[:disable_editing_keys].present? && args[:disable_editing_keys] == true
          @disable_adding_rows = true
        elsif args[:disable_adding_rows].present?
          @disable_adding_rows = args[:disable_adding_rows]
        else
          @disable_adding_rows = false
        end
        @disable_deleting_rows = args[:disable_deleting_rows].present? ? args[:disable_deleting_rows] : false
      end

      def to_permitted_param
        [:"#{id}", "#{id}": {} ]
      end

      def hydrate_field(fields, model, resource, view)
        {
          key_label: @key_label,
          value_label: @value_label,
          action_text: @action_text,
          delete_text: @delete_text,
          disable_editing_keys: @disable_editing_keys,
          disable_adding_rows: @disable_adding_rows,
          disable_deleting_rows: @disable_deleting_rows,
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avo-0.5.0.beta9 lib/avo/fields/key_value_field.rb
avo-0.5.0.beta8 lib/avo/fields/key_value_field.rb
avo-0.5.0.beta7 lib/avo/fields/key_value_field.rb
avo-0.5.0.beta6 lib/avo/fields/key_value_field.rb
avo-0.5.0.beta5 lib/avo/fields/key_value_field.rb