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

#apply_update_using, #assign_value, #custom?, #custom_name?, #database_id, #execute_block, #fill_field, #form_field_label, #has_attribute?, #hidden_in_reflection?, #meta, #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.



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

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
  # Defaults to nil so that if not set falls back to `link_to_child_resource` defined in the resource
  @link_to_child_resource = args[:link_to_child_resource]
  @reloadable = args[:reloadable].present? ? args[:reloadable] : false
  @linkable = args[:linkable].present? ? args[:linkable] : false
  @attach_fields = args[:attach_fields]
end

Instance Attribute Details

#attach_fieldsObject (readonly)

Returns the value of attribute attach_fields.



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

def attach_fields
  @attach_fields
end

#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



113
114
115
# File 'lib/avo/fields/has_base_field.rb', line 113

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

#authorized?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/avo/fields/has_base_field.rb', line 96

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



90
91
92
93
94
# File 'lib/avo/fields/has_base_field.rb', line 90

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



109
110
111
# File 'lib/avo/fields/has_base_field.rb', line 109

def default_name
  use_resource&.name || super
end

#field_labelObject

What the user sees in the text field



58
59
60
61
62
# File 'lib/avo/fields/has_base_field.rb', line 58

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

#field_resourceObject



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

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

#field_valueObject

The value



51
52
53
54
55
# File 'lib/avo/fields/has_base_field.rb', line 51

def field_value
  value.send(database_value)
rescue
  nil
end

#frame_url(add_turbo_frame: true) ⇒ Object



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

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)


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

def has_own_panel?
  true
end

#placeholderObject



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

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

#query_params(add_turbo_frame: true) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/avo/fields/has_base_field.rb', line 117

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

#target_resourceObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/avo/fields/has_base_field.rb', line 64

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



39
40
41
# File 'lib/avo/fields/has_base_field.rb', line 39

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

#visible_in_reflection?Boolean

Returns:

  • (Boolean)


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

def visible_in_reflection?
  false
end