public
Fork of sam/dm-core
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/somebee/dm-core.git
Search Repo:
added support for ordering one_to_many-associations
somebee (author)
Wed May 14 16:46:51 -0700 2008
commit  6160981583de998b75d7ae925c1864c77370e176
tree    69b93b1a517c3cc22861c79a8d9d423ded6bef92
parent  c40a8c4791060f22d32a42a14f25ee147e7ed68e
...
3
4
5
6
 
7
8
9
...
3
4
5
 
6
7
8
9
0
@@ -3,7 +3,7 @@
0
 module DataMapper
0
   module Associations
0
     module OneToMany
0
- OPTIONS = [ :class_name, :child_key, :parent_key, :min, :max ]
0
+ OPTIONS = [ :class_name, :child_key, :parent_key, :min, :max, :order ]
0
 
0
       private
0
 
...
36
37
38
39
 
 
40
41
42
43
...
81
82
83
 
 
 
 
84
85
86
87
88
 
89
90
91
...
36
37
38
 
39
40
41
42
43
44
...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
0
@@ -36,7 +36,8 @@
0
 
0
       def get_children(parent)
0
         query = child_key.to_query(parent_key.get(parent))
0
-
0
+ query.merge!({:order => @child_order}) if @child_order
0
+
0
         DataMapper.repository(parent.repository.name) do
0
           child_model.all(query)
0
         end
0
0
@@ -81,11 +82,16 @@
0
         if parent_properties = options[:parent_key]
0
           raise ArgumentError, "+parent_properties+ must be an Array or nil, but was #{parent_properties.class}", caller unless Array === parent_properties
0
         end
0
+
0
+ if child_order = options[:order]
0
+ raise ArgumentError, "+child_order+ must be an Array or nil, but was #{child_order.class}", caller unless Array === child_order
0
+ end
0
 
0
         @name = name
0
         @repository_name = repository_name
0
         @child_model_name = child_model_name
0
         @child_properties = child_properties # may be nil
0
+ @child_order = child_order # may be nil
0
         @parent_model_name = parent_model_name
0
         @parent_properties = parent_properties # may be nil
0
         @loader = loader
...
54
55
56
57
 
58
59
60
61
...
355
356
357
358
 
359
360
361
362
363
364
365
366
 
 
367
368
369
...
54
55
56
 
57
58
59
60
61
...
355
356
357
 
358
359
360
361
362
363
364
 
 
365
366
367
368
369
0
@@ -54,7 +54,7 @@
0
     property :name, String
0
 
0
     repository(:sqlite3) do
0
- one_to_many :slices
0
+ one_to_many :slices, :order => [:id.desc]
0
     end
0
   end
0
 
0
0
@@ -355,15 +355,15 @@
0
         s.host_id.should be_nil
0
       end
0
 
0
- it "should load the associated instances" do
0
+ it "should load the associated instances, in the correct order" do
0
         h = repository(:sqlite3) do
0
           Host.first(:id => 1)
0
         end
0
 
0
         h.slices.should_not be_nil
0
         h.slices.size.should == 2
0
- h.slices.first.id.should == 1
0
- h.slices.last.id.should == 2
0
+ h.slices.first.id.should == 2 # ordered by [:id.desc]
0
+ h.slices.last.id.should == 1
0
 
0
         s0 = repository(:sqlite3) do
0
           Slice.first(:id => 0)

Comments

    No one has commented yet.