提交 3ef8d536 编写于 作者: E eileencodes

reuse available belongs_to? method

Reflection has a `belongs_to?` method. Instead of checking for
`macro == :belongs_to` throughout the source reuse existing
method.

I also bumped `foreign_key_present?` method onto on line because
the `belongs_to?` makes it shorter than other longer lines in
the same class.
上级 489a8f2a
......@@ -116,7 +116,7 @@ def build_record(attributes)
end
def target_reflection_has_associated_record?
!(through_reflection.macro == :belongs_to && owner[through_reflection.foreign_key].blank?)
!(through_reflection.belongs_to? && owner[through_reflection.foreign_key].blank?)
end
def update_through_counter?(method)
......
......@@ -63,14 +63,13 @@ def construct_join_attributes(*records)
# Note: this does not capture all cases, for example it would be crazy to try to
# properly support stale-checking for nested associations.
def stale_state
if through_reflection.macro == :belongs_to
if through_reflection.belongs_to?
owner[through_reflection.foreign_key] && owner[through_reflection.foreign_key].to_s
end
end
def foreign_key_present?
through_reflection.macro == :belongs_to &&
!owner[through_reflection.foreign_key].nil?
through_reflection.belongs_to? && !owner[through_reflection.foreign_key].nil?
end
def ensure_mutable
......
......@@ -278,7 +278,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
if group_attrs.first.respond_to?(:to_sym)
association = @klass._reflect_on_association(group_attrs.first.to_sym)
associated = group_attrs.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations
associated = group_attrs.size == 1 && association && association.belongs_to? # only count belongs_to associations
group_fields = Array(associated ? association.foreign_key : group_attrs)
else
group_fields = group_attrs
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册