From a9c0c46263dcafcf01944246e31cbe5650ee605e Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 22 Dec 2014 12:06:01 -0700 Subject: Convert `references` to kwargs While we aren't taking PRs with these kinds of changes just yet, they are fine if we're actively working on the method and it makes things easier. --- .../abstract/schema_definitions.rb | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 537e21029e..33419680fb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -306,15 +306,25 @@ module ActiveRecord # t.belongs_to(:supplier, polymorphic: true) # # See SchemaStatements#add_reference - def references(*args) - options = args.extract_options! - polymorphic = options.delete(:polymorphic) - index_options = options.delete(:index) - type = options.delete(:type) || :integer + def references( + *args, + polymorphic: false, + index: false, + type: :integer, + **options + ) + polymorphic_options = polymorphic.is_a?(Hash) ? polymorphic : options + index_options = index.is_a?(Hash) ? index : {} args.each do |col| column("#{col}_id", type, options) - column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic - index(polymorphic ? %w(type id).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options + + if polymorphic + column("#{col}_type", :string, polymorphic_options) + end + + if index + self.index(polymorphic ? %w(type id).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options) + end end end alias :belongs_to :references -- cgit v1.2.3