Class: Avo::Fields::SelectField

Inherits:
BaseField show all
Includes:
FieldExtensions::HasIncludeBlank
Defined in:
lib/avo/fields/select_field.rb

Defined Under Namespace

Classes: EditComponent, IndexComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from BaseField

#action, #as_avatar, #autocomplete, #block, #computable, #computed, #computed_value, #copyable, #default, #for_attribute, #for_presentation_only, #format_using, #help, #id, #null_values, #nullable, #panel_name, #readonly, #record, #required, #sortable, #stacked, #summarizable, #user

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

Attributes included from Concerns::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_preview, #show_on_show

Attributes included from Concerns::IsVisible

#visible

Attributes included from Concerns::IsResourceItem

#resource, #view

Instance Method Summary collapse

Methods included from FieldExtensions::HasIncludeBlank

#include_blank

Methods inherited from BaseField

#apply_update_using, #assign_value, #custom?, #custom_name?, #database_id, #default_name, #execute_block, #form_field_label, #has_attribute?, #has_own_panel?, #hidden_in_reflection?, #meta, #name, #options_for_filter, #placeholder, #plural_name, #record_errors, #resolve_attribute, #table_header_label, #translated_name, #translated_plural_name, #translation_key, #type, #updatable, #value, #visible_in_reflection?

Methods included from Concerns::UseViewComponents

#component_for_view, #view_component_name, #view_component_namespace

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::HasHelpers

#helpers

Methods included from Concerns::VisibleInDifferentViews

#except_on, #hide_on, #initialize_views, #only_on, #post_initialize, #show_on, #show_on_create, #show_on_update, #visible_in_view?

Methods included from Concerns::IsVisible

#visible?

Methods included from Concerns::HasItemType

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

Methods included from Concerns::IsResourceItem

#visible?

Methods included from Concerns::Hydration

#hydrate

Constructor Details

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

Returns a new instance of SelectField.



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

def initialize(id, **args, &block)
  args[:placeholder] ||= I18n.t("avo.choose_an_option")

  super(id, **args, &block)

  @options = if args[:options].is_a? Hash
    ActiveSupport::HashWithIndifferentAccess.new args[:options]
  elsif args[:enum].present?
    args[:enum]
  else
    args[:options]
  end

  @enum = args[:enum]
  @multiple = args[:multiple]
  @display_value = args[:display_value] || false
end

Instance Attribute Details

#display_valueObject (readonly)

Returns the value of attribute display_value.



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

def display_value
  @display_value
end

#multipleObject (readonly)

Returns the value of attribute multiple.



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

def multiple
  @multiple
end

Instance Method Details

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



73
74
75
76
77
78
79
# File 'lib/avo/fields/select_field.rb', line 73

def fill_field(record, key, value, params)
  if @multiple
    value = value.reject(&:blank?)
  end

  super
end

#labelObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/avo/fields/select_field.rb', line 43

def label
  return "" if value.nil? || (@multiple && value.empty?)

  # If options are array don't need any pre-process
  if options.is_a?(Array)
    return @multiple ? value.join(", ") : value
  end

  # If options are enum and display_value is true we return the Value of that key-value pair, else return key of that key-value pair
  # WARNING: value here is the DB stored value and not the value of a key-value pair.
  if @enum.present?
    return @enum[value] if display_value
    return value
  end

  # When code arrive here it means options are Hash
  # If display_value is true we only need to return the value stored in DB
  if display_value
    value
  elsif @multiple
    options.select { |_, v| value.include?(v.to_s) }.keys.join(", ")
  else
    options.invert[value]
  end
end

#options_for_selectObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/avo/fields/select_field.rb', line 26

def options_for_select
  # If options are array don't need any pre-process
  return options if options.is_a?(Array)

  # If options are enum we invert the enum if display value, else (see next comment)
  if @enum.present?
    return @enum.invert if display_value

    # We need to use the label attribute as the option value because Rails casts it like that
    return @enum.map { |label, value| [label, label] }.to_h
  end

  # When code arrive here it means options are Hash
  # If display_value is true we only need to return the values of the Hash
  display_value ? options.values : options
end

#to_permitted_paramObject



69
70
71
# File 'lib/avo/fields/select_field.rb', line 69

def to_permitted_param
  @multiple ? {"#{id}": []} : id
end