mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #38719 from eileencodes/refactor-fetch_attribute
Refactor fetch_attribute
This commit is contained in:
commit
99b607e6ad
6 changed files with 43 additions and 26 deletions
|
@ -47,16 +47,8 @@ module Arel
|
|||
end
|
||||
|
||||
def self.fetch_attribute(value, &block) # :nodoc:
|
||||
case value
|
||||
when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality,
|
||||
Arel::Nodes::NotEqual, Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual,
|
||||
Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
|
||||
attribute_value = value.detect_attribute
|
||||
yield attribute_value if attribute_value
|
||||
when Arel::Nodes::Or
|
||||
fetch_attribute(value.left, &block) && fetch_attribute(value.right, &block)
|
||||
when Arel::Nodes::Grouping
|
||||
fetch_attribute(value.expr, &block)
|
||||
unless String === value
|
||||
value.fetch_attribute(&block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,14 +11,6 @@ module Arel # :nodoc: all
|
|||
@right = right
|
||||
end
|
||||
|
||||
def detect_attribute
|
||||
if self.left.is_a?(Arel::Attributes::Attribute)
|
||||
self.left
|
||||
elsif self.right.is_a?(Arel::Attributes::Attribute)
|
||||
self.right
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_copy(other)
|
||||
super
|
||||
@left = @left.clone if @left
|
||||
|
@ -37,18 +29,34 @@ module Arel # :nodoc: all
|
|||
alias :== :eql?
|
||||
end
|
||||
|
||||
module FetchAttribute
|
||||
def fetch_attribute
|
||||
if left.is_a?(Arel::Attributes::Attribute)
|
||||
yield left
|
||||
elsif right.is_a?(Arel::Attributes::Attribute)
|
||||
yield right
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Between < Binary; include FetchAttribute; end
|
||||
class NotIn < Binary; include FetchAttribute; end
|
||||
class GreaterThan < Binary; include FetchAttribute; end
|
||||
class GreaterThanOrEqual < Binary; include FetchAttribute; end
|
||||
class NotEqual < Binary; include FetchAttribute; end
|
||||
class LessThan < Binary; include FetchAttribute; end
|
||||
class LessThanOrEqual < Binary; include FetchAttribute; end
|
||||
|
||||
class Or < Binary
|
||||
def fetch_attribute(&block)
|
||||
left.fetch_attribute(&block) && right.fetch_attribute(&block)
|
||||
end
|
||||
end
|
||||
|
||||
%w{
|
||||
As
|
||||
Assignment
|
||||
Between
|
||||
GreaterThan
|
||||
GreaterThanOrEqual
|
||||
Join
|
||||
LessThan
|
||||
LessThanOrEqual
|
||||
NotEqual
|
||||
NotIn
|
||||
Or
|
||||
Union
|
||||
UnionAll
|
||||
Intersect
|
||||
|
|
|
@ -10,6 +10,14 @@ module Arel # :nodoc: all
|
|||
def invert
|
||||
Arel::Nodes::NotEqual.new(left, right)
|
||||
end
|
||||
|
||||
def fetch_attribute
|
||||
if left.is_a?(Arel::Attributes::Attribute)
|
||||
yield left
|
||||
elsif right.is_a?(Arel::Attributes::Attribute)
|
||||
yield right
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class IsDistinctFrom < Equality
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
module Arel # :nodoc: all
|
||||
module Nodes
|
||||
class Grouping < Unary
|
||||
def fetch_attribute(&block)
|
||||
expr.fetch_attribute(&block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,6 +41,9 @@ module Arel # :nodoc: all
|
|||
collector = engine.connection.visitor.accept self, collector
|
||||
collector.value
|
||||
end
|
||||
|
||||
def fetch_attribute
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,9 @@ module Arel # :nodoc: all
|
|||
def encode_with(coder)
|
||||
coder.scalar = self.to_s
|
||||
end
|
||||
|
||||
def fetch_attribute
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue