Class: Avo::Fields::HasOneField

Inherits:
HasBaseField show all
Defined in:
lib/avo/fields/has_one_field.rb

Defined Under Namespace

Classes: IndexComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from HasBaseField

#display

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, #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 HasBaseField

#target_resource, #turbo_frame

Methods inherited from BaseField

#component_for_view, #custom?, #database_id, #has_own_panel?, #hydrate, #model_errors, #name, #placeholder, #resolve_attribute, #to_permitted_param, #translation_key, #type, #value, #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) ⇒ HasOneField

Returns a new instance of HasOneField.



6
7
8
9
10
11
12
13
14
# File 'lib/avo/fields/has_one_field.rb', line 6

def initialize(id, **args, &block)
  super(id, **args, &block)

  hide_on :new, :edit

  @placeholder ||= I18n.t "avo.choose_an_option"

  @relation_method = name.to_s.parameterize.underscore
end

Instance Attribute Details

#relation_methodObject

Returns the value of attribute relation_method.



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

def relation_method
  @relation_method
end

Instance Method Details

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/avo/fields/has_one_field.rb', line 28

def fill_field(model, key, value, params)
  if value.blank?
    related_model = nil
  else
    related_class = model.class.reflections[name.to_s.downcase].class_name
    related_model = related_class.safe_constantize.find value
  end

  model.public_send("#{key}=", related_model)

  model
end

#frame_urlObject



24
25
26
# File 'lib/avo/fields/has_one_field.rb', line 24

def frame_url
  "#{@resource.record_path}/#{id}/#{value.id}?turbo_frame=#{turbo_frame}"
end

#labelObject



16
17
18
# File 'lib/avo/fields/has_one_field.rb', line 16

def label
  value.send(target_resource.title)
end

#resourceObject



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

def resource
  Avo::App.get_resource_by_model_name @model.class
end