diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-12-22 12:35:28 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-12-22 13:47:11 -0700 |
commit | 68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25 (patch) | |
tree | 3a39f28602b504fa9016709aecfe564ba14f805f /activerecord/lib | |
parent | 99a6f9e60ea55924b44f894a16f8de0162cf2702 (diff) | |
download | rails-68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25.tar.gz rails-68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25.tar.bz2 rails-68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25.zip |
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 24 |
1 files 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 |