stepford 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -93,6 +93,14 @@ If working with a legacy schema, you may have models with foreign_key columns th
93
93
 
94
94
  Use `--cache-associations` to store and use factories to avoid 'stack level too deep' errors.
95
95
 
96
+ This uses the [factory_girl-cache][factory_girl-cache] gem in the autogenerated factories, so you will need to include it also in your Gemfile:
97
+
98
+ gem 'factory_girl-cache'
99
+
100
+ and
101
+
102
+ bundle install
103
+
96
104
  ##### Specifying Models
97
105
 
98
106
  Specify `--models` and a comma-delimited list of models to only output the models you specify. If you don't want to overwrite existing factory files, you should direct the output to another file and manually copy each in:
@@ -164,7 +172,7 @@ or referring to created objects through associations, though he said multiple ne
164
172
 
165
173
  Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
166
174
 
167
- [singletons]: http://stackoverflow.com/questions/2015473/using-factory-girl-in-rails-with-associations-that-have-unique-constraints-gett/3569062#3569062
175
+ [factory_girl-cache]: https://github.com/garysweaver/factory_girl-cache
168
176
  [test_factories]: https://github.com/thoughtbot/factory_girl/wiki/Testing-all-Factories-%28with-RSpec%29
169
177
  [factory_girl]: https://github.com/thoughtbot/factory_girl/
170
178
  [lic]: http://github.com/garysweaver/stepford/blob/master/LICENSE
@@ -2,26 +2,7 @@ require 'stepford/common'
2
2
 
3
3
  module Stepford
4
4
  class FactoryGirl
5
- CACHE_VALUES_FILENAME = 'fg_cache.rb'
6
-
7
5
  def self.generate_factories(options={})
8
- # guard against circular references
9
- if options[:cache_associations]
10
- File.open(File.join(File.dirname(get_factories_rb_pathname(options)), CACHE_VALUES_FILENAME), "w") do |f|
11
- #TODO: just copy this file from the gem to project vs. writing it like this
12
- f.puts '# originally created by Stepford: https://github.com/garysweaver/stepford'
13
- f.puts '# idea somewhat based on d2vid and snowangel\'s answer in http://stackoverflow.com/questions/2015473/using-factory-girl-in-rails-with-associations-that-have-unique-constraints-gett/3569062#3569062'
14
- f.puts 'fg_cachehash = {}'
15
- f.puts 'def fg_cache(class_sym, assc_sym = nil, number = nil)'
16
- # if missing 3rd arg, assume 2nd arg is 3rd arg or use default
17
- # if missing 2nd and 3rd arg, assume 2nd arg is 1st arg
18
- f.puts ' number ||= assc_sym'
19
- f.puts ' assc_sym ||= class_sym'
20
- f.puts ' fg_cachehash[factory_sym, assc_sym, number] ||= (number ? FactoryGirl.create_list(class_sym, number) : FactoryGirl.create(class_sym))'
21
- f.puts 'end'
22
- end
23
- end
24
-
25
6
  factories = {}
26
7
  expected = {}
27
8
  included_models = options[:models] ? options[:models].split(',').collect{|s|s.strip}.compact : nil
@@ -44,13 +25,18 @@ module Stepford
44
25
  should_be_trait = !(options[:associations] || required) && options[:association_traits]
45
26
  if options[:associations] || required || should_be_trait
46
27
  if options[:cache_associations]
47
- if reflection.macro == :has_many
48
- "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}after(:create) do |user, evaluator|; #{is_reserved?(assc_sym) ? 'self.' : ''}#{assc_sym} = fg_cache(#{clas_sym.inspect}#{clas_sym == assc_sym ? '' : ", #{assc_sym.inspect}"}, 2); end"
28
+ if reflection.macro == :has_many
29
+ #
30
+ "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}after(:create) do |user, evaluator|; #{is_reserved?(assc_sym) ? 'self.' : ''}#{assc_sym} = FactoryGirlCache.create_list(#{clas_sym.inspect}#{clas_sym == assc_sym ? '' : ", #{assc_sym.inspect}"}, 2); end#{should_be_trait ? '; end' : ''}"
49
31
  else
50
- "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}after(:create) do |user, evaluator|; #{is_reserved?(assc_sym) ? 'self.' : ''}#{assc_sym} = fg_cache(#{clas_sym.inspect}#{clas_sym == assc_sym ? '' : ", #{assc_sym.inspect}"}); end"
32
+ "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}before(:build) do |user, evaluator|; #{is_reserved?(assc_sym) ? 'self.' : ''}#{assc_sym} = FactoryGirlCache.create(#{clas_sym.inspect}#{clas_sym == assc_sym ? '' : ", #{assc_sym.inspect}"}); end#{should_be_trait ? '; end' : ''}"
51
33
  end
52
34
  else
53
- if reflection.macro == :has_many
35
+ if reflection.macro == :has_many
36
+ # In factory girl v4.1.0:
37
+ # create_list must be done in an after(:create) or you get Trait not registered or Factory not registered errors.
38
+ # this means that validators that verify presence or size > 0 in a association list will not work with this method, and you'll need to
39
+ # use build, not create: http://stackoverflow.com/questions/11209347/has-many-with-at-least-two-entries
54
40
  "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}#{should_be_trait || has_presence_validator ? '' : '#'}after(:create) do |user, evaluator|; FactoryGirl.create_list #{clas_sym.inspect}, 2; end#{should_be_trait ? '; end' : ''}#{should_be_trait ? '' : ' # commented to avoid circular reference'}"
55
41
  elsif assc_sym != clas_sym
56
42
  "#{should_be_trait ? "trait #{"with_#{assc_sym}".to_sym.inspect} do; " : ''}#{should_be_trait || reflection.macro == :belongs_to || has_presence_validator ? '' : '#'}association #{assc_sym.inspect}#{assc_sym != clas_sym ? ", factory: #{clas_sym.inspect}" : ''}#{should_be_trait ? '; end' : ''}#{should_be_trait || reflection.macro == :belongs_to ? '' : ' # commented to avoid circular reference'}"
@@ -159,9 +145,9 @@ module Stepford
159
145
 
160
146
  def self.write_header(f, options)
161
147
  f.puts 'require \'factory_girl_rails\''
162
- f.puts "require_relative \'#{CACHE_VALUES_FILENAME.chomp('.rb')}\'" if options[:cache_associations]
148
+ f.puts 'require \'factory_girl-cache\'' if options[:cache_associations]
163
149
  f.puts ''
164
- f.puts '# originally created by Stepford: https://github.com/garysweaver/stepford'
150
+ f.puts '# original version autogenerated by Stepford: https://github.com/garysweaver/stepford'
165
151
  f.puts ''
166
152
  f.puts 'FactoryGirl.define do'
167
153
  f.puts ' '
@@ -1,3 +1,3 @@
1
1
  module Stepford
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stepford
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor