Class: Jav::Fields::KeyValueField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/jav/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, #autocomplete, #block, #computable, #computed, #computed_value, #default, #format_using, #help, #id, #in_sidebar, #index_text_align, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #stacked, #user, #view, #visible

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

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

#assign_value, #component_for_view, #custom?, #custom_name?, #database_id, #default_name, #has_own_panel?, #hidden_in_reflection?, #hydrate, #model_errors, #name, #placeholder, #plural_name, #resolve_attribute, #translation_key, #type, #updatable, #update_using, #value, #view_component_name, #visible?, #visible_in_reflection?

Methods included from FieldExtensions::HasFieldName

#field_name, #field_name=

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on, #show_on_create, #show_on_update, #visible_on?

Methods included from Concerns::IsResourceItem

#is_field?, #is_main_panel?, #is_panel?, #is_row?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?

Constructor Details

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

Returns a new instance of KeyValueField.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jav/fields/key_value_field.rb', line 8

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

  hide_on :index

  @key_label = args[:key_label]
  @value_label = args[:value_label]
  @action_text = args[:action_text]
  @delete_text = args[:delete_text]

  @disable_editing_keys = args[:disable_editing_keys].presence || false
  # disabling editing keys also disables adding rows (doesn't take into account the value of disable_adding_rows)
  @disable_adding_rows = args[:disable_adding_rows].presence || false
  @disable_deleting_rows = args[:disable_deleting_rows].presence || false
end

Instance Attribute Details

#disable_adding_rowsObject (readonly)

Returns the value of attribute disable_adding_rows.



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

def disable_adding_rows
  @disable_adding_rows
end

#disable_editing_keysObject (readonly)

Returns the value of attribute disable_editing_keys.



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

def disable_editing_keys
  @disable_editing_keys
end

Instance Method Details

#action_textObject



36
37
38
39
40
# File 'lib/jav/fields/key_value_field.rb', line 36

def action_text
  return @action_text if @action_text.present?

  I18n.t("jav.key_value_field.add_row")
end

#delete_textObject



42
43
44
45
46
# File 'lib/jav/fields/key_value_field.rb', line 42

def delete_text
  return @delete_text if @delete_text.present?

  I18n.t("jav.key_value_field.delete_row")
end

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



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jav/fields/key_value_field.rb', line 82

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

  model.send(:"#{key}=", new_value)

  model
end

#key_labelObject



24
25
26
27
28
# File 'lib/jav/fields/key_value_field.rb', line 24

def key_label
  return @key_label if @key_label.present?

  I18n.t("jav.key_value_field.key")
end

#optionsObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jav/fields/key_value_field.rb', line 70

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_editing_keys || @disable_deleting_rows
  }
end

#parsed_valueObject



52
53
54
55
56
# File 'lib/jav/fields/key_value_field.rb', line 52

def parsed_value
  value.to_json
rescue StandardError
  {}
end

#to_permitted_paramObject



48
49
50
# File 'lib/jav/fields/key_value_field.rb', line 48

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

#value_labelObject



30
31
32
33
34
# File 'lib/jav/fields/key_value_field.rb', line 30

def value_label
  return @value_label if @value_label.present?

  I18n.t("jav.key_value_field.value")
end