Class: Avo::Fields::HasBaseField

Inherits:
BaseField show all
Includes:
Concerns::IsSearchable, Concerns::LinkableTitle, Concerns::ReloadIcon, Concerns::UseResource
Defined in:
lib/avo/fields/has_base_field.rb

Instance Attribute Summary collapse

Attributes inherited from BaseField

#action, #as_avatar, #autocomplete, #block, #computable, #computed, #computed_value, #default, #for_attribute, #for_presentation_only, #format_using, #help, #id, #null_values, #nullable, #panel_name, #readonly, #record, #required, #sortable, #stacked, #summarizable, #user

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::LinkableTitle

#linkable?

Methods included from Concerns::ReloadIcon

#reloadable?

Methods included from Concerns::UseResource

#use_resource

Methods included from Concerns::IsSearchable

#is_searchable?

Methods inherited from BaseField

#assign_value, #custom?, #custom_name?, #database_id, #execute_block, #fill_field, #form_field_label, #has_attribute?, #hidden_in_reflection?, #name, #options_for_filter, #plural_name, #record_errors, #resolve_attribute, #table_header_label, #to_permitted_param, #translated_name, #translated_plural_name, #translation_key, #type, #updatable, #value

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_field?, #is_heading?, #is_main_panel?, #is_panel?, #is_row?, #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) ⇒ HasBaseField

Returns a new instance of HasBaseField.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/avo/fields/has_base_field.rb', line 17

def initialize(id, **args, &block)
  super(id, **args, &block)
  @scope = args[:scope].present? ? args[:scope] : nil
  @attach_scope = args[:attach_scope].present? ? args[:attach_scope] : nil
  @display = args[:display].present? ? args[:display] : :show
  @searchable = args[:searchable] == true
  @hide_search_input = args[:hide_search_input] || false
  @description = args[:description]
  @use_resource = args[:use_resource] || nil
  @discreet_pagination = args[:discreet_pagination] || false
  @link_to_child_resource = args[:link_to_child_resource] || false
  @reloadable = args[:reloadable].present? ? args[:reloadable] : false
  @linkable = args[:linkable].present? ? args[:linkable] : false
end

Instance Attribute Details

#attach_scopeObject

Returns the value of attribute attach_scope.



11
12
13
# File 'lib/avo/fields/has_base_field.rb', line 11

def attach_scope
  @attach_scope
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/avo/fields/has_base_field.rb', line 12

def description
  @description
end

#discreet_paginationObject

Returns the value of attribute discreet_pagination.



13
14
15
# File 'lib/avo/fields/has_base_field.rb', line 13

def discreet_pagination
  @discreet_pagination
end

#displayObject

Returns the value of attribute display.



9
10
11
# File 'lib/avo/fields/has_base_field.rb', line 9

def display
  @display
end

#hide_search_inputObject

Returns the value of attribute hide_search_input.



14
15
16
# File 'lib/avo/fields/has_base_field.rb', line 14

def hide_search_input
  @hide_search_input
end

Returns the value of attribute link_to_child_resource.



15
16
17
# File 'lib/avo/fields/has_base_field.rb', line 15

def link_to_child_resource
  @link_to_child_resource
end

#scopeObject

Returns the value of attribute scope.



10
11
12
# File 'lib/avo/fields/has_base_field.rb', line 10

def scope
  @scope
end

Instance Method Details

#association_nameObject



106
107
108
# File 'lib/avo/fields/has_base_field.rb', line 106

def association_name
  @association_name ||= (@for_attribute || id).to_s
end

#authorized?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
# File 'lib/avo/fields/has_base_field.rb', line 91

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

  if service.has_method? method
    service.authorize_action(method, raise_exception: false)
  else
    true
  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



85
86
87
88
89
# File 'lib/avo/fields/has_base_field.rb', line 85

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

  super(view)
end

#default_nameObject



102
103
104
# File 'lib/avo/fields/has_base_field.rb', line 102

def default_name
  use_resource&.name || super
end

#field_labelObject

What the user sees in the text field



55
56
57
58
59
# File 'lib/avo/fields/has_base_field.rb', line 55

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

#field_resourceObject



32
33
34
# File 'lib/avo/fields/has_base_field.rb', line 32

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

#field_valueObject

The value



48
49
50
51
52
# File 'lib/avo/fields/has_base_field.rb', line 48

def field_value
  value.send(database_value)
rescue
  nil
end

#frame_url(add_turbo_frame: true) ⇒ Object



40
41
42
43
44
45
# File 'lib/avo/fields/has_base_field.rb', line 40

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)


75
76
77
# File 'lib/avo/fields/has_base_field.rb', line 75

def has_own_panel?
  true
end

#placeholderObject



71
72
73
# File 'lib/avo/fields/has_base_field.rb', line 71

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

#query_params(add_turbo_frame: true) ⇒ Object



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

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

#target_resourceObject



61
62
63
64
65
66
67
68
69
# File 'lib/avo/fields/has_base_field.rb', line 61

def target_resource
  if @record._reflections[(@for_attribute || id).to_s].klass.present?
    get_resource_by_model_class(@record._reflections[association_name].klass.to_s)
  elsif @record._reflections[association_name].options[:class_name].present?
    get_resource_by_model_class(@record._reflections[association_name].options[:class_name])
  else
    Avo.resource_manager.get_resource_by_name association_name
  end
end

#turbo_frameObject



36
37
38
# File 'lib/avo/fields/has_base_field.rb', line 36

def turbo_frame
  "#{self.class.name.demodulize.to_s.underscore}_#{display}_#{frame_id}"
end

#visible_in_reflection?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/avo/fields/has_base_field.rb', line 79

def visible_in_reflection?
  false
end