Sha256: 38a145a37cbbb9e3a7f2ddbaec803f18cc8668f5900caeb6046080add3db5c7b

Contents?: true

Size: 1.31 KB

Versions: 14

Compression:

Stored size: 1.31 KB

Contents

module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
  class ForeignKeyDefinition < Struct.new(:name, :table_name, :column_names, :references_table_name, :references_column_names, :on_update, :on_delete, :deferrable)
    ACTIONS = { :cascade => "CASCADE", :restrict => "RESTRICT", :set_null => "SET NULL", :set_default => "SET DEFAULT", :no_action => "NO ACTION" }.freeze

    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

    def to_sql
      sql = name ? "CONSTRAINT #{name} " : ""
      sql << "FOREIGN KEY (#{Array(column_names).join(", ")}) REFERENCES #{references_table_name} (#{Array(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
    alias :to_s :to_sql
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
frolic-redhillonrails_core-0.0.1 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.9.1 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.9 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.8 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.6 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.5 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.4.1 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.4 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.3.1 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.3 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.2 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.1 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
redhillonrails_core-1.0.0 lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
authorails-1.0.0 plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb