Class: RedhillonrailsCore::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition
- Inherits:
-
Struct
- Object
- Struct
- RedhillonrailsCore::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition
- 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
-
#column_names ⇒ Object
Returns the value of attribute column_names.
-
#deferrable ⇒ Object
Returns the value of attribute deferrable.
-
#name ⇒ Object
Returns the value of attribute name.
-
#on_delete ⇒ Object
Returns the value of attribute on_delete.
-
#on_update ⇒ Object
Returns the value of attribute on_update.
-
#references_column_names ⇒ Object
Returns the value of attribute references_column_names.
-
#references_table_name ⇒ Object
Returns the value of attribute references_table_name.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #__unqoute(value) ⇒ Object
-
#initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil) ⇒ ForeignKeyDefinition
constructor
A new instance of ForeignKeyDefinition.
- #quote(name) ⇒ Object
- #quoted_column_names ⇒ Object
- #quoted_references_column_names ⇒ Object
- #quoted_references_table_name ⇒ Object
- #quoted_table_name ⇒ Object
- #to_dump ⇒ Object
- #to_sql ⇒ Object (also: #to_s)
- #unquote(names) ⇒ Object
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_names ⇒ Object
Returns the value of attribute 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 |
#deferrable ⇒ Object
Returns the value of attribute deferrable
4 5 6 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4 def deferrable @deferrable end |
#name ⇒ Object
Returns the value of attribute name
4 5 6 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb', line 4 def name @name end |
#on_delete ⇒ Object
Returns the value of attribute 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_update ⇒ Object
Returns the value of attribute 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_names ⇒ Object
Returns the value of attribute 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_name ⇒ Object
Returns the value of attribute 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_name ⇒ Object
Returns the value of attribute 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_names ⇒ Object
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_names ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_dump ⇒ Object
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_sql ⇒ Object 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 |