# Patches for ActiveRecord::Core (see include in engine.rb) module RailsConnector module CoreExtensions module ActiveRecord module Core # Reason for patch: activerecord/lib/active_record/core#inspect does not support RailsConnector::Linklist. # We need to transform RailsConnector::Linklist to a string to prevent circular references. # Rails 6.0 does not make use of `attribute_for_inspect`, therefore we need to patch `format_for_inspect` directly. # `format_for_inspect` in > 6.1 uses two parameters # # Reason for change: activerecord/lib/active_record/core#inspect might change in future Rails versions. if Rails::VERSION::MAJOR < 6 def attribute_for_inspect(attr_name) value = read_attribute(attr_name) if value.is_a?(RailsConnector::LinkList) value.destination_objects.map(&:to_s) else super end end elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero? def format_for_inspect(value) if value.is_a?(RailsConnector::LinkList) value.destination_objects.map(&:to_s) else super end end else def attribute_for_inspect(attr_name) attr_name = attr_name.to_s attr_name = self.class.attribute_aliases[attr_name] || attr_name value = _read_attribute(attr_name) if value.present? && value.is_a?(RailsConnector::LinkList) value.destination_objects.map(&:to_s) else super end end end # Reason for patch: activerecord/lib/active_record/core#pretty_print ends up in circular reference, # as RailsConnector::Linklist was not handled. infopark_reactor/lib/reactor/attributes.rb#_read_attribute(attr_name) overwrites # the method, which is called by activerecord/lib/active_record/core#pretty_print. # # Reason for change: activerecord/lib/active_record/core#pretty_print might change in future Rails versions. # Check rails console output of an Obj with RailsConnector::Linklist attributes. def pretty_print(pp) return super if custom_inspect_method_defined? pp.object_address_group(self) do if defined?(@attributes) && @attributes attr_names = self.class.attribute_names.select { |name| has_attribute?(name) } pp.seplist(attr_names, proc { pp.text "," }) do |attr_name| pp.breakable " " pp.group(1) do pp.text attr_name pp.text ":" pp.breakable value = _read_attribute(attr_name) value = value.destination_objects.map(&:to_s) if value.present? && value.is_a?(RailsConnector::LinkList) value = inspection_filter.filter_param(attr_name, value) unless value.nil? pp.pp value end end else pp.breakable " " pp.text "not initialized" end end end end end end end