Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/base.rb

Defined Under Namespace

Modules: CompositeClassMethods, CompositeInstanceMethods

Constant Summary collapse

INVALID_FOR_COMPOSITE_KEYS =
'Not appropriate for composite primary keys'
NOT_IMPLEMENTED_YET =
'Not implemented for composite primary keys yet'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.composite?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/composite_primary_keys/base.rb', line 39

def composite?
  false
end

.primary_key=(keys) ⇒ Object Also known as: primary_keys=



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/composite_primary_keys/base.rb', line 11

def primary_key=(keys)
  unless keys.kind_of?(Array)
    self.primary_key_without_composite_key_support = keys
    return
  end

  @primary_keys = keys.map { |k| k.to_s }.to_composite_keys

  class_eval <<-EOV
    extend  CompositeClassMethods
    include CompositeInstanceMethods
  EOV
end

.primary_key_without_composite_key_support=Object



10
# File 'lib/composite_primary_keys/base.rb', line 10

alias_method :primary_key_without_composite_key_support=, :primary_key=

.set_primary_keys(*keys) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/composite_primary_keys/base.rb', line 26

def set_primary_keys(*keys)
  ActiveSupport::Deprecation.warn(
      "Calling set_primary_keys is deprecated. Please use `self.primary_keys = keys` instead."
  )

  keys = keys.first if keys.first.is_a?(Array)
  if keys.length == 1
    self.primary_key = keys.first
  else
    self.primary_keys = keys
  end
end

Instance Method Details

#composite?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/composite_primary_keys/base.rb', line 44

def composite?
  self.class.composite?
end