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

#attach_scope, #description, #discreet_pagination, #display, #hide_search_input, #link_to_child_resource, #scope

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, #sortable, #stacked, #updatable, #user, #view, #visible

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 HasBaseField

#authorized?, #component_for_view, #default_name, #field_label, #field_value, #has_own_panel?, #placeholder, #searchable, #target_resource, #turbo_frame, #use_resource, #visible_in_reflection?

Methods inherited from BaseField

#component_for_view, #custom?, #custom_name?, #database_id, #default_name, #has_own_panel?, #hidden_in_reflection?, #hydrate, #model_errors, #name, #placeholder, #plural_name, #resolve_attribute, #to_permitted_param, #translation_key, #type, #value, #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::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) ⇒ 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)
  hide_on :new, :edit

  super(id, **args, &block)

  @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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/avo/fields/has_one_field.rb', line 31

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
27
28
29
# File 'lib/avo/fields/has_one_field.rb', line 24

def frame_url
  Avo::Services::URIService.parse(@resource.record_path)
    .append_paths(id, value.id)
    .append_query(turbo_frame: turbo_frame)
    .to_s
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