Module: AutomaticForeignKey::ActiveRecord::ConnectionAdapters::TableDefinition
- Defined in:
- lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#belongs_to(table, options = {}) ⇒ Object
Some people liked this; personally I’ve decided against using it but I’ll keep it nonetheless.
- #column_with_automatic_foreign_key(name, type, options = {}) ⇒ Object
- #indices ⇒ Object
- #primary_key_with_automatic_foreign_key(name, options = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb', line 6 def self.included(base) base.class_eval do alias_method_chain :column, :automatic_foreign_key alias_method_chain :primary_key, :automatic_foreign_key end end |
Instance Method Details
#belongs_to(table, options = {}) ⇒ Object
Some people liked this; personally I’ve decided against using it but I’ll keep it nonetheless
40 41 42 43 44 |
# File 'lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb', line 40 def belongs_to(table, = {}) = .merge(:references => table) [:on_delete] = .delete(:dependent) if .has_key?(:dependent) column("#{table.to_s.singularize}_id".to_sym, :integer, ) end |
#column_with_automatic_foreign_key(name, type, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb', line 21 def column_with_automatic_foreign_key(name, type, = {}) column_without_automatic_foreign_key(name, type, ) unless AutomaticForeignKey.disable references = ActiveRecord::Base.references(self.name, name, ) if references AutomaticForeignKey.set_default_update_and_delete_actions!() foreign_key(name, references.first, references.last, ) if index = () # append [column_name, index_options] pair self.indices << [name, AutomaticForeignKey.(index)] end elsif [:index] self.indices << [name, AutomaticForeignKey.([:index])] end end self end |
#indices ⇒ Object
17 18 19 |
# File 'lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb', line 17 def indices @indices ||= [] end |
#primary_key_with_automatic_foreign_key(name, options = {}) ⇒ Object
13 14 15 |
# File 'lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb', line 13 def primary_key_with_automatic_foreign_key(name, = {}) column(name, :primary_key, ) end |