Class: ActiveRecord::ForeignKeyConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/foreign_key_saver/foreign_key_saver_patches.rb

Constant Summary collapse

ACTIONS =
{ :restrict => 'RESTRICT', :no_action   => 'NO ACTION', :cascade  => 'CASCADE',
:set_null => 'SET NULL', :set_default => 'SET DEFAULT' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, key, referenced_table, referenced_key, update_action = nil, delete_action = nil) ⇒ ForeignKeyConstraint

Returns a new instance of ForeignKeyConstraint.



11
12
13
14
15
16
17
18
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 11

def initialize(name, key, referenced_table, referenced_key, update_action = nil, delete_action = nil)
  self.name = name
  self.key = key
  self.referenced_table = referenced_table
  self.referenced_key = referenced_key
  self.update_action = update_action
  self.delete_action = delete_action
end

Instance Attribute Details

#delete_actionObject

Returns the value of attribute delete_action.



9
10
11
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 9

def delete_action
  @delete_action
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 9

def key
  @key
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 8

def name
  @name
end

#referenced_keyObject

Returns the value of attribute referenced_key.



9
10
11
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 9

def referenced_key
  @referenced_key
end

#referenced_tableObject

Returns the value of attribute referenced_table.



8
9
10
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 8

def referenced_table
  @referenced_table
end

#update_actionObject

Returns the value of attribute update_action.



9
10
11
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 9

def update_action
  @update_action
end

Class Method Details

.constraint_action_from_sql(action) ⇒ Object



66
67
68
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 66

def self.constraint_action_from_sql(action)
  ACTIONS.invert[action] || action
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 36

def ==(other)
  name == other.name && key == other.key &&
  referenced_table == other.referenced_table && referenced_key == other.referenced_key &&
  update_action == other.update_action && delete_action == other.delete_action
end

#quote_constraint_action(action) ⇒ Object



42
43
44
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 42

def quote_constraint_action(action)
  ACTIONS[action.to_sym] || action.to_s
end

#to_dumpObject



54
55
56
57
58
59
60
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 54

def to_dump
  dump = "#{key.inspect}, #{referenced_table.inspect}, #{referenced_key.inspect}"
  dump << ", :name => #{name.inspect}" unless name.blank?
  dump << ", :on_update => #{update_action.inspect}" if update_action != :restrict
  dump << ", :on_delete => #{delete_action.inspect}" if delete_action != :restrict
  dump
end

#to_sObject



62
63
64
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 62

def to_s
  name
end

#to_sql(connection) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 46

def to_sql(connection)
  (name.blank? ? "" : "CONSTRAINT #{connection.quote_column_name(name)} ") +
  "FOREIGN KEY (#{connection.quote_column_names(key)}) " +
  "REFERENCES #{connection.quote_table_name(referenced_table)} (#{connection.quote_column_names(referenced_key)}) " +
  "ON UPDATE #{quote_constraint_action(update_action)} " +
  "ON DELETE #{quote_constraint_action(delete_action)}"
end