Sha256: a69dfd7d2d6d12e99aa77634be5ec3ba22fca4e4875749eb3c9989390e5734ac

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 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

module ActiveRecord::AttributeMethods::Read
  prepend AdaptiveAlias::ActiveModelPatches::ReadAttribute
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adaptive_alias-0.2.0 lib/adaptive_alias/active_model_patches/read_attribute.rb