Sha256: 08accb33289c6552658c16aa679fa319b9841781184847b083ce019ec2a7abd9

Contents?: true

Size: 949 Bytes

Versions: 68

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

require 'active_support/concern'
require 'active_record/associations'
require 'active_record/associations/belongs_to_polymorphic_association'

module BetterRecord
  module AssociationsExtensions
    module BelongsToPolymorphicAssociationExtensions
      extend ActiveSupport::Concern

      included do
        def klass
          type = owner[reflection.foreign_type]
          type.presence && type.capitalize.singularize.constantize
        end

        def replace_keys record
          super
          owner[reflection.foreign_type] = record ? get_type_value(record) : nil
        end

        def get_type_value record
          BetterRecord::PolymorphicOverride.polymorphic_value(record.class, reflection.options)
        end
      end
    end
  end
end

ActiveRecord::Associations::BelongsToPolymorphicAssociation.send(:include, BetterRecord::AssociationsExtensions::BelongsToPolymorphicAssociationExtensions)

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
better_record-0.22.5 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.22.4 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.22.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.21.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.22.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.22.0 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.21.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.20.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.10 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.9 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.8 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.7 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.6 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.4 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.1 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.19.0 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.18.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.18.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.18.1 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb