stepford 0.12.1 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +9 -11
 - data/lib/stepford/factory_girl.rb +19 -7
 - data/lib/stepford/version.rb +1 -1
 - metadata +1 -1
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            Stepford
         
     | 
| 
       2 
2 
     | 
    
         
             
            =====
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
      
 4 
     | 
    
         
            +
            FactoryGirl = so easy now
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            Stepford is an automatic required (non-null or presence validated) association resolver and factory generator for [FactoryGirl][factory_girl].
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
         @@ -46,13 +46,12 @@ By default autogenerated factories just have required attributes, e.g.: 
     | 
|
| 
       46 
46 
     | 
    
         
             
                    price 1.23
         
     | 
| 
       47 
47 
     | 
    
         
             
                    sequence(:isbn)
         
     | 
| 
       48 
48 
     | 
    
         
             
                    sequence(:ean)
         
     | 
| 
       49 
     | 
    
         
            -
                    trait :with_summary do; template 'Test Summary'; end
         
     | 
| 
       50 
49 
     | 
    
         
             
                    updated_at { 2.weeks.ago }
         
     | 
| 
       51 
50 
     | 
    
         
             
                  end
         
     | 
| 
       52 
51 
     | 
    
         | 
| 
       53 
52 
     | 
    
         
             
                end
         
     | 
| 
       54 
53 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
            But you can have it autogenerate  
     | 
| 
      
 54 
     | 
    
         
            +
            But you can have it autogenerate all attributes, associations, and traits:
         
     | 
| 
       56 
55 
     | 
    
         | 
| 
       57 
56 
     | 
    
         
             
                require 'factory_girl_rails'
         
     | 
| 
       58 
57 
     | 
    
         | 
| 
         @@ -70,15 +69,14 @@ But you can have it autogenerate lots of the FactoryGirl bells and whistles: 
     | 
|
| 
       70 
69 
     | 
    
         
             
                    price 1.23
         
     | 
| 
       71 
70 
     | 
    
         
             
                    sequence(:isbn)
         
     | 
| 
       72 
71 
     | 
    
         
             
                    sequence(:ean)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    description 'Test Description'
         
     | 
| 
       73 
73 
     | 
    
         
             
                    trait :with_summary do; template 'Test Summary'; end
         
     | 
| 
       74 
74 
     | 
    
         
             
                    updated_at { 2.weeks.ago }
         
     | 
| 
       75 
75 
     | 
    
         
             
                  end
         
     | 
| 
       76 
76 
     | 
    
         | 
| 
       77 
77 
     | 
    
         
             
                end
         
     | 
| 
       78 
78 
     | 
    
         | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
            Stepford's FactoryGirl (and optionally its rspec helper) can help you avoid the heavy lifting.
         
     | 
| 
      
 79 
     | 
    
         
            +
            However, without modification, if you use the CLI to generate these you could more likely run into association interdependency problems (circular references). To fix those you could hand-edit the factories, or write methods to create more complex structures. Or, to keep it simple, just use the defaults for the factories CLI and then use the deep_* methods in your specs to automatically create dependencies as needed!
         
     | 
| 
       82 
80 
     | 
    
         | 
| 
       83 
81 
     | 
    
         
             
            ### Setup
         
     | 
| 
       84 
82 
     | 
    
         | 
| 
         @@ -104,7 +102,7 @@ Put this in your `test/spec_helper.rb`, `spec/spec_helper.rb`, or some other fil 
     | 
|
| 
       104 
102 
     | 
    
         | 
| 
       105 
103 
     | 
    
         
             
            #### Stepford::FactoryGirl
         
     | 
| 
       106 
104 
     | 
    
         | 
| 
       107 
     | 
    
         
            -
            Stepford::FactoryGirl acts just like FactoryGirl, but it goes through all the null=false associations for foreign keys that aren't primary keys in the factory and/or its presence validated associations and attempts to create 
     | 
| 
      
 105 
     | 
    
         
            +
            Stepford::FactoryGirl acts just like FactoryGirl, but it goes through all the null=false associations for foreign keys that aren't primary keys in the factory and/or its presence validated associations and attempts to create required association data. Pass in the option `:with_factory_options` with a hash of factory name symbols to the arguments and block you'd pass to it if you want to change only parts of required dependent associations that are created. You specify the block using a `:blk` option with a lambda.
         
     | 
| 
       108 
106 
     | 
    
         | 
| 
       109 
107 
     | 
    
         
             
            If you don't specify options, it's easy (note: it is even easier with the rspec helper- see below). If Foo requires Bar and Bar requires a list of Foobars and a Barfoo, and you have factories for each of those, you'd only have to do:
         
     | 
| 
       110 
108 
     | 
    
         | 
| 
         @@ -115,8 +113,8 @@ and that would create a list of 5 Foos, that each have a Bar, where each Bar has 
     | 
|
| 
       115 
113 
     | 
    
         
             
            But, you might want to specify traits, and certain attributes or associations or a block or different methods to use. That's pretty easy, too. Let's say you only need to tweak bar and foobar on each item, but the rest gets created as it would with just `Stepford::FactoryGirl.create_list`, so if you wanted to create 5 with two traits `:fancy` and `:light` and only build the bar and build bar's foobar as a stub:
         
     | 
| 
       116 
114 
     | 
    
         | 
| 
       117 
115 
     | 
    
         
             
                Stepford::FactoryGirl.create_list(:foo, 5, :fancy, :light, with_factory_options: {
         
     | 
| 
       118 
     | 
    
         
            -
                  bar: [: 
     | 
| 
       119 
     | 
    
         
            -
                  foobar: [: 
     | 
| 
      
 116 
     | 
    
         
            +
                  bar: [:create_list, :bar],
         
     | 
| 
      
 117 
     | 
    
         
            +
                  foobar: [:create, :foobar, :some_trait, :some_other_trait, blk: -> {block you would send to foobar.create goes here}]
         
     | 
| 
       120 
118 
     | 
    
         
             
                }) do
         
     | 
| 
       121 
119 
     | 
    
         
             
                  # any block you would send to FactoryGirl.create_list(:foo) would go here
         
     | 
| 
       122 
120 
     | 
    
         
             
                end
         
     | 
| 
         @@ -323,6 +321,8 @@ ThoughtBot's Josh Clayton provided some suggestions for this, including using me 
     | 
|
| 
       323 
321 
     | 
    
         
             
                  post.reload
         
     | 
| 
       324 
322 
     | 
    
         
             
                end
         
     | 
| 
       325 
323 
     | 
    
         | 
| 
      
 324 
     | 
    
         
            +
            (Note, the deep_* methods that do this automatically for you, including the reloads.)
         
     | 
| 
      
 325 
     | 
    
         
            +
             
     | 
| 
       326 
326 
     | 
    
         
             
            or referring to created objects through associations, though he said multiple nestings get tricky:
         
     | 
| 
       327 
327 
     | 
    
         | 
| 
       328 
328 
     | 
    
         
             
                factory :post do
         
     | 
| 
         @@ -343,8 +343,6 @@ or referring to created objects through associations, though he said multiple ne 
     | 
|
| 
       343 
343 
     | 
    
         
             
                comment = FactoryGirl.create(:comment, :authored_by_post_author)
         
     | 
| 
       344 
344 
     | 
    
         
             
                comment.author == comment.post.author # true
         
     | 
| 
       345 
345 
     | 
    
         | 
| 
       346 
     | 
    
         
            -
            This is the reason we wrote the Stepford's Factory Girl proxy and helper rspec methods (see above). It automatically determines what needs to be set in what order and does create, create_list or build, build_list, etc. automatically.
         
     | 
| 
       347 
     | 
    
         
            -
             
     | 
| 
       348 
346 
     | 
    
         
             
            ### License
         
     | 
| 
       349 
347 
     | 
    
         | 
| 
       350 
348 
     | 
    
         
             
            Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
         
     | 
| 
         @@ -27,11 +27,8 @@ module Stepford 
     | 
|
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                  def handle_factory_girl_method(m, *args, &block)
         
     | 
| 
       29 
29 
     | 
    
         | 
| 
       30 
     | 
    
         
            -
                    #puts "#{'  ' * @@indent}handling Stepford::FactoryGirl.#{m}(#{args.inspect})" if ::Stepford::FactoryGirl.debug?
         
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
30 
     | 
    
         
             
                    if args && args.size > 0
         
     | 
| 
       33 
31 
     | 
    
         
             
                      # call Stepford::FactoryGirl.* on any not null associations recursively
         
     | 
| 
       34 
     | 
    
         
            -
                      model_sym = args[0].to_sym
         
     | 
| 
       35 
32 
     | 
    
         
             
                      model_class = args[0].to_s.camelize.constantize
         
     | 
| 
       36 
33 
     | 
    
         | 
| 
       37 
34 
     | 
    
         
             
                      args = args.dup # need local version because we'll be dup'ing the options hash to add things to set prior to create/build
         
     | 
| 
         @@ -54,6 +51,9 @@ module Stepford 
     | 
|
| 
       54 
51 
     | 
    
         
             
                      orig_options[:nesting_breadcrumbs] = [] unless orig_options[:nesting_breadcrumbs]
         
     | 
| 
       55 
52 
     | 
    
         
             
                      breadcrumbs = orig_options[:nesting_breadcrumbs]
         
     | 
| 
       56 
53 
     | 
    
         
             
                      breadcrumbs << [args[0]]
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                      orig_options[:to_reload] = [] unless orig_options[:to_reload]
         
     | 
| 
      
 56 
     | 
    
         
            +
                      to_reload = orig_options[:to_reload]
         
     | 
| 
       57 
57 
     | 
    
         | 
| 
       58 
58 
     | 
    
         
             
                      if ::Stepford::FactoryGirl.debug?
         
     | 
| 
       59 
59 
     | 
    
         
             
                        puts "#{breadcrumbs.join('>')} start. args=#{debugargs(args)}"
         
     | 
| 
         @@ -88,16 +88,15 @@ module Stepford 
     | 
|
| 
       88 
88 
     | 
    
         
             
                            blk = method_options.is_a?(Hash) ? method_args_and_options.delete(:blk) : nil
         
     | 
| 
       89 
89 
     | 
    
         
             
                            begin
         
     | 
| 
       90 
90 
     | 
    
         
             
                              if blk
         
     | 
| 
       91 
     | 
    
         
            -
                                #puts "#{'  ' * @@indent}FactoryGirl.__send__(#{method_args_and_options.inspect}, &blk)" if ::Stepford::FactoryGirl.debug?
         
     | 
| 
       92 
91 
     | 
    
         
             
                                options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options, &blk)
         
     | 
| 
       93 
92 
     | 
    
         
             
                              else
         
     | 
| 
       94 
     | 
    
         
            -
                                #puts "#{'  ' * @@indent}FactoryGirl.__send__(#{method_args_and_options.inspect})" if ::Stepford::FactoryGirl.debug?
         
     | 
| 
       95 
93 
     | 
    
         
             
                                options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options)
         
     | 
| 
       96 
94 
     | 
    
         
             
                              end
         
     | 
| 
      
 95 
     | 
    
         
            +
                              to_reload << options[assc_sym]
         
     | 
| 
       97 
96 
     | 
    
         
             
                            rescue ActiveRecord::RecordInvalid => e
         
     | 
| 
       98 
97 
     | 
    
         
             
                              puts "#{breadcrumbs.join('>')}: FactoryGirl.__send__(#{method_args_and_options.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}"
         
     | 
| 
       99 
98 
     | 
    
         
             
                              raise e
         
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
      
 99 
     | 
    
         
            +
                            end
         
     | 
| 
       101 
100 
     | 
    
         
             
                          else
         
     | 
| 
       102 
101 
     | 
    
         
             
                            if reflection.macro == :has_many
         
     | 
| 
       103 
102 
     | 
    
         
             
                              options[assc_sym] = ::Stepford::FactoryGirl.create_list(clas_sym, 2, orig_options)               
         
     | 
| 
         @@ -122,6 +121,7 @@ module Stepford 
     | 
|
| 
       122 
121 
     | 
    
         
             
                    if args.last.is_a?(Hash)
         
     | 
| 
       123 
122 
     | 
    
         
             
                      (args.last).delete(:with_factory_options)
         
     | 
| 
       124 
123 
     | 
    
         
             
                      (args.last).delete(:nesting_breadcrumbs)
         
     | 
| 
      
 124 
     | 
    
         
            +
                      (args.last).delete(:to_reload)
         
     | 
| 
       125 
125 
     | 
    
         
             
                    end
         
     | 
| 
       126 
126 
     | 
    
         | 
| 
       127 
127 
     | 
    
         
             
                    begin
         
     | 
| 
         @@ -130,9 +130,21 @@ module Stepford 
     | 
|
| 
       130 
130 
     | 
    
         
             
                      puts "#{breadcrumbs.join('>')}: FactoryGirl.#{m}(#{args.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}" if defined?(breadcrumbs)
         
     | 
| 
       131 
131 
     | 
    
         
             
                      raise e
         
     | 
| 
       132 
132 
     | 
    
         
             
                    end
         
     | 
| 
       133 
     | 
    
         
            -
             
     | 
| 
      
 133 
     | 
    
         
            +
                    
         
     | 
| 
       134 
134 
     | 
    
         
             
                    if args.last.is_a?(Hash) && defined?(breadcrumbs) && breadcrumbs.size > 0
         
     | 
| 
      
 135 
     | 
    
         
            +
                      # still handling association/subassociation
         
     | 
| 
       135 
136 
     | 
    
         
             
                      args.last[:nesting_breadcrumbs] = breadcrumbs
         
     | 
| 
      
 137 
     | 
    
         
            +
                      args.last[:to_reload] = to_reload
         
     | 
| 
      
 138 
     | 
    
         
            +
                      orig_options[:to_reload] << result
         
     | 
| 
      
 139 
     | 
    
         
            +
                    else
         
     | 
| 
      
 140 
     | 
    
         
            +
                      # ready to return the initially requested instances, so reload children with their parents, in reverse order added
         
     | 
| 
      
 141 
     | 
    
         
            +
                      orig_options[:to_reload].reverse.each do |i|
         
     | 
| 
      
 142 
     | 
    
         
            +
                        begin
         
     | 
| 
      
 143 
     | 
    
         
            +
                          i.reload
         
     | 
| 
      
 144 
     | 
    
         
            +
                        rescue => e
         
     | 
| 
      
 145 
     | 
    
         
            +
                          puts "#{i} reload failed: #{e}\n#{e.backtrace.join("\n")}" if ::Stepford::FactoryGirl.debug?
         
     | 
| 
      
 146 
     | 
    
         
            +
                        end
         
     | 
| 
      
 147 
     | 
    
         
            +
                      end
         
     | 
| 
       136 
148 
     | 
    
         
             
                    end
         
     | 
| 
       137 
149 
     | 
    
         | 
| 
       138 
150 
     | 
    
         
             
                    result
         
     | 
    
        data/lib/stepford/version.rb
    CHANGED