From 68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 22 Dec 2014 12:35:28 -0700 Subject: Convert `add_references` to use kwargs While we still aren't accepting PRs that only make changes like this, it's fine when we're actively working on a method if it makes our lives easier. --- .../abstract/schema_statements.rb | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index b22a304d4c..0577f868e1 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -641,13 +641,25 @@ module ActiveRecord # # add_reference(:products, :supplier, polymorphic: true, index: true) # - def add_reference(table_name, ref_name, options = {}) - polymorphic = options.delete(:polymorphic) - index_options = options.delete(:index) - type = options.delete(:type) || :integer + def add_reference( + table_name, + ref_name, + polymorphic: false, + index: false, + type: :integer, + **options + ) + polymorphic_options = polymorphic.is_a?(Hash) ? polymorphic : options + index_options = index.is_a?(Hash) ? index : {} add_column(table_name, "#{ref_name}_id", type, options) - add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic - add_index(table_name, polymorphic ? %w[type id].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options + + if polymorphic + add_column(table_name, "#{ref_name}_type", :string, polymorphic_options) + end + + if index + add_index(table_name, polymorphic ? %w[type id].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options) + end end alias :add_belongs_to :add_reference -- cgit v1.2.3