Sha256: d018fa3e4788017d69441d62ab88b0a2bed91120daf011ca1672fb2371216c70

Contents?: true

Size: 969 Bytes

Versions: 2

Compression:

Stored size: 969 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?
        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

2 entries across 2 versions & 1 rubygems

Version Path
adaptive_alias-1.1.2 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-1.1.1 lib/adaptive_alias/active_model_patches/read_attribute.rb