提交 47134a04 编写于 作者: S Santiago Pastorino 提交者: José Valim

blocks removed from all the ActiveRelation query_methods, extend method added instead

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 026cec33
......@@ -328,6 +328,15 @@ def inspect
to_a.inspect
end
def extend(*args, &block)
if block_given?
apply_modules Module.new(&block)
self
else
super
end
end
protected
def method_missing(method, *args, &block)
......
......@@ -11,9 +11,8 @@ module QueryMethods
next if [:where, :having, :select].include?(query_method)
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args, &block)
def #{query_method}(*args)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = Array.wrap(args.flatten).reject {|x| x.blank? }
new_relation.#{query_method}_values += value if value.present?
new_relation
......@@ -21,9 +20,8 @@ def #{query_method}(*args, &block)
CEVAL
end
def reorder(*args, &block)
def reorder(*args)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = Array.wrap(args.flatten).reject {|x| x.blank? }
new_relation.order_values = value if value.present?
new_relation
......@@ -42,9 +40,8 @@ def select(*args)
[:where, :having].each do |query_method|
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args, &block)
def #{query_method}(*args)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = build_where(*args)
new_relation.#{query_method}_values += Array.wrap(value) if value.present?
new_relation
......@@ -56,9 +53,8 @@ def #{query_method}(*args, &block)
attr_accessor :"#{query_method}_value"
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(value = true, &block)
def #{query_method}(value = true)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
new_relation.#{query_method}_value = value
new_relation
end
......
......@@ -618,7 +618,7 @@ def test_except
end
def test_anonymous_extension
relation = Post.where(:author_id => 1).order('id ASC') do
relation = Post.where(:author_id => 1).order('id ASC').extend do
def author
'lifo'
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册