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.13.0 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.12.5 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.12.4 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.12.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.12.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.12.1 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.11.5 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.11.4 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.10 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.9 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.8 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.7 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.6 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.5 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.4 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.2 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.1 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.10.0 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb
better_record-0.9.3 lib/better_record/concerns/active_record_extensions/associations_extensions/belongs_to_polymorphic_extensions.rb