Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/base.rb,
lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.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

.clear_active_connections!Object



80
81
82
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 80

def clear_active_connections!
  connection_handler.clear_active_connections!
end

.composite?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/composite_primary_keys/base.rb', line 51

def composite?
  false
end

.connected?Boolean

Returns true if Active Record is connected.

Returns:

  • (Boolean)


72
73
74
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 72

def connected?
  connection_handler.connected?(self)
end

.connectionObject

Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do database work unrelated to any of the specific Active Records.



49
50
51
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 49

def connection
  retrieve_connection
end

.connection_configObject

Returns the configuration of the associated connection as a hash:

ActiveRecord::Base.connection_config
# => {:pool=>5, :timeout=>5000, :database=>"db/development.sqlite3", :adapter=>"sqlite3"}

Please use only for reading.



59
60
61
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 59

def connection_config
  connection_pool.spec.config
end

.connection_poolObject



63
64
65
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 63

def connection_pool
  connection_handler.retrieve_connection_pool(self)
end

.establish_connection(spec = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 9

def self.establish_connection(spec = nil)
  case spec
    when nil
      raise AdapterNotSpecified unless defined?(Rails.env)
      establish_connection(Rails.env)
    when ConnectionSpecification
      self.connection_handler.establish_connection(name, spec)
    when Symbol, String
      if configuration = configurations[spec.to_s]
        establish_connection(configuration)
      else
        raise AdapterNotSpecified, "#{spec} database is not configured"
      end
    else
      spec = spec.symbolize_keys
      unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end

      begin
        require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
      rescue LoadError => e
        raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})"
      end

      # CPK
      load_cpk_adapter(spec[:adapter])

      adapter_method = "#{spec[:adapter]}_connection"
      unless respond_to?(adapter_method)
        raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
      end

      remove_connection
      establish_connection(ConnectionSpecification.new(spec, adapter_method))
  end
end

.load_cpk_adapter(adapter) ⇒ Object



3
4
5
6
7
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 3

def self.load_cpk_adapter(adapter)
  if adapter.to_s == 'postgresql'
    require "composite_primary_keys/connection_adapters/#{adapter}_adapter.rb"
  end
end

.primary_keysObject



10
11
12
13
14
15
# File 'lib/composite_primary_keys/base.rb', line 10

def primary_keys
  unless defined?(@primary_keys)
    reset_primary_keys
  end
  @primary_keys
end

.primary_keys=(keys) ⇒ Object



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

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

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

  class_eval <<-EOV
    extend  CompositeClassMethods
    include CompositeInstanceMethods
  EOV
end

.remove_connection(klass = self) ⇒ Object



76
77
78
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 76

def remove_connection(klass = self)
  connection_handler.remove_connection(klass)
end

.reset_primary_keysObject

Don’t like this method name, but its modeled after how AR does it



18
19
20
21
22
# File 'lib/composite_primary_keys/base.rb', line 18

def reset_primary_keys
  if self != base_class
    self.primary_keys = base_class.primary_keys
  end
end

.retrieve_connectionObject



67
68
69
# File 'lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb', line 67

def retrieve_connection
  connection_handler.retrieve_connection(self)
end

.set_primary_keys(*keys) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/composite_primary_keys/base.rb', line 38

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)


56
57
58
# File 'lib/composite_primary_keys/base.rb', line 56

def composite?
  self.class.composite?
end

#initialize_dup(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/composite_primary_keys/base.rb', line 60

def initialize_dup(other)
  cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
  # CPK
  # cloned_attributes.delete(self.class.primary_key)
  Array(self.class.primary_key).each {|key| cloned_attributes.delete(key.to_s)}

  @attributes = cloned_attributes

  _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)

  @changed_attributes = {}
  self.class.column_defaults.each do |attr, orig_value|
    @changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
  end

  @aggregation_cache = {}
  @association_cache = {}
  @attributes_cache = {}
  @new_record  = true

  ensure_proper_type
  populate_with_current_scope_attributes
  super
end