22
33module FixtureBot
44 module Row
5- Declaration = Data . define ( :table , :name , :literal_values , :association_refs , :tag_refs )
5+ TableReference = Data . define ( :table_name , :record_name )
6+ Declaration = Data . define ( :table , :name , :literal_values , :association_refs , :tag_refs , :polymorphic_refs )
67
78 class Definition
8- attr_reader :literal_values , :association_refs , :tag_refs
9+ attr_reader :literal_values , :association_refs , :tag_refs , :polymorphic_refs
910
1011 def initialize ( table , schema )
1112 @literal_values = { }
1213 @association_refs = { }
1314 @tag_refs = { }
15+ @polymorphic_refs = { }
1416
1517 define_column_methods ( table )
1618 define_association_methods ( table )
19+ define_polymorphic_methods ( table )
1720 define_join_table_methods ( table , schema )
21+ define_table_reference_methods ( schema , table )
22+ end
23+
24+ def define_table_reference_methods ( schema , table )
25+ schema . tables . each_value do |tbl |
26+ next if table . belongs_to_associations . any? { |a | a . name == tbl . singular_name }
27+ next if table . polymorphic_belongs_to_associations . any? { |a | a . name == tbl . singular_name }
28+ define_singleton_method ( tbl . singular_name ) do |record_name |
29+ TableReference . new ( table_name : tbl . name , record_name : record_name )
30+ end
31+ end
1832 end
1933
2034 private
@@ -35,6 +49,14 @@ def define_association_methods(table)
3549 end
3650 end
3751
52+ def define_polymorphic_methods ( table )
53+ table . polymorphic_belongs_to_associations . each do |assoc |
54+ define_singleton_method ( assoc . name ) do |table_ref |
55+ @polymorphic_refs [ assoc . name ] = table_ref
56+ end
57+ end
58+ end
59+
3860 def define_join_table_methods ( table , schema )
3961 schema . join_tables . each_value do |jt |
4062 if jt . left_table == table . name
@@ -69,6 +91,8 @@ def record
6991 result [ col ] = @row . literal_values [ col ]
7092 elsif foreign_key_values . key? ( col )
7193 result [ col ] = foreign_key_values [ col ]
94+ elsif polymorphic_values . key? ( col )
95+ result [ col ] = polymorphic_values [ col ]
7296 elsif defaulted_values . key? ( col )
7397 result [ col ] = defaulted_values [ col ]
7498 end
@@ -112,10 +136,27 @@ def foreign_key_values
112136 end
113137 end
114138
139+ def polymorphic_values
140+ @polymorphic_values ||= @row . polymorphic_refs . each_with_object ( { } ) do |( assoc_name , table_ref ) , hash |
141+ assoc = @table . polymorphic_belongs_to_associations . find { |a | a . name == assoc_name }
142+ hash [ assoc . foreign_key ] = Key . generate ( table_ref . table_name , table_ref . record_name )
143+ hash [ assoc . type_column ] = classify ( table_ref . table_name )
144+ end
145+ end
146+
147+ def classify ( table_name )
148+ if defined? ( ActiveSupport ::Inflector )
149+ ActiveSupport ::Inflector . classify ( table_name . to_s )
150+ else
151+ table_name . to_s . sub ( /s$/ , "" ) . capitalize
152+ end
153+ end
154+
115155 def defaulted_values
116156 @defaulted_values ||= @defaults . each_with_object ( { } ) do |( col , block ) , result |
117157 next if @row . literal_values . key? ( col )
118158 next if foreign_key_values . key? ( col )
159+ next if polymorphic_values . key? ( col )
119160
120161 fixture = Default ::Fixture . new ( key : @row . name )
121162 context = Default ::Context . new ( literal_values : @row . literal_values )
0 commit comments