public
Description: Build your fixtures in Ruby.
Homepage: http://errtheblog.com/posts/61-fixin-fixtures
Clone URL: git://github.com/defunkt/fixture_scenarios_builder.git
fixture_scenarios_builder: custom names [Paul Cantrell]
defunkt (author)
Thu Dec 27 14:40:32 -0800 2007
commit  212be98be4a96b242e40afa5b5139e581a528e99
tree    dc45022552572d10db2ccea9a1ac43d806596d71
parent  fc1ef6efa12c1b3cbd38af70a550511ac2dcdb2f
0
...
80
81
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
84
85
...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
0
@@ -80,6 +80,37 @@ Notice how the keys correspond to the user names. FixtureScenariosBuilder will
0
 to an extent, to guess the name of your key. If it can't figure it out,
0
 keys will be the standard user_001, user_002, etc format.
0
 
0
+Thanks to Paul Cantrell's handywork, custom key names are supported. Simply use the
0
++name+ method:
0
+
0
+ scenario :foo do
0
+ name "small_red_widget", Widget.create(:size => 'small', :color => 'red')
0
+ name "big_blue_widget", Widget.create(:size => 'big', :color => 'blue')
0
+ end
0
+
0
+Another option is to assign your records to instance variables, then call +names_from_ivars+
0
+at the conclusion of your +scenario+ block.
0
+
0
+ scenario :foo do
0
+ @small_red_widget = Widget.create(:size => 'small', :color => 'red')
0
+ @big_blue_widget = Widget.create(:size => 'big', :color => 'blue')
0
+
0
+ names_from_ivars!
0
+ end
0
+
0
+The above produces the following YAML:
0
+
0
+ small_red_widget:
0
+ size: small
0
+ color: red
0
+ updated_at: 2007-12-27 10:09:05
0
+ created_at: 2007-12-27 10:09:05
0
+ big_blue_widget:
0
+ size: big
0
+ color: blue
0
+ updated_at: 2007-12-27 10:19:23
0
+ created_at: 2007-12-27 10:19:23
0
+
0
 On subsequent test runs this YAML file will not be needlessly re-created. YAML
0
 files will only be re-generated when the <tt>scenarios.rb</tt> file is
0
 modified.
...
33
34
35
 
36
37
38
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
91
 
 
 
 
 
 
92
93
94
95
96
97
 
98
99
100
...
110
111
112
113
 
114
115
 
116
117
118
...
33
34
35
36
37
38
39
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
 
115
116
117
118
...
128
129
130
 
131
132
 
133
134
135
136
0
@@ -33,6 +33,7 @@ class ScenarioBuilder
0
 
0
     @block = block
0
     @children = []
0
+ @custom_names = {}
0
   end
0
 
0
   def validate_parent(scenario)
0
@@ -88,13 +89,30 @@ class ScenarioBuilder
0
   def skip_tables
0
     %w( schema_info )
0
   end
0
+
0
+ def name(custom_name, model_object)
0
+ key = [model_object.class.name, model_object.id]
0
+ @custom_names[key] = custom_name
0
+ model_object
0
+ end
0
+
0
+ def names_from_ivars!
0
+ instance_values.each do |var, value|
0
+ name(var, value) if value.is_a? ActiveRecord::Base
0
+ end
0
+ end
0
 
0
+ def record_name(record_hash)
0
+ key = [@table_name.classify, record_hash['id'].to_i]
0
+ @record_names << (@custom_names[key] || inferred_record_name(record_hash) )
0
+ name
0
+ end
0
+
0
   def inferred_record_name(record_hash)
0
     @@record_name_fields.each do |try|
0
       if name = record_hash[try]
0
         inferred_name = name.underscore.gsub(/\W/, ' ').squeeze(' ').tr(' ', '_')
0
- count = @inferred_names.select { |name| name == inferred_name }.size
0
- @inferred_names << inferred_name
0
+ count = @record_names.select { |name| name == inferred_name }.size
0
         return count.zero? ? inferred_name : "#{inferred_name}_#{count}"
0
       end
0
     end
0
@@ -110,9 +128,9 @@ class ScenarioBuilder
0
       next files if rows.empty?
0
 
0
       @row_index = '000'
0
- @inferred_names = []
0
+ @record_names = []
0
       fixture_data = rows.inject({}) do |hash, record|
0
- hash.merge(inferred_record_name(record) => record)
0
+ hash.merge(record_name(record) => record)
0
       end
0
 
0
       write_fixture_file fixture_data

Comments

    No one has commented yet.