mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
9fff631a06
commit
a9c0c46263
1 changed files with 17 additions and 7 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue