lib/avo/fields/has_base_field.rb in avo-3.6.1 vs lib/avo/fields/has_base_field.rb in avo-3.6.2

- old
+ new

@@ -2,10 +2,11 @@ module Fields class HasBaseField < BaseField include Avo::Fields::Concerns::IsSearchable include Avo::Fields::Concerns::UseResource include Avo::Fields::Concerns::ReloadIcon + include Avo::Fields::Concerns::LinkableTitle attr_accessor :display attr_accessor :scope attr_accessor :attach_scope attr_accessor :description @@ -23,24 +24,25 @@ @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 def field_resource resource || get_resource_by_model_class(@record.class) end def turbo_frame "#{self.class.name.demodulize.to_s.underscore}_#{display}_#{frame_id}" end - def frame_url + def frame_url(add_turbo_frame: true) Avo::Services::URIService.parse(field_resource.record_path) .append_path(id.to_s) - .append_query(query_params) + .append_query(query_params(add_turbo_frame:)) .to_s end # The value def field_value @@ -55,16 +57,16 @@ rescue nil end 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]) + 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 id.to_s + Avo.resource_manager.get_resource_by_name association_name end end def placeholder @placeholder || I18n.t("avo.choose_an_option") @@ -85,11 +87,11 @@ super(view) end def authorized? - method = "view_#{id}?".to_sym + method = :"view_#{id}?" service = field_resource.authorization if service.has_method? method service.authorize_action(method, raise_exception: false) else @@ -99,14 +101,19 @@ def default_name use_resource&.name || super end - def query_params + def association_name + @association_name ||= (@for_attribute || id).to_s + end + + def query_params(add_turbo_frame: true) { - turbo_frame: turbo_frame, - view: view - } + view:, + for_attribute: @for_attribute, + turbo_frame: add_turbo_frame ? turbo_frame : nil + }.compact end private def frame_id