Class: Avo::Fields::BelongsToField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/avo/fields/belongs_to_field.rb

Defined Under Namespace

Classes: EditComponent, IndexComponent, ShowComponent

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, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #updatable, #user, #view, #visible

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?, #has_own_panel?, #hydrate, #model_errors, #placeholder, #resolve_attribute, #translation_key, #type, #view_component_name, #visible?

Methods included from FieldExtensions::HasFieldName

#field_name, #get_field_name

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on

Constructor Details

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

Returns a new instance of BelongsToField.



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

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

  super(id, **args, &block)

  @searchable = args[:searchable] == true
  @polymorphic_as = args[:polymorphic_as]
  @types = args[:types]
  @relation_method = name.to_s.parameterize.underscore
end

Instance Attribute Details

#polymorphic_asObject (readonly)

Returns the value of attribute polymorphic_as.



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

def polymorphic_as
  @polymorphic_as
end

#relation_methodObject (readonly)

Returns the value of attribute relation_method.



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

def relation_method
  @relation_method
end

#searchableObject (readonly)

Returns the value of attribute searchable.



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

def searchable
  @searchable
end

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

#database_id(model) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/avo/fields/belongs_to_field.rb', line 94

def database_id(model)
  # If the field is a polymorphic value, return the polymorphic_type as key and pre-fill the _id in fill_field.
  return "#{polymorphic_as}_type" if polymorphic_as.present?

  foreign_key
rescue
  id
end

#database_valueObject



39
40
41
# File 'lib/avo/fields/belongs_to_field.rb', line 39

def database_value
  target_resource.id
end

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/avo/fields/belongs_to_field.rb', line 75

def fill_field(model, key, value, params)
  return model unless model.methods.include? key.to_sym

  if polymorphic_as.present?
    model.send("#{polymorphic_as}_type=", params["#{polymorphic_as}_type"])

    # If the type is blank, reset the id too.
    if params["#{polymorphic_as}_type"].blank?
      model.send("#{polymorphic_as}_id=", nil)
    else
      model.send("#{polymorphic_as}_id=", params["#{polymorphic_as}_id"])
    end
  else
    model.send("#{key}=", value)
  end

  model
end

#foreign_keyObject



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

def foreign_key
  return polymorphic_as if polymorphic_as.present?

  if @model.present?
    get_model_class(@model).reflections[@relation_method].foreign_key
  elsif @resource.present? && @resource.model_class.reflections[@relation_method].present?
    @resource.model_class.reflections[@relation_method].foreign_key
  end
end

#get_modelObject



123
124
125
126
127
128
129
# File 'lib/avo/fields/belongs_to_field.rb', line 123

def get_model
  return @model if @model.present?

  @resource.model
rescue
  nil
end

#labelObject



63
64
65
# File 'lib/avo/fields/belongs_to_field.rb', line 63

def label
  value.send(target_resource.class.title)
end

#nameObject



131
132
133
134
135
# File 'lib/avo/fields/belongs_to_field.rb', line 131

def name
  return polymorphic_as.to_s.humanize if polymorphic_as.present? && view == :index

  super
end

#optionsObject



24
25
26
27
28
29
30
31
# File 'lib/avo/fields/belongs_to_field.rb', line 24

def options
  ::Avo::Services::AuthorizationService.apply_policy(user, target_resource.class.query_scope).all.map do |model|
    {
      value: model.id,
      label: model.send(target_resource.class.title)
    }
  end
end

#reflection_for_key(key) ⇒ Object



53
54
55
56
57
# File 'lib/avo/fields/belongs_to_field.rb', line 53

def reflection_for_key(key)
  get_model_class(get_model).reflections[key.to_s]
rescue
  nil
end

#relation_model_classObject



59
60
61
# File 'lib/avo/fields/belongs_to_field.rb', line 59

def relation_model_class
  @resource.model_class
end

#target_resourceObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/avo/fields/belongs_to_field.rb', line 103

def target_resource
  if polymorphic_as.present?
    if value.present?
      return App.get_resource_by_model_name(value.class)
    else
      return nil
    end
  end

  reflection_key = polymorphic_as || id

  if @model._reflections[reflection_key.to_s].klass.present?
    App.get_resource_by_model_name @model._reflections[reflection_key.to_s].klass.to_s
  elsif @model._reflections[reflection_key.to_s].options[:class_name].present?
    App.get_resource_by_model_name @model._reflections[reflection_key.to_s].options[:class_name]
  else
    App.get_resource_by_name reflection_key.to_s
  end
end

#to_permitted_paramObject



67
68
69
70
71
72
73
# File 'lib/avo/fields/belongs_to_field.rb', line 67

def to_permitted_param
  if polymorphic_as.present?
    return ["#{polymorphic_as}_type".to_sym, "#{polymorphic_as}_id".to_sym]
  end

  foreign_key.to_sym
end

#valueObject



20
21
22
# File 'lib/avo/fields/belongs_to_field.rb', line 20

def value
  super(polymorphic_as)
end

#values_for_type(type) ⇒ Object



33
34
35
36
37
# File 'lib/avo/fields/belongs_to_field.rb', line 33

def values_for_type(type)
  ::Avo::Services::AuthorizationService.apply_policy(user, type).all.map do |model|
    [model.send(App.get_resource_by_model_name(type).class.title), model.id]
  end
end