Class: Avo::Fields::FrameBaseField

Inherits:
BaseField
  • Object
show all
Includes:
Concerns::FrameLoadingMode, Concerns::HasDescription, Concerns::LinkableTitle, Concerns::ReloadIcon, Concerns::UseResource
Defined in:
lib/avo/fields/frame_base_field.rb

Direct Known Subclasses

HasOneField, ManyFrameBaseField

Instance Attribute Summary

Attributes inherited from BaseField

#action, #autocomplete, #block, #computable, #computed, #computed_value, #copyable, #default, #for_attribute, #for_presentation_only, #format_display_using, #format_edit_using, #format_form_using, #format_index_using, #format_new_using, #format_show_using, #format_using, #help, #id, #label_help, #null_values, #nullable, #panel_name, #readonly, #record, #required, #size, #sortable, #stacked, #summarizable, #user, #width

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

Attributes included from Concerns::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_preview, #show_on_show

Attributes included from Concerns::IsVisible

#visible

Attributes included from Concerns::IsResourceItem

#resource, #view

Instance Method Summary collapse

Methods included from Concerns::FrameLoadingMode

#auto_load_for, #default_manual_auto_load_for, #lazy_loading_mode?, #loading_mode, #manual?

Methods included from Concerns::HasDescription

#description, #discreet_description

Methods included from Concerns::LinkableTitle

#linkable?

Methods included from Concerns::ReloadIcon

#reloadable?

Methods included from Concerns::UseResource

#use_resource

Methods inherited from BaseField

#apply_update_using, #assign_value, #attribute_id, #custom?, #custom_name?, #database_id, #execute_context, #fill_field, #form_field_label, #has_attribute?, #hidden_in_reflection?, #meta, #name, #name_override, #options_for_filter, #parent, #plural_name, #record_errors, #resolve_attribute, #table_header_class, #table_header_label, #to_permitted_param, #translated_name, #translated_plural_name, #translation_key, #type, #updatable, #value, #width_class, #width_option

Methods included from Concerns::DomId

#model_name, #to_key

Methods included from Concerns::UseViewComponents

#view_component_name, #view_component_namespace

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::HasHelpers

#helpers

Methods included from Concerns::VisibleInDifferentViews

#except_on, #hide_on, #initialize_views, #only_on, #post_initialize, #show_on, #show_on_create, #show_on_update, #visible_in_view?

Methods included from Concerns::IsVisible

#visible?

Methods included from Concerns::HasItemType

#is_card?, #is_collaboration?, #is_field?, #is_header?, #is_heading?, #is_panel?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?

Methods included from Concerns::IsResourceItem

#visible?

Methods included from Concerns::Hydration

#hydrate

Constructor Details

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

Returns a new instance of FrameBaseField.



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

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

  @use_resource = args[:use_resource]
  @reloadable = args[:reloadable]
  @linkable = args[:linkable]
  @description = args[:description]
  @loading = args[:loading]
end

Instance Method Details

#association_nameObject



105
106
107
# File 'lib/avo/fields/frame_base_field.rb', line 105

def association_name
  @association_name ||= attribute_id.to_s
end

#authorized?Boolean

Returns:

  • (Boolean)


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

def authorized?
  return true unless Avo.configuration.authorization_enabled?

  method = :"view_#{id}?"
  service = field_resource.authorization

  if service.has_method? method
    service.authorize_action(method, raise_exception: false)
  else
    !Avo.configuration.explicit_authorization
  end
end

#component_for_view(view = Avo::ViewInquirer.new("index")) ⇒ Object

Adds the view override component has_one, has_many, has_and_belongs_to_many fields don’t have edit views



82
83
84
85
86
# File 'lib/avo/fields/frame_base_field.rb', line 82

def component_for_view(view = Avo::ViewInquirer.new("index"))
  view = Avo::ViewInquirer.new("show") if view.in? %w[new create update edit]

  super
end

#default_nameObject



101
102
103
# File 'lib/avo/fields/frame_base_field.rb', line 101

def default_name
  use_resource&.name || super
end

#field_labelObject

What the user sees in the text field



50
51
52
53
54
# File 'lib/avo/fields/frame_base_field.rb', line 50

def field_label
  target_resource.new(record: value, view: view, user: user).record_title
rescue
  nil
end

#field_resourceObject



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

def field_resource
  resource || get_resource_by_model_class(@record.class)
end

#field_valueObject

The value



43
44
45
46
47
# File 'lib/avo/fields/frame_base_field.rb', line 43

def field_value
  value.send(database_value)
rescue
  nil
end

#frame_loading_defaultsObject

Association frames inherit their cold-start defaults (‘loading`, `auto_load_for`) from `config.associations = {…}` when the field itself doesn’t pass ‘loading:`. See Avo::Concerns::FrameLoadingMode.



38
39
40
# File 'lib/avo/fields/frame_base_field.rb', line 38

def frame_loading_defaults
  Avo.configuration.associations.fetch(:frames, {})
end

#frame_url(add_turbo_frame: true) ⇒ Object



28
29
30
31
32
33
# File 'lib/avo/fields/frame_base_field.rb', line 28

def frame_url(add_turbo_frame: true)
  Avo::Services::URIService.parse(field_resource.record_path)
    .append_path(id.to_s)
    .append_query(query_params(add_turbo_frame:))
    .to_s
end

#has_own_panel?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/avo/fields/frame_base_field.rb', line 72

def has_own_panel?
  true
end

#placeholderObject



68
69
70
# File 'lib/avo/fields/frame_base_field.rb', line 68

def placeholder
  @placeholder || I18n.t("avo.choose_an_option")
end

#query_params(add_turbo_frame: true) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/avo/fields/frame_base_field.rb', line 109

def query_params(add_turbo_frame: true)
  {
    view:,
    for_attribute: @for_attribute,
    turbo_frame: add_turbo_frame ? turbo_frame : nil
  }.compact
end

#resource_class(params) ⇒ Object



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

def resource_class(params)
  return use_resource if use_resource.present?

  return Avo.resource_manager.get_resource_by_name @id.to_s if @array

  reflection = @record.class.reflect_on_association(@for_attribute || params[:related_name])

  reflected_model = reflection.klass

  Avo.resource_manager.get_resource_by_model_class reflected_model
end

#target_resourceObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/avo/fields/frame_base_field.rb', line 56

def target_resource
  reflection = @record.class.reflect_on_association(association_name)

  if reflection.klass.present?
    get_resource_by_model_class(reflection.klass.to_s)
  elsif reflection.options[:class_name].present?
    get_resource_by_model_class(reflection.options[:class_name])
  else
    Avo.resource_manager.get_resource_by_name association_name
  end
end

#turbo_frameObject



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

def turbo_frame
  "#{self.class.name.demodulize.to_s.underscore}_show_#{@id}"
end

#visible_in_reflection?(reflection = nil) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/avo/fields/frame_base_field.rb', line 76

def visible_in_reflection?(reflection = nil)
  false
end