diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-06-26 23:30:43 -0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-06-26 19:44:44 -0700 |
commit | 8d9545389f6a53dcd1ebae28c8b966c296da95f2 (patch) | |
tree | 1de8e40897f1eabf478690edbb876bbe3c63cc23 /activerecord/lib | |
parent | 0abf4b07462d4383691581bc38cd6bf1b5887281 (diff) | |
download | rails-8d9545389f6a53dcd1ebae28c8b966c296da95f2.tar.gz rails-8d9545389f6a53dcd1ebae28c8b966c296da95f2.tar.bz2 rails-8d9545389f6a53dcd1ebae28c8b966c296da95f2.zip |
Refactor: metaprogramming here it's confusing and make use of tap
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 137 |
1 files changed, 73 insertions, 64 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 201a4799a4..015ca8c24c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -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) |