提交 8d954538 编写于 作者: S Santiago Pastorino 提交者: Jeremy Kemper

Refactor: metaprogramming here it's confusing and make use of tap

Signed-off-by: NJeremy Kemper <jeremy@bitsweat.net>
上级 0abf4b07
......@@ -5,89 +5,98 @@ module ActiveRecord
module QueryMethods
extend ActiveSupport::Concern
included do
(ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).each do |query_method|
attr_accessor :"#{query_method}_values"
next if [:where, :having, :select, :joins].include?(query_method)
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args)
args.reject! { |a| a.blank? }
new_relation = clone
new_relation.#{query_method}_values += args if args.present?
new_relation
end
CEVAL
end
attr_accessor :includes_values, :eager_load_values, :preload_values,
:select_values, :group_values, :order_values, :joins_values, :where_values, :having_values,
:limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value, :from_value
def reorder(*args)
args.reject! { |a| a.blank? }
new_relation = clone
new_relation.order_values = args if args.present?
new_relation
end
def includes(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.includes_values += args if args.present? }
end
def select(*args)
if block_given?
to_a.select { |*block_args| yield(*block_args) }
else
args.reject! { |a| a.blank? }
new_relation = clone
new_relation.select_values += args if args.present?
new_relation
end
end
def eager_load(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.eager_load_values += args if args.present? }
end
def joins(*args)
args.flatten!
def preload(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.preload_values += args if args.present? }
end
def select(*args)
if block_given?
to_a.select { |*block_args| yield(*block_args) }
else
args.reject! { |a| a.blank? }
new_relation = clone
new_relation.joins_values += args if args.present?
new_relation
clone.tap { |r| r.select_values += args if args.present? }
end
end
[:where, :having].each do |query_method|
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args)
new_relation = clone
value = build_where(*args)
new_relation.#{query_method}_values += Array.wrap(value) if value.present?
new_relation
end
CEVAL
end
def group(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.group_values += args if args.present? }
end
ActiveRecord::Relation::SINGLE_VALUE_METHODS.each do |query_method|
attr_accessor :"#{query_method}_value"
def order(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.order_values += args if args.present? }
end
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(value = true)
new_relation = clone
new_relation.#{query_method}_value = value
new_relation
end
CEVAL
end
def reorder(*args)
args.reject! { |a| a.blank? }
clone.tap { |r| r.order_values = args if args.present? }
end
def extending(*modules)
new_relation = clone
new_relation.send :apply_modules, *modules
new_relation
def joins(*args)
args.flatten!
args.reject! { |a| a.blank? }
clone.tap { |r| r.joins_values += args if args.present? }
end
def lock(locks = true, &block)
relation = clone
relation.send(:apply_modules, Module.new(&block)) if block_given?
def where(*args)
value = build_where(*args)
clone.tap { |r| r.where_values += Array.wrap(value) if value.present? }
end
def having(*args)
value = build_where(*args)
clone.tap { |r| r.having_values += Array.wrap(value) if value.present? }
end
def limit(value = true)
clone.tap { |r| r.limit_value = value }
end
def offset(value = true)
clone.tap { |r| r.offset_value = value }
end
def lock(locks = true)
case locks
when String, TrueClass, NilClass
clone.tap {|new_relation| new_relation.lock_value = locks || true }
clone.tap { |r| r.lock_value = locks || true }
else
clone.tap {|new_relation| new_relation.lock_value = false }
clone.tap { |r| r.lock_value = false }
end
end
def readonly(value = true)
clone.tap { |r| r.readonly_value = value }
end
def create_with(value = true)
clone.tap { |r| r.create_with_value = value }
end
def from(value = true)
clone.tap { |r| r.from_value = value }
end
def extending(*modules)
clone.tap { |r| r.send :apply_modules, *modules }
end
def reverse_order
order_clause = arel.send(:order_clauses).join(', ')
relation = except(:order)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册