Module: ActiveRecord::ConnectionAdapters::OracleEnhancedTableDefinition
- Defined in:
- lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb
Defined Under Namespace
Classes: ForeignKey
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #column_with_virtual_columns(name, type, options = {}) ⇒ Object
-
#foreign_key(to_table, options = {}) ⇒ Object
Defines a foreign key for the table.
- #lob_columns ⇒ Object
- #raw(name, options = {}) ⇒ Object
-
#references_with_foreign_keys(*args) ⇒ Object
Adds a :foreign_key option to TableDefinition.references.
-
#to_sql_with_foreign_keys ⇒ Object
:nodoc:.
- #virtual(*args) ⇒ Object
Class Method Details
.included(base) ⇒ Object
:nodoc:
60 61 62 63 64 65 66 67 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 60 def self.included(base) #:nodoc: base.class_eval do alias_method_chain :references, :foreign_keys alias_method_chain :to_sql, :foreign_keys alias_method_chain :column, :virtual_columns end end |
Instance Method Details
#column_with_virtual_columns(name, type, options = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 79 def column_with_virtual_columns(name, type, = {}) if type == :virtual default = {:type => [:type]} if [:as] default[:as] = [:as] elsif [:default] warn "[DEPRECATION] virtual column `:default` option is deprecated. Please use `:as` instead." default[:as] = [:default] else raise "No virtual column definition found." end [:default] = default end column_without_virtual_columns(name, type, ) end |
#foreign_key(to_table, options = {}) ⇒ Object
Defines a foreign key for the table. to_table
can be a single Symbol, or an Array of Symbols. See SchemaStatements#add_foreign_key
Examples
Creating a simple foreign key
t.foreign_key(:people)
Defining the column
t.foreign_key(:people, :column => :sender_id)
Creating a named foreign key
t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
Defining the column of the to_table
.
t.foreign_key(:people, :column => :sender_id, :primary_key => :person_id)
131 132 133 134 135 136 137 138 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 131 def foreign_key(to_table, = {}) if @base.respond_to?(:supports_foreign_keys?) && @base.supports_foreign_keys? to_table = to_table.to_s.pluralize if ActiveRecord::Base.pluralize_table_names foreign_keys << ForeignKey.new(@base, to_table, ) else raise ArgumentError, "this ActiveRecord adapter is not supporting foreign_key definition" end end |
#lob_columns ⇒ Object
146 147 148 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 146 def lob_columns columns.select(&:lob?) end |
#raw(name, options = {}) ⇒ Object
69 70 71 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 69 def raw(name, ={}) column(name, :raw, ) end |
#references_with_foreign_keys(*args) ⇒ Object
Adds a :foreign_key option to TableDefinition.references. If :foreign_key is true, a foreign key constraint is added to the table. You can also specify a hash, which is passed as foreign key options.
Examples
Add goat_id column and a foreign key to the goats table.
t.references(:goat, :foreign_key => true)
Add goat_id column and a cascading foreign key to the goats table.
t.references(:goat, :foreign_key => {:dependent => :delete})
Note: No foreign key is created if :polymorphic => true is used. Note: If no name is specified, the database driver creates one for you!
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 107 def references_with_foreign_keys(*args) = args. = .delete(:foreign_key) if && ![:polymorphic] = {} if == true args.each { |to_table| foreign_key(to_table, ) } end references_without_foreign_keys(*(args << )) end |
#to_sql_with_foreign_keys ⇒ Object
:nodoc:
140 141 142 143 144 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 140 def to_sql_with_foreign_keys #:nodoc: sql = to_sql_without_foreign_keys sql << ', ' << (foreign_keys * ', ') unless foreign_keys.blank? sql end |
#virtual(*args) ⇒ Object
73 74 75 76 77 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 73 def virtual(* args) = args. column_names = args column_names.each { |name| column(name, :virtual, ) } end |