mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor: metaprogramming here it's confusing and make use of tap
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
parent
0abf4b0746
commit
8d9545389f
1 changed files with 78 additions and 69 deletions
|
@ -5,87 +5,96 @@ 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"
|
||||
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
|
||||
|
||||
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
|
||||
def includes(*args)
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.includes_values += args if args.present? }
|
||||
end
|
||||
|
||||
def reorder(*args)
|
||||
def eager_load(*args)
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.eager_load_values += args if args.present? }
|
||||
end
|
||||
|
||||
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.order_values = args if args.present?
|
||||
new_relation
|
||||
clone.tap { |r| r.select_values += args if args.present? }
|
||||
end
|
||||
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
|
||||
def group(*args)
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.group_values += args if args.present? }
|
||||
end
|
||||
|
||||
def order(*args)
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.order_values += args if args.present? }
|
||||
end
|
||||
|
||||
def reorder(*args)
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.order_values = args if args.present? }
|
||||
end
|
||||
|
||||
def joins(*args)
|
||||
args.flatten!
|
||||
args.reject! { |a| a.blank? }
|
||||
clone.tap { |r| r.joins_values += args if args.present? }
|
||||
end
|
||||
|
||||
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 { |r| r.lock_value = locks || true }
|
||||
else
|
||||
clone.tap { |r| r.lock_value = false }
|
||||
end
|
||||
end
|
||||
|
||||
def joins(*args)
|
||||
args.flatten!
|
||||
args.reject! { |a| a.blank? }
|
||||
new_relation = clone
|
||||
new_relation.joins_values += args if args.present?
|
||||
new_relation
|
||||
end
|
||||
def readonly(value = true)
|
||||
clone.tap { |r| r.readonly_value = value }
|
||||
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 create_with(value = true)
|
||||
clone.tap { |r| r.create_with_value = value }
|
||||
end
|
||||
|
||||
ActiveRecord::Relation::SINGLE_VALUE_METHODS.each do |query_method|
|
||||
attr_accessor :"#{query_method}_value"
|
||||
|
||||
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 from(value = true)
|
||||
clone.tap { |r| r.from_value = value }
|
||||
end
|
||||
|
||||
def extending(*modules)
|
||||
new_relation = clone
|
||||
new_relation.send :apply_modules, *modules
|
||||
new_relation
|
||||
end
|
||||
|
||||
def lock(locks = true, &block)
|
||||
relation = clone
|
||||
relation.send(:apply_modules, Module.new(&block)) if block_given?
|
||||
|
||||
case locks
|
||||
when String, TrueClass, NilClass
|
||||
clone.tap {|new_relation| new_relation.lock_value = locks || true }
|
||||
else
|
||||
clone.tap {|new_relation| new_relation.lock_value = false }
|
||||
end
|
||||
clone.tap { |r| r.send :apply_modules, *modules }
|
||||
end
|
||||
|
||||
def reverse_order
|
||||
|
|
Loading…
Reference in a new issue