Class: Avo::Fields::HasOneField::ShowComponent

Inherits:
ShowComponent show all
Includes:
ApplicationHelper, Concerns::FrameLoading
Defined in:
app/components/avo/fields/has_one_field/show_component.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Attribute Summary

Attributes inherited from ShowComponent

#compact, #field, #index, #kwargs, #resource, #short, #stacked, #view

Instance Method Summary collapse

Methods included from Concerns::FrameLoading

#turbo_frame_loading

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #card_classes, #chart_color, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_license_warning, #root_path_without_url, #svg, #white_panel_classes

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #resource_grid, #resource_show_path, #resource_table

Methods inherited from ShowComponent

#disabled?, #field_wrapper_args, #initialize, #stimulus_attributes, #wrapper_data

Methods inherited from BaseComponent

#has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Constructor Details

This class inherits a constructor from Avo::Fields::ShowComponent

Instance Method Details

#attach_pathObject



24
25
26
27
28
29
30
31
# File 'app/components/avo/fields/has_one_field/show_component.rb', line 24

def attach_path
  helpers.avo.resources_associations_new_path(
    @resource.singular_model_key,
    @resource.record_param,
    @field.id,
    for_attribute: @field.for_attribute
  )
end

#can_attach?Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/components/avo/fields/has_one_field/show_component.rb', line 7

def can_attach?
  policy_result = true

  if @field.present?
    reflection_resource = @field.target_resource
    if reflection_resource.present? && @resource.present?
      method_name = :"attach_#{@field.id}?"

      if @resource.authorization.has_method?(method_name, raise_exception: false)
        policy_result = @resource.authorization.authorize_action(method_name, raise_exception: false)
      end
    end
  end

  policy_result
end

#can_see_the_create_button?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'app/components/avo/fields/has_one_field/show_component.rb', line 33

def can_see_the_create_button?
  create = "create_#{@field.id}?"

  authorization_service = @resource.authorization

  # By default return true if the create method is not defined for this field
  return true unless authorization_service.has_method?(create, raise_exception: false)

  authorization_service.authorize_action(create, raise_exception: false)
end

#create_pathObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/components/avo/fields/has_one_field/show_component.rb', line 44

def create_path
  association_id = @field.resource.model_class.reflect_on_association(@field.association_name).inverse_of.name

  args = {
    via_relation: association_id,
    via_relation_class: @resource.model_class.to_s,
    via_resource_class: @resource.class,
    via_record_id: @resource.record_param,
    via_association_type: :has_one
  }

  helpers.new_resource_path(resource: @field.target_resource, **args)
end