Module: Switchman::ActiveRecord::AttributeMethods::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#define_attribute_methodsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/switchman/active_record/attribute_methods.rb', line 28

def define_attribute_methods
  super
  # ensure that we're using the sharded attribute method
  # and not the silly one in AR::AttributeMethods::PrimaryKey
  return unless sharded_column?(@primary_key)

  class_eval(
    build_sharded_getter("id",
                         "_read_attribute(@primary_key)",
                         "::#{connection_class_for_self.name}"),
    __FILE__,
    __LINE__
  )
  class_eval(build_sharded_setter("id", @primary_key, "::#{connection_class_for_self.name}"),
             __FILE__,
             __LINE__)
end

#sharded_column?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/switchman/active_record/attribute_methods.rb', line 18

def sharded_column?(column_name)
  column_name = column_name.to_s
  @sharded_column_values ||= {}
  unless @sharded_column_values.key?(column_name)
    @sharded_column_values[column_name] =
      (column_name == primary_key && sharded_primary_key?) || sharded_foreign_key?(column_name)
  end
  @sharded_column_values[column_name]
end

#sharded_foreign_key?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/switchman/active_record/attribute_methods.rb', line 11

def sharded_foreign_key?(column_name)
  reflection = reflection_for_integer_attribute(column_name.to_s)
  return false unless reflection

  reflection.options[:polymorphic] || reflection.klass.sharded_primary_key?
end

#sharded_primary_key?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/switchman/active_record/attribute_methods.rb', line 7

def sharded_primary_key?
  !(self <= UnshardedRecord) && integral_id?
end