Class: Avo::Fields::HasManyField

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

Instance Attribute Summary

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, #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) ⇒ HasManyField

Returns a new instance of HasManyField.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/avo/app/fields/has_many.rb', line 4

def initialize(name, **args, &block)
  @defaults = {
    updatable: false,
    component: 'has-many-field'
  }

  super(name, **args, &block)

  hide_on :index

  @resource = args[:resource]
end

Instance Method Details

#get_target_resource(model) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/avo/app/fields/has_many.rb', line 38

def get_target_resource(model)
  if @resource.present?
    App.get_resources.find { |r| r.class == @resource }
  else
    class_name = model._reflections[id.to_s].options[:class_name].present? ? model._reflections[id.to_s].options[:class_name] : model._reflections[id.to_s].name
    App.get_resources.find { |r| r.class == "Avo::Resources::#{class_name.to_s.camelcase.singularize}".safe_constantize }
  end
end

#has_own_panel?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/avo/app/fields/has_many.rb', line 34

def has_own_panel?
  true
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/avo/app/fields/has_many.rb', line 17

def hydrate_field(fields, model, resource, view)
  if view === :create
    return {
      relationship: :has_many,
    }
  end

  return {} if [:index].include? view

  target_resource = get_target_resource model
  fields[:relation_class] = target_resource.class.to_s
  fields[:path] = target_resource.url
  fields[:relationship] = :has_many

  fields
end