Class: RedhillonrailsCore::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition

Inherits:
Struct
  • Object
show all
Defined in:
lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil) ⇒ ForeignKeyDefinition

Returns a new instance of ForeignKeyDefinition.



7
8
9
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 7

def initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil)
  super(name, unquote(table_name), unquote(column_names), unquote(references_table_name), unquote(references_column_names), on_update, on_delete, deferrable)
end

Instance Attribute Details

#column_namesObject

Returns the value of attribute column_names

Returns:

  • (Object)

    the current value of column_names



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def column_names
  @column_names
end

#deferrableObject

Returns the value of attribute deferrable

Returns:

  • (Object)

    the current value of deferrable



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def deferrable
  @deferrable
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def name
  @name
end

#on_deleteObject

Returns the value of attribute on_delete

Returns:

  • (Object)

    the current value of on_delete



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def on_delete
  @on_delete
end

#on_updateObject

Returns the value of attribute on_update

Returns:

  • (Object)

    the current value of on_update



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def on_update
  @on_update
end

#references_column_namesObject

Returns the value of attribute references_column_names

Returns:

  • (Object)

    the current value of references_column_names



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def references_column_names
  @references_column_names
end

#references_table_nameObject

Returns the value of attribute references_table_name

Returns:

  • (Object)

    the current value of references_table_name



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def references_table_name
  @references_table_name
end

#table_nameObject

Returns the value of attribute table_name

Returns:

  • (Object)

    the current value of table_name



4
5
6
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4

def table_name
  @table_name
end

Instance Method Details

#__unqoute(value) ⇒ Object



68
69
70
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 68

def __unqoute(value)
  value.to_s.sub(/^["`](.*)["`]$/, '\1')
end

#quote(name) ⇒ Object



56
57
58
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 56

def quote(name)
  ::ActiveRecord::Base.connection.quote(name)
end

#quoted_column_namesObject



40
41
42
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 40

def quoted_column_names
  Array(column_names).collect { |name| ::ActiveRecord::Base.connection.quote_column_name(name) }
end

#quoted_references_column_namesObject



44
45
46
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 44

def quoted_references_column_names
  Array(references_column_names).collect { |name| ::ActiveRecord::Base.connection.quote_column_name(name) }
end

#quoted_references_table_nameObject



52
53
54
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 52

def quoted_references_table_name
  ::ActiveRecord::Base.connection.quote_table_name(references_table_name)
end

#quoted_table_nameObject



48
49
50
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 48

def quoted_table_name
  ::ActiveRecord::Base.connection.quote_table_name(table_name)
end

#to_dumpObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 11

def to_dump
  dump = "add_foreign_key"
  dump << " #{table_name.inspect}, [#{Array(column_names).collect{ |name| name.inspect }.join(', ')}]"
  dump << ", #{references_table_name.inspect}, [#{Array(references_column_names).collect{ |name| name.inspect }.join(', ')}]"
  dump << ", :on_update => :#{on_update}" if on_update
  dump << ", :on_delete => :#{on_delete}" if on_delete
  dump << ", :deferrable => #{deferrable}" if deferrable
  dump << ", :name => #{name.inspect}" if name
  dump
end

#to_sqlObject Also known as: to_s



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 22

def to_sql
  if name
    sql = "CONSTRAINT #{name} "
  elsif table_name.present?
    sql = "CONSTRAINT #{table_name}_#{Array(column_names).join('_')}_fkey "
  else
    sql = ""
  end

  sql << "FOREIGN KEY (#{quoted_column_names.join(", ")}) REFERENCES #{quoted_references_table_name} (#{quoted_references_column_names.join(", ")})"
  sql << " ON UPDATE #{ACTIONS[on_update]}" if on_update
  sql << " ON DELETE #{ACTIONS[on_delete]}" if on_delete
  sql << " DEFERRABLE" if deferrable
  sql
end

#unquote(names) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 60

def unquote(names)
  if names.is_a?(Array)
    names.collect { |name| __unqoute(name) }
  else
    __unqoute(names)
  end
end