public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
New association method in data_factory [#102 state:resolved]
dsutedja (author)
Mon Aug 18 08:22:12 -0700 2008
commit  450aaec8f875dc3551709da6458dac602c915828
tree    2305d48bd66ebfb9f9c1ff44f482634d625d56e2
parent  ba6ec235f36909f2a120ca10fdc8e093dcb1e62a
...
70
71
72
73
 
74
75
76
...
80
81
82
83
84
85
86
 
87
88
89
...
70
71
72
 
73
74
75
76
...
80
81
82
 
 
 
 
83
84
85
86
0
@@ -70,7 +70,7 @@ class ItemFactory
0
     include Mack::Data::Factory
0
     
0
     field :title, "MyItem"
0
-    field :owner_id, {:user => 'id'}, {:assoc => :first}
0
+    association :owner_id, {:user => 'id'}, :first
0
 end
0
 
0
 class UserFactory
0
@@ -80,10 +80,7 @@ class UserFactory
0
     field :password, "roastedPeanuts", :immutable => true
0
 end
0
 
0
-In the above example, the ItemFactory define the owner_id field's value as {:user => 'id'}.  When the factory is run,
0
-it will generate value for owner_id as 'the id of the user #x'.  You can also pass in rules to define which instance 
0
-of user that the id will be retrieved from.  The rules are: :first, :last, :random, and :spread.  The default is :spread.
0
-Definition of the association rules:
0
+In the above example, the ItemFactory define the owner_id field as an association to {:user => 'id'}.  When the factory is run, it will generate value for owner_id as 'the id of the user #x'.  You can also pass in rules to define which instance of user that the id will be retrieved from.  The rules are: :first, :last, :random, and :spread.  The default is :spread.  Definition of the association rules:
0
   :first  --> always associate with the first object that this object belongs to.  If there are 10 users [id = 0 - 10], then the item will always get associated with user #0 (i.e. item's owner_id always == 0)
0
   :last   --> always associate with the last object that this object belongs to.  If there are 10 users [id = 0 - 10], then the item will always get associated with user #10 (i.e. item's owner_id always == 9)
0
   :random --> always associate with the Nth object that this object belongs to (and N is random). If there are 10 users [id = 0 - 10], then the item will get associated with user #n (i.e. item's owner_id == rand(10))
...
83
84
85
 
 
 
 
 
 
 
86
87
88
...
83
84
85
86
87
88
89
90
91
92
93
94
95
0
@@ -83,6 +83,13 @@ module Mack
0
         end
0
         
0
         # 
0
+        # Define an association rule for this field
0
+        #
0
+        def association(model_attrib_sym, assoc_map, assoc_rule = :spread)
0
+          field(model_attrib_sym, {:df_assoc_map => assoc_map}, {:assoc => assoc_rule})
0
+        end
0
+        
0
+        # 
0
         # Define a scope in the factory.
0
         # Any field defined in a scope will overwrite its cousin in the default scope.
0
         #
...
40
41
42
43
44
45
 
 
 
 
46
47
48
49
50
51
 
52
53
54
...
59
60
61
62
 
63
64
65
...
76
77
78
79
80
 
 
81
82
83
...
40
41
42
 
 
 
43
44
45
46
47
48
49
50
51
 
52
53
54
55
...
60
61
62
 
63
64
65
66
...
77
78
79
 
 
80
81
82
83
84
0
@@ -40,15 +40,16 @@ module Mack
0
         
0
         # 
0
         # if the value is a hash, then it's a relationship mapping
0
-        if field_value.is_a?(Hash)
0
-          owner = field_value.keys[0]
0
-          key   = field_value[owner]
0
+        if field_value.is_a?(Hash) and field_value[:df_assoc_map]
0
+          map_data = field_value[:df_assoc_map]
0
+          owner = map_data.keys[0]
0
+          key   = map_data[owner]
0
           begin
0
             owner_model = owner.to_s.camelcase.constantize
0
             bridge = Mack::Data::Bridge.new
0
             
0
             assoc_rules = field_rules[:assoc] || :spread
0
-            assoc_rules = :random if !([:first, :last, :random, :spread].include?(assoc_rules))
0
+            assoc_rules = :spread if !([:first, :last, :random, :spread].include?(assoc_rules))
0
             # cache the query once
0
             if Mack::Data::RegistryMap.registered_items[self.field_name.to_sym] == nil
0
               all_owner_models = bridge.get_all(owner_model)
0
@@ -59,7 +60,7 @@ module Mack
0
             
0
             case assoc_rules
0
               when :first
0
-                value = all_owner_models[0].send(key)
0
+                value = all_owner_models[0].send(key.to_s)
0
               when :last
0
                 value = all_owner_models[all_owner_models.size - 1].send(key)
0
               when :random
0
@@ -76,8 +77,8 @@ module Mack
0
             end
0
             
0
             return value
0
-          rescue Exception
0
-            Mack.logger.warn "WARNING: DataFactory: field_value for #{field_name} is not set properly because data relationship defined is not correct"
0
+          rescue Exception => ex
0
+            Mack.logger.warn "WARNING: DataFactory: id_map for #{field_name} is not set properly because data relationship defined is not correct"
0
           end
0
         end
0
         
...
93
94
95
96
 
97
98
99
 
100
101
102
103
 
104
105
106
107
 
108
109
110
111
 
112
113
114
...
93
94
95
 
96
97
98
 
99
100
101
102
 
103
104
105
106
 
107
108
109
110
 
111
112
113
114
0
@@ -93,22 +93,22 @@ module Mack
0
       field :id, 0 do |def_value, rules, index|
0
         index
0
       end
0
-      field :owner_id, {"Mack::FactoryTest::User" => "id"}
0
+      association :owner_id, {Mack::FactoryTest::User => "id"}
0
       
0
       scope_for(:relationship_first) do
0
-        field :owner_id, {"Mack::FactoryTest::User" => "id"}, {:assoc => :first}
0
+        association :owner_id, {Mack::FactoryTest::User => "id"}, :first
0
       end
0
       
0
       scope_for(:relationship_last) do
0
-        field :owner_id, {"Mack::FactoryTest::User" => "id"}, {:assoc => :last}
0
+        association :owner_id, {Mack::FactoryTest::User => "id"}, :last
0
       end
0
       
0
       scope_for(:relationship_random) do
0
-        field :owner_id, {"Mack::FactoryTest::User" => "id"}, {:assoc => :random}
0
+        association :owner_id, {Mack::FactoryTest::User => :id}, :random
0
       end
0
       
0
       scope_for(:relationship_spread) do
0
-        field :owner_id, {"Mack::FactoryTest::User" => "id"}, {:assoc => :spread}
0
+        association :owner_id, {Mack::FactoryTest::User => "id"}, :spread
0
       end      
0
     end
0
     
...
129
130
131
132
 
133
134
135
...
129
130
131
 
132
133
134
135
0
@@ -129,7 +129,7 @@ describe "DataFactory" do
0
 
0
 
0
     describe "Relationship" do
0
-      it "should handle default relation ship rule" do
0
+      it "should handle default relationship rule" do
0
         users = @user_factory.create(10)
0
         item = @item_factory.create(1)
0
       

Comments