diff options
-rw-r--r-- | lib/active_relation/predicates.rb | 6 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/relations/order.rb | 9 | ||||
-rw-r--r-- | lib/active_relation/relations/selection.rb | 3 | ||||
-rw-r--r-- | lib/active_relation/relations/table.rb | 1 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/order_spec.rb | 26 |
6 files changed, 36 insertions, 14 deletions
diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index f68ec991c3..6fcef3cfd6 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -40,35 +40,30 @@ module ActiveRelation (operand1 == other.operand2 and operand2 == other.operand1)) end - protected def predicate_sql '=' end end class GreaterThanOrEqualTo < Binary - protected def predicate_sql '>=' end end class GreaterThan < Binary - protected def predicate_sql '>' end end class LessThanOrEqualTo < Binary - protected def predicate_sql '<=' end end class LessThan < Binary - protected def predicate_sql '<' end @@ -79,7 +74,6 @@ module ActiveRelation end class In < Binary - protected delegate :predicate_sql, :to => :operand2 end end
\ No newline at end of file diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 835e965f72..ab5f440d9e 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -36,7 +36,6 @@ module ActiveRelation Join.new(join_sql, relation1.descend(&block), relation2.descend(&block), *predicates.collect(&block)) end - protected def joins this_join = [ join_sql, @@ -64,11 +63,11 @@ module ActiveRelation delegate :engine, :to => :relation def table_sql - relation.aggregation?? relation.to_sql(Sql::TableReference.new(engine)) : relation.send(:table_sql) + relation.aggregation?? relation.to_sql(Sql::TableReference.new(engine)) : relation.table_sql end def selects - relation.aggregation?? [] : relation.send(:selects) + relation.aggregation?? [] : relation.selects end def attributes diff --git a/lib/active_relation/relations/order.rb b/lib/active_relation/relations/order.rb index 6949b3acf7..bfd05f48b3 100644 --- a/lib/active_relation/relations/order.rb +++ b/lib/active_relation/relations/order.rb @@ -1,9 +1,10 @@ module ActiveRelation class Order < Compound - attr_reader :orders + attr_reader :order def initialize(relation, *orders) - @relation, @orders = relation, orders + @order = orders.pop + @relation = orders.empty?? relation : Order.new(relation, *orders) end def ==(other) @@ -15,5 +16,9 @@ module ActiveRelation def descend(&block) Order.new(relation.descend(&block), *orders.collect(&block)) end + + def orders + relation.orders + [order] + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/selection.rb b/lib/active_relation/relations/selection.rb index e7a91f7572..fe28908cc2 100644 --- a/lib/active_relation/relations/selection.rb +++ b/lib/active_relation/relations/selection.rb @@ -17,9 +17,8 @@ module ActiveRelation Selection.new(relation.descend(&block), yield(predicate)) end - protected def selects - relation.send(:selects) + [predicate] + relation.selects + [predicate] end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 4682298608..72682bee55 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -44,7 +44,6 @@ module ActiveRelation @attributes = @columns = nil end - protected def table_sql "#{engine.quote_table_name(name)}" end diff --git a/spec/active_relation/unit/relations/order_spec.rb b/spec/active_relation/unit/relations/order_spec.rb index f37a9f26e9..e4f925f21a 100644 --- a/spec/active_relation/unit/relations/order_spec.rb +++ b/spec/active_relation/unit/relations/order_spec.rb @@ -7,6 +7,17 @@ module ActiveRelation @attribute = @relation[:id] end + describe '#initialize' do + before do + @another_attribtue = @relation[:name] + end + + it "manufactures nested Order relations if multiple predicates are provided" do + Order.new(@relation, @predicate, @another_attribute). \ + should == Order.new(Order.new(@relation, @another_attribute), @predicate) + end + end + describe '#qualify' do it "descends" do Order.new(@relation, @attribute).qualify. \ @@ -59,6 +70,21 @@ module ActiveRelation ") end end + + describe "when ordering an ordered relation" do + before do + @ordered_relation = Order.new(@relation, @attribute) + @another_attribute = @relation[:name] + end + + it "manufactures sql with an order clause populated by comma-separated attributes" do + Order.new(@ordered_relation, @another_attribute).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`id`, `users`.`name` + ") + end + end end end end |