Class: Avo::Fields::BelongsToField

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

Overview

Requirements

  • list

  • ID

  • label

  • Type

  • foreign_key

  • foreign_key for poly type

  • foreign_key for poly id

  • is_disabled?

Defined Under Namespace

Classes: AutocompleteComponent, 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, #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?, #default_name, #has_own_panel?, #hidden_in_reflection?, #hydrate, #model_errors, #placeholder, #plural_name, #resolve_attribute, #translation_key, #type, #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) ⇒ BelongsToField

Returns a new instance of BelongsToField.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/avo/fields/belongs_to_field.rb', line 69

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 = id.to_s.parameterize.underscore
  @allow_via_detaching = args[:allow_via_detaching] == true
  @attach_scope = args[:attach_scope]
  @polymorphic_help = args[:polymorphic_help]
  @target = args[:target]
end

Instance Attribute Details

#allow_via_detachingObject (readonly)

Returns the value of attribute allow_via_detaching.



65
66
67
# File 'lib/avo/fields/belongs_to_field.rb', line 65

def allow_via_detaching
  @allow_via_detaching
end

#attach_scopeObject (readonly)

Returns the value of attribute attach_scope.



66
67
68
# File 'lib/avo/fields/belongs_to_field.rb', line 66

def attach_scope
  @attach_scope
end

#polymorphic_asObject (readonly)

Returns the value of attribute polymorphic_as.



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

def polymorphic_as
  @polymorphic_as
end

#polymorphic_helpObject (readonly)

Returns the value of attribute polymorphic_help.



67
68
69
# File 'lib/avo/fields/belongs_to_field.rb', line 67

def polymorphic_help
  @polymorphic_help
end

#relation_methodObject (readonly)

Returns the value of attribute relation_method.



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

def relation_method
  @relation_method
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

#typesObject (readonly)

for Polymorphic associations



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

def types
  @types
end

Instance Method Details

#database_idObject



215
216
217
218
219
220
221
222
# File 'lib/avo/fields/belongs_to_field.rb', line 215

def database_id
  # 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



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

def database_value
  target_resource.id
rescue
  nil
end

#field_labelObject

What the user sees in the text field



106
107
108
109
110
# File 'lib/avo/fields/belongs_to_field.rb', line 106

def field_label
  value.send(target_resource.class.title)
rescue
  nil
end

#field_valueObject

The value



99
100
101
102
103
# File 'lib/avo/fields/belongs_to_field.rb', line 99

def field_value
  value.send(database_value)
rescue
  nil
end

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/avo/fields/belongs_to_field.rb', line 196

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



157
158
159
160
161
162
163
164
165
# File 'lib/avo/fields/belongs_to_field.rb', line 157

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



244
245
246
247
248
249
250
# File 'lib/avo/fields/belongs_to_field.rb', line 244

def get_model
  return @model if @model.present?

  @resource.model
rescue
  nil
end

#id_input_foreign_keyObject



143
144
145
146
147
148
149
# File 'lib/avo/fields/belongs_to_field.rb', line 143

def id_input_foreign_key
  if is_polymorphic?
    "#{foreign_key}_id"
  else
    foreign_key
  end
end

#is_polymorphic?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/avo/fields/belongs_to_field.rb', line 151

def is_polymorphic?
  polymorphic_as.present?
rescue
  false
end

#labelObject



184
185
186
# File 'lib/avo/fields/belongs_to_field.rb', line 184

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

#nameObject



252
253
254
255
256
# File 'lib/avo/fields/belongs_to_field.rb', line 252

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

  super
end

#optionsObject



112
113
114
# File 'lib/avo/fields/belongs_to_field.rb', line 112

def options
  values_for_type
end

#reflectionObject

Get the model reflection instance



174
175
176
177
178
# File 'lib/avo/fields/belongs_to_field.rb', line 174

def reflection
  reflection_for_key(id)
rescue
  nil
end

#reflection_for_key(key) ⇒ Object



167
168
169
170
171
# File 'lib/avo/fields/belongs_to_field.rb', line 167

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

#relation_model_classObject



180
181
182
# File 'lib/avo/fields/belongs_to_field.rb', line 180

def relation_model_class
  @resource.model_class
end

#searchableObject



84
85
86
# File 'lib/avo/fields/belongs_to_field.rb', line 84

def searchable
  @searchable && ::Avo::App.license.has_with_trial(:searchable_associations)
end

#target_resourceObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/avo/fields/belongs_to_field.rb', line 224

def target_resource
  if is_polymorphic?
    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



188
189
190
191
192
193
194
# File 'lib/avo/fields/belongs_to_field.rb', line 188

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

#type_input_foreign_keyObject



137
138
139
140
141
# File 'lib/avo/fields/belongs_to_field.rb', line 137

def type_input_foreign_key
  if is_polymorphic?
    "#{foreign_key}_type"
  end
end

#valueObject



88
89
90
91
92
93
94
95
96
# File 'lib/avo/fields/belongs_to_field.rb', line 88

def value
  if is_polymorphic?
    # Get the value from the pre-filled assoociation record
    super(polymorphic_as)
  else
    # Get the value from the pre-filled assoociation record
    super(relation_method)
  end
end

#values_for_type(model = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/avo/fields/belongs_to_field.rb', line 116

def values_for_type(model = nil)
  resource = target_resource
  resource = App.get_resource_by_model_name model if model.present?

  query = resource.class.query_scope

  if attach_scope.present?
    query = Avo::Hosts::AssociationScopeHost.new(block: attach_scope, query: query, parent: get_model).handle
  end

  query.all.map do |model|
    [model.send(resource.class.title), model.id]
  end
end