提交 68a6c8ec 编写于 作者: S Sean Griffin

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.
上级 99a6f9e6
......@@ -641,13 +641,25 @@ def index_name_exists?(table_name, index_name, default)
#
# 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册