Module: Switchman::ActiveRecord::Association

Defined in:
lib/switchman/active_record/association.rb

Instance Method Summary collapse

Instance Method Details

#association_scopeObject



21
22
23
24
25
# File 'lib/switchman/active_record/association.rb', line 21

def association_scope
  if klass
    shard.activate(klass.shard_category) { super }
  end
end

#build_record(*args) ⇒ Object



13
14
15
# File 'lib/switchman/active_record/association.rb', line 13

def build_record(*args)
  self.shard.activate { super }
end

#creation_attributesObject



32
33
34
35
36
37
38
39
40
# File 'lib/switchman/active_record/association.rb', line 32

def creation_attributes
  attributes = super

  # translate keys
  if reflection.macro.in?([:has_one, :has_many]) && !options[:through]
    attributes[reflection.foreign_key] = Shard.relative_id_for(owner[reflection.active_record_primary_key], owner.shard, self.shard)
  end
  attributes
end

#load_targetObject



17
18
19
# File 'lib/switchman/active_record/association.rb', line 17

def load_target
  self.shard.activate { super }
end

#scopeObject



27
28
29
30
# File 'lib/switchman/active_record/association.rb', line 27

def scope
  shard_value = @reflection.options[:multishard] ? @owner : self.shard
  @owner.shard.activate { super.shard(shard_value, :association) }
end

#shardObject



4
5
6
7
8
9
10
11
# File 'lib/switchman/active_record/association.rb', line 4

def shard
  if @reflection.options[:polymorphic] || @reflection.klass.shard_category == @owner.class.shard_category
    # polymorphic associations assume the same shard as the owning item
    @owner.shard
  else
    Shard.default
  end
end