Sha256: b0acbca5bd9f2bffbecde4a27405b5770381dcdce14474101721be78d40c5596

Contents?: true

Size: 997 Bytes

Versions: 3

Compression:

Stored size: 997 Bytes

Contents

require 'active_record'

module AdaptiveAlias
  module ActiveModelPatches
    module ReadAttribute
      def read_attribute(attr_name, &block) # :nodoc:
        name = attr_name.to_s
        name = self.class.attribute_aliases[name] || name

        name = @primary_key if name == 'id' && @primary_key
        _read_attribute(name, &block)
      end

      # This method exists to avoid the expensive primary_key check internally, without
      # breaking compatibility with the write_attribute API
      def _read_attribute(attr_name, &block) # :nodoc:
        name = attr_name.to_s
        name = self.class.attribute_aliases[name] || name

        sync_with_transaction_state if @transaction_state&.finalized?
        attribute_set_fix!
        return @attributes.fetch_value(name, &block)
      end
    end
  end
end

# Nested module include is not supported until ruby 3.0
class ActiveRecord::Base
  prepend AdaptiveAlias::ActiveModelPatches::ReadAttribute
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adaptive_alias-1.3.0 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-1.2.1 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-1.2.0 lib/adaptive_alias/active_model_patches/read_attribute.rb