Class: Avo::Fields::HasBaseField

Inherits:
BaseField show all
Includes:
Concerns::IsSearchable, 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_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::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, #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.



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

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
end

Instance Attribute Details

#attach_scopeObject

Returns the value of attribute attach_scope.



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

def attach_scope
  @attach_scope
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#discreet_paginationObject

Returns the value of attribute discreet_pagination.



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

def discreet_pagination
  @discreet_pagination
end

#displayObject

Returns the value of attribute display.



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

def display
  @display
end

#hide_search_inputObject

Returns the value of attribute hide_search_input.



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

def hide_search_input
  @hide_search_input
end

Returns the value of attribute link_to_child_resource.



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

def link_to_child_resource
  @link_to_child_resource
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


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

def authorized?
  method = "view_#{id}?".to_sym
  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



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

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



100
101
102
# File 'lib/avo/fields/has_base_field.rb', line 100

def default_name
  use_resource&.name || super
end

#field_labelObject

What the user sees in the text field



53
54
55
56
57
# File 'lib/avo/fields/has_base_field.rb', line 53

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

#field_resourceObject



30
31
32
# File 'lib/avo/fields/has_base_field.rb', line 30

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

#field_valueObject

The value



46
47
48
49
50
# File 'lib/avo/fields/has_base_field.rb', line 46

def field_value
  value.send(database_value)
rescue
  nil
end

#frame_urlObject



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

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

#has_own_panel?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/avo/fields/has_base_field.rb', line 73

def has_own_panel?
  true
end

#placeholderObject



69
70
71
# File 'lib/avo/fields/has_base_field.rb', line 69

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

#query_paramsObject



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

def query_params
  {
    turbo_frame: turbo_frame,
    view: view
  }
end

#target_resourceObject



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

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

#turbo_frameObject



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

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

#visible_in_reflection?Boolean

Returns:

  • (Boolean)


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

def visible_in_reflection?
  false
end