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.



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

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
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

Instance Method Details

#disallowedObject



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

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

#field_valueObject



20
21
22
23
24
# File 'lib/avo/fields/tags_field.rb', line 20

def field_value
  return json_value if acts_as_taggable_on.present?

  value || []
end

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



34
35
36
37
38
39
40
41
42
# File 'lib/avo/fields/tags_field.rb', line 34

def fill_field(model, key, value, params)
  if acts_as_taggable_on.present?
    model.send(act_as_taggable_attribute(key), parsed_value(value))
  else
    model.send("#{key}=", parsed_value(value))
  end

  model
end

#json_valueObject



26
27
28
29
30
31
32
# File 'lib/avo/fields/tags_field.rb', line 26

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

#suggestionsObject



44
45
46
47
48
49
50
51
52
# File 'lib/avo/fields/tags_field.rb', line 44

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