Module: SchemaPlus::ActiveRecord::ConnectionAdapters::TableDefinition
- Defined in:
- lib/schema_plus/active_record/connection_adapters/table_definition.rb
Overview
SchemaPlus adds several methods to TableDefinition, allowing indexes and foreign key constraints to be defined within a create_table
block of a migration, allowing for better encapsulation and more DRY definitions.
For example, without SchemaPlus you might define a table like this:
create_table :widgets do |t|
t.string :name
end
add_index :widgets, :name
But with SchemaPlus, the index can be defined within the create_table block, so you don’t need to repeat the table name:
create_table :widgets do |t|
t.string :name
t.index :name
end
Even more DRY, you can define the index as part of the column definition, via:
create_table :widgets do |t|
t.string :name, :index => true
end
For details about the :index option (including unique and multi-column indexes), see the documentation for Migration::ClassMethods#add_column
SchemaPlus also supports creation of foreign key constraints analogously, using Migration::ClassMethods#add_foreign_key or TableDefinition#foreign_key or as part of the column definition, for example:
create_table :posts do |t| # not DRY
t.integer :author_id
end
add_foreign_key :posts, :author_id, :references => :authors
create_table :posts do |t| # DRYer
t.integer :author_id
t.foreign_key :author_id, :references => :authors
end
create_table :posts do |t| # Dryest
t.integer :author_id, :foreign_key => true
end
NOTE: In the standard configuration, SchemaPlus automatically creates foreign key constraints for columns whose names end in _id
. So the above examples are redundant, unless automatic creation was disabled at initialization in the global Config.
SchemaPlus likewise by default automatically creates foreign key constraints for columns defined via t.references
. However, SchemaPlus does not create foreign key constraints if the :polymorphic
option is true
Finally, the configuration for foreign keys can be overriden on a per-table basis by passing Config options to Migration::ClassMethods#create_table, such as
create_table :students, :foreign_keys => {:auto_create => false} do
t.integer :student_id
end
Instance Attribute Summary collapse
-
#foreign_keys ⇒ Object
readonly
:nodoc:.
-
#schema_plus_config ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#belongs_to_with_schema_plus(*args) ⇒ Object
need detect :polymorphic at this level, because rails strips it out before calling #column (twice, once for _id and once for _type).
-
#column_with_schema_plus(name, type, options = {}) ⇒ Object
:nodoc:.
- #foreign_key(column_names, references_table_name, references_column_names, options = {}) ⇒ Object
- #index(column_name, options = {}) ⇒ Object
-
#initialize_with_schema_plus(*args) ⇒ Object
:nodoc:.
-
#primary_key_with_schema_plus(name, type = :primary_key, options = {}) ⇒ Object
:nodoc:.
-
#references_with_schema_plus(*args) ⇒ Object
need detect :polymorphic at this level, because rails strips it out before calling #column (twice, once for _id and once for _type).
-
#to_sql_with_schema_plus ⇒ Object
:nodoc:.
Methods included from SchemaPlus::ActiveRecord::ColumnOptionsHandler
#schema_plus_handle_column_options, #schema_plus_normalize_column_options
Instance Attribute Details
#foreign_keys ⇒ Object (readonly)
:nodoc:
70 71 72 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 70 def foreign_keys @foreign_keys end |
#schema_plus_config ⇒ Object
:nodoc:
69 70 71 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 69 def schema_plus_config @schema_plus_config end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 72 def self.included(base) #:nodoc: base.class_eval do alias_method_chain :initialize, :schema_plus alias_method_chain :column, :schema_plus alias_method_chain :references, :schema_plus alias_method_chain :belongs_to, :schema_plus alias_method_chain :primary_key, :schema_plus if ::ActiveRecord::VERSION::MAJOR.to_i < 4 attr_accessor :name attr_accessor :indexes alias_method_chain :to_sql, :schema_plus end end end |
Instance Method Details
#belongs_to_with_schema_plus(*args) ⇒ Object
need detect :polymorphic at this level, because rails strips it out before calling #column (twice, once for _id and once for _type)
120 121 122 123 124 125 126 127 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 120 def belongs_to_with_schema_plus(*args) #:nodoc: = args. [:references] = nil if [:polymorphic] () [:_index] = .delete(:index) unless [:polymorphic] # usurp index creation from AR args << belongs_to_without_schema_plus(*args) end |
#column_with_schema_plus(name, type, options = {}) ⇒ Object
:nodoc:
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 129 def column_with_schema_plus(name, type, = {}) #:nodoc: () # prevent AR from seeing :index => false as a request for an index if noindex = [:index] == false .delete(:index) end column_without_schema_plus(name, type, ) [:index] = false if noindex (self.name, name, , :config => schema_plus_config) self end |
#foreign_key(column_names, references_table_name, references_column_names, options = {}) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 154 def foreign_key(column_names, references_table_name, references_column_names, = {}) .merge!(:column_names => column_names, :references_column_names => references_column_names) .reverse_merge!(:name => ForeignKeyDefinition.default_name(self.name, column_names)) @foreign_keys << ForeignKeyDefinition.new(self.name, AbstractAdapter.proper_table_name(references_table_name), ) self end |
#index(column_name, options = {}) ⇒ Object
149 150 151 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 149 def index(column_name, ={}) @indexes << ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(self.name, column_name, ) end |
#initialize_with_schema_plus(*args) ⇒ Object
:nodoc:
88 89 90 91 92 93 94 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 88 def initialize_with_schema_plus(*args) #:nodoc: initialize_without_schema_plus(*args) @foreign_keys = [] if ::ActiveRecord::VERSION::MAJOR.to_i < 4 @indexes = [] end end |
#primary_key_with_schema_plus(name, type = :primary_key, options = {}) ⇒ Object
:nodoc:
97 98 99 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 97 def primary_key_with_schema_plus(name, = {}) #:nodoc: column(name, :primary_key, ) end |
#references_with_schema_plus(*args) ⇒ Object
need detect :polymorphic at this level, because rails strips it out before calling #column (twice, once for _id and once for _type)
109 110 111 112 113 114 115 116 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 109 def references_with_schema_plus(*args) #:nodoc: = args. [:references] = nil if [:polymorphic] () [:_index] = .delete(:index) unless [:polymorphic] # usurp index creation from AR args << references_without_schema_plus(*args) end |
#to_sql_with_schema_plus ⇒ Object
:nodoc:
141 142 143 144 145 |
# File 'lib/schema_plus/active_record/connection_adapters/table_definition.rb', line 141 def to_sql_with_schema_plus #:nodoc: sql = to_sql_without_schema_plus sql << ', ' << @foreign_keys.map(&:to_sql) * ', ' unless @foreign_keys.empty? sql end |