Sha256: bcea85bf3347a5fb5b770954b4aa40daa7432202388fc3706d5084f44e8b2969

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

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 yield(name) if block_given? and AdaptiveAlias.missing_value?(@attributes, self.class, name)
        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

6 entries across 6 versions & 1 rubygems

Version Path
adaptive_alias-1.1.0 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-1.0.0 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-0.2.4 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-0.2.3 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-0.2.2 lib/adaptive_alias/active_model_patches/read_attribute.rb
adaptive_alias-0.2.1 lib/adaptive_alias/active_model_patches/read_attribute.rb