Class: Avo::Fields::HasBaseField

Inherits:
BaseField show all
Defined in:
lib/avo/fields/has_base_field.rb

Instance Attribute Summary collapse

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

#disabled

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 BaseField

#custom?, #custom_name?, #database_id, #fill_field, #hidden_in_reflection?, #hydrate, #model_errors, #name, #plural_name, #resolve_attribute, #to_permitted_param, #translation_key, #type, #value, #view_component_name, #visible?

Methods included from FieldExtensions::HasFieldName

#field_name, #get_field_name

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::IsDisabled

#is_disabled?

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) ⇒ HasBaseField

Returns a new instance of HasBaseField.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/avo/fields/has_base_field.rb', line 12

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
end

Instance Attribute Details

#attach_scopeObject

Returns the value of attribute attach_scope.



6
7
8
# File 'lib/avo/fields/has_base_field.rb', line 6

def attach_scope
  @attach_scope
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#discreet_paginationObject

Returns the value of attribute discreet_pagination.



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

def discreet_pagination
  @discreet_pagination
end

#displayObject

Returns the value of attribute display.



4
5
6
# File 'lib/avo/fields/has_base_field.rb', line 4

def display
  @display
end

#hide_search_inputObject

Returns the value of attribute hide_search_input.



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

def hide_search_input
  @hide_search_input
end

Returns the value of attribute link_to_child_resource.



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

def link_to_child_resource
  @link_to_child_resource
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/avo/fields/has_base_field.rb', line 5

def scope
  @scope
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


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

def authorized?
  method = "view_#{id}?".to_sym
  service = resource.authorization

  if service.has_method? method
    service.authorize_action(method, raise_exception: false)
  else
    true
  end
end

#component_for_view(view = :index) ⇒ Object

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



87
88
89
90
91
# File 'lib/avo/fields/has_base_field.rb', line 87

def component_for_view(view = :index)
  view = :show if view.in? [:new, :create, :update, :edit]

  super view
end

#default_nameObject



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

def default_name
  use_resource&.name || super
end

#field_labelObject

What the user sees in the text field



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

def field_label
  value.send(target_resource.class.title)
rescue
  nil
end

#field_valueObject

The value



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

def field_value
  value.send(database_value)
rescue
  nil
end

#frame_urlObject



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

def frame_url
  Avo::Services::URIService.parse(@resource.record_path)
    .append_path(id.to_s)
    .append_query(turbo_frame: turbo_frame.to_s)
    .to_s
end

#has_own_panel?Boolean

Returns:

  • (Boolean)


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

def has_own_panel?
  true
end

#placeholderObject



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

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

#resourceObject



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

def resource
  @resource || Avo::App.get_resource_by_model_name(@model.class)
end

#searchableObject



26
27
28
# File 'lib/avo/fields/has_base_field.rb', line 26

def searchable
  @searchable && ::Avo::App.license.has_with_trial(:searchable_associations)
end

#target_resourceObject



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

def target_resource
  if @model._reflections[id.to_s].klass.present?
    Avo::App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
  elsif @model._reflections[id.to_s].options[:class_name].present?
    Avo::App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
  else
    Avo::App.get_resource_by_name id.to_s
  end
end

#turbo_frameObject



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

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

#use_resourceObject



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

def use_resource
  App.get_resource @use_resource
end

#visible_in_reflection?Boolean

Returns:

  • (Boolean)


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

def visible_in_reflection?
  false
end