Class: Avo::Fields::BelongsToField

Inherits:
Field
  • Object
show all
Defined in:
lib/avo/app/fields/belongs_to.rb

Instance Attribute Summary collapse

Attributes inherited from Field

#block, #component, #computable, #default, #format_using, #help, #id, #is_array_param, #is_object_param, #name, #null_values, #nullable, #placeholder, #readonly, #required, #sortable, #updatable

Attributes included from FieldExtensions::VisibleOnDifferentViews

#show_on_create, #show_on_edit, #show_on_index, #show_on_show

Instance Method Summary collapse

Methods inherited from Field

#database_id, #fetch_for_action, #fetch_for_resource, #fill_field, #has_own_panel?, #resolve_attribute

Methods included from FieldExtensions::HasFieldName

#field_name, #field_name_attribute, #get_field_name

Methods included from FieldExtensions::VisibleOnDifferentViews

#except_on, #hide_on, #only_on, #show_on

Constructor Details

#initialize(name, **args, &block) ⇒ BelongsToField

Returns a new instance of BelongsToField.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/avo/app/fields/belongs_to.rb', line 7

def initialize(name, **args, &block)
  @defaults = {
    component: 'belongs-to-field',
    placeholder: "Choose #{name.downcase}",
  }

  @searchable = args[:searchable] == true ? true : false
  @relation_method = name.to_s.parameterize.underscore

  super(name, **args, &block)
end

Instance Attribute Details

#relation_methodObject

Returns the value of attribute relation_method.



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

def relation_method
  @relation_method
end

#searchableObject

Returns the value of attribute searchable.



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

def searchable
  @searchable
end

Instance Method Details

#foreign_key(model) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/avo/app/fields/belongs_to.rb', line 57

def foreign_key(model)
  if model.class == Class
    model.reflections[@relation_method].foreign_key
  else
    model.class.reflections[@relation_method].foreign_key
  end
end

#hydrate_field(fields, model, resource, view) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/avo/app/fields/belongs_to.rb', line 19

def hydrate_field(fields, model, resource, view)
  return fields if model_or_class(model) == 'class'

  fields[:searchable] = @searchable
  fields[:is_relation] = true
  fields[:database_id] = foreign_key model
  target_resource = App.get_resources.find { |r| r.class == "Avo::Resources::#{name}".safe_constantize }

  relation_model = model.public_send(@relation_method)

  if relation_model.present?
    relation_model = model.public_send(@relation_method)
    fields[:value] = relation_model.send(target_resource.title) if relation_model.present?
    fields[:database_value] = relation_model[:id] if relation_model.present?
    fields[:link] = Avo::Resources::Resource.show_path(relation_model)
  end

  # Populate the options on show and edit
  fields[:options] = []

  if [:edit, :create].include? view
    if self.searchable
      fields[:model] = relation_model
    else
      fields[:options] = target_resource.model.all.map do |model|
        {
          value: model.id,
          label: model.send(target_resource.title)
        }
      end
    end
  end

  fields[:resource_name_plural] = target_resource.resource_name_plural

  fields
end