Class: Avo::Fields::TagsField

Inherits:
BaseField show all
Defined in:
lib/avo/fields/tags_field.rb

Defined Under Namespace

Classes: EditComponent, IndexComponent, ShowComponent, TagComponent

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, #index_text_align, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #stacked, #updatable, #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

#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, #to_permitted_param, #translation_key, #type, #value, #view_component_name, #visible?, #visible_in_reflection?

Methods included from FieldExtensions::HasFieldName

#field_name, #get_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_sidebar?, #is_tab?, #is_tab_group?, #is_tool?

Constructor Details

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

Returns a new instance of TagsField.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/avo/fields/tags_field.rb', line 10

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

  add_boolean_prop args, :close_on_select
  add_boolean_prop args, :enforce_suggestions
  add_string_prop args, :acts_as_taggable_on
  add_array_prop args, :disallowed
  add_array_prop args, :delimiters, [","]
  add_array_prop args, :suggestions
  add_string_prop args, :mode, nil
  add_string_prop args, :fetch_values_from
end

Instance Attribute Details

#acts_as_taggable_onObject (readonly)

Returns the value of attribute acts_as_taggable_on.



4
5
6
# File 'lib/avo/fields/tags_field.rb', line 4

def acts_as_taggable_on
  @acts_as_taggable_on
end

#close_on_selectObject (readonly)

Returns the value of attribute close_on_select.



5
6
7
# File 'lib/avo/fields/tags_field.rb', line 5

def close_on_select
  @close_on_select
end

#delimitersObject (readonly)

Returns the value of attribute delimiters.



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

def delimiters
  @delimiters
end

#enforce_suggestionsObject (readonly)

Returns the value of attribute enforce_suggestions.



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

def enforce_suggestions
  @enforce_suggestions
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

Instance Method Details

#disallowedObject



64
65
66
67
68
69
70
71
72
# File 'lib/avo/fields/tags_field.rb', line 64

def disallowed
  return @disallowed if @disallowed.is_a? Array

  if @disallowed.respond_to? :call
    return Avo::Hosts::RecordHost.new(block: @disallowed, record: model).handle
  end

  []
end

#fetch_values_fromObject



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

def fetch_values_from
  if @fetch_values_from.respond_to?(:call)
    Avo::Hosts::ResourceRecordHost.new(block: @fetch_values_from, resource: resource, record: model).handle
  else
    @fetch_values_from
  end
end

#field_valueObject



23
24
25
26
27
# File 'lib/avo/fields/tags_field.rb', line 23

def field_value
  return json_value if acts_as_taggable_on.present?

  value || []
end

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/avo/fields/tags_field.rb', line 37

def fill_field(model, key, value, params)
  if acts_as_taggable_on.present?
    model.send(act_as_taggable_attribute(key), value)
  else
    val = if value.is_a?(String)
      value.split(",")
    elsif value.is_a?(Array)
      value
    else
      value
    end
    model.send("#{key}=", val)
  end

  model
end

#json_valueObject



29
30
31
32
33
34
35
# File 'lib/avo/fields/tags_field.rb', line 29

def json_value
  value.map do |item|
    {
      value: item.name
    }
  end.as_json
end

#suggestionsObject



54
55
56
57
58
59
60
61
62
# File 'lib/avo/fields/tags_field.rb', line 54

def suggestions
  return @suggestions if @suggestions.is_a? Array

  if @suggestions.respond_to? :call
    return Avo::Hosts::RecordHost.new(block: @suggestions, record: model).handle
  end

  []
end