module Switchman::ActiveRecord::AttributeMethods

Public Class Methods

included(klass) click to toggle source
# File lib/switchman/active_record/attribute_methods.rb, line 131
def self.included(klass)
  klass.singleton_class.include(ClassMethods)
  klass.attribute_method_prefix 'global_', 'local_', 'original_'
end

Public Instance Methods

attribute(attr_name) click to toggle source

these are called if the specific methods haven't been defined yet

Calls superclass method
# File lib/switchman/active_record/attribute_methods.rb, line 146
def attribute(attr_name)
  return super unless self.class.sharded_column?(attr_name)

  reflection = self.class.send(:reflection_for_integer_attribute, attr_name)
  ::Switchman::Shard.relative_id_for(super, shard, ::Switchman::Shard.current(connection_classes_for_reflection(reflection)))
end
attribute=(attr_name, new_value) click to toggle source
Calls superclass method
# File lib/switchman/active_record/attribute_methods.rb, line 153
def attribute=(attr_name, new_value)
  unless self.class.sharded_column?(attr_name)
    super
    return
  end

  reflection = self.class.send(:reflection_for_integer_attribute, attr_name)
  super(::Switchman::Shard.relative_id_for(new_value, ::Switchman::Shard.current(connection_classes_for_reflection(reflection)), shard))
end
global_attribute(attr_name) click to toggle source
# File lib/switchman/active_record/attribute_methods.rb, line 163
def global_attribute(attr_name)
  if self.class.sharded_column?(attr_name)
    ::Switchman::Shard.global_id_for(attribute(attr_name), shard)
  else
    attribute(attr_name)
  end
end
id() click to toggle source

ensure that we're using the sharded attribute method and not the silly one in AR::AttributeMethods::PrimaryKey

Calls superclass method
# File lib/switchman/active_record/attribute_methods.rb, line 138
def id
  return super if is_a?(Shard)

  self.class.define_attribute_methods
  super
end
local_attribute(attr_name) click to toggle source
# File lib/switchman/active_record/attribute_methods.rb, line 171
def local_attribute(attr_name)
  if self.class.sharded_column?(attr_name)
    ::Switchman::Shard.local_id_for(attribute(attr_name), shard).first
  else
    attribute(attr_name)
  end
end

Private Instance Methods

connection_classes_for_reflection(reflection) click to toggle source
# File lib/switchman/active_record/attribute_methods.rb, line 181
def connection_classes_for_reflection(reflection)
  if reflection
    if reflection.options[:polymorphic]
      read_attribute(reflection.foreign_type)&.constantize&.connection_classes
    else
      reflection.klass.connection_classes
    end
  else
    self.class.connection_classes
  end
end