Class: Avo::Fields::KeyValueField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/avo/fields/key_value_field.rb

Defined Under Namespace

Classes: EditComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from BaseField

#action, #as_avatar, #as_description, #as_label, #block, #computable, #computed, #computed_value, #default, #format_using, #help, #id, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #updatable, #user, #view, #visible

Attributes included from FieldExtensions::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_show

Instance Method Summary collapse

Methods inherited from BaseField

#component_for_view, #custom?, #database_id, #has_own_panel?, #hydrate, #model_errors, #name, #placeholder, #resolve_attribute, #translation_key, #type, #value, #view_component_name, #visible?

Methods included from FieldExtensions::HasFieldName

#field_name, #get_field_name

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on

Constructor Details

#initialize(id, **args, &block) ⇒ KeyValueField

Returns a new instance of KeyValueField.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/avo/fields/key_value_field.rb', line 13

def initialize(id, **args, &block)
  super(id, **args, &block)

  hide_on :index

  @key_label = args[:key_label].present? ? args[:key_label].to_s : I18n.translate('avo.key_value_field.key')
  @value_label = args[:value_label].present? ? args[:value_label].to_s : I18n.translate('avo.key_value_field.value')
  @action_text = args[:action_text].present? ? args[:action_text].to_s : I18n.translate('avo.key_value_field.add_row')
  @delete_text = args[:delete_text].present? ? args[:delete_text].to_s : I18n.translate('avo.key_value_field.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)
  @disable_adding_rows = if args[:disable_editing_keys].present? && args[:disable_editing_keys] == true
    true
  elsif args[:disable_adding_rows].present?
    args[:disable_adding_rows]
  else
    false
  end
  @disable_deleting_rows = args[:disable_deleting_rows].present? ? args[:disable_deleting_rows] : false
end

Instance Attribute Details

#action_textObject (readonly)

Returns the value of attribute action_text.



8
9
10
# File 'lib/avo/fields/key_value_field.rb', line 8

def action_text
  @action_text
end

#delete_textObject (readonly)

Returns the value of attribute delete_text.



9
10
11
# File 'lib/avo/fields/key_value_field.rb', line 9

def delete_text
  @delete_text
end

#disable_adding_rowsObject (readonly)

Returns the value of attribute disable_adding_rows.



11
12
13
# File 'lib/avo/fields/key_value_field.rb', line 11

def disable_adding_rows
  @disable_adding_rows
end

#disable_editing_keysObject (readonly)

Returns the value of attribute disable_editing_keys.



10
11
12
# File 'lib/avo/fields/key_value_field.rb', line 10

def disable_editing_keys
  @disable_editing_keys
end

#key_labelObject (readonly)

Returns the value of attribute key_label.



6
7
8
# File 'lib/avo/fields/key_value_field.rb', line 6

def key_label
  @key_label
end

#value_labelObject (readonly)

Returns the value of attribute value_label.



7
8
9
# File 'lib/avo/fields/key_value_field.rb', line 7

def value_label
  @value_label
end

Instance Method Details

#fill_field(model, key, value, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/avo/fields/key_value_field.rb', line 57

def fill_field(model, key, value, params)
  begin
    new_value = JSON.parse(value)
  rescue
    new_value = {}
  end

  model[key] = new_value

  model
end

#optionsObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/avo/fields/key_value_field.rb', line 45

def options
  {
    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

#parsed_valueObject



39
40
41
42
43
# File 'lib/avo/fields/key_value_field.rb', line 39

def parsed_value
  value.to_json
rescue
  {}
end

#to_permitted_paramObject



35
36
37
# File 'lib/avo/fields/key_value_field.rb', line 35

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