From bba529dad0330fdb8e333db33249bfc59a9ffd84 Mon Sep 17 00:00:00 2001 From: "Gary S. Weaver" Date: Tue, 13 Nov 2012 10:12:22 -0500 Subject: [PATCH] version 0.12.2. now reloads children with parents automatically, as it should have --- README.md | 6 +++--- lib/stepford/factory_girl.rb | 26 +++++++++++++++++++------- lib/stepford/version.rb | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 133c95c..bb36739 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Stepford ===== -Now getting started with FactoryGirl is even more simple and DRY! +FactoryGirl = so easy now Stepford is an automatic required (non-null or presence validated) association resolver and factory generator for [FactoryGirl][factory_girl]. @@ -321,6 +321,8 @@ ThoughtBot's Josh Clayton provided some suggestions for this, including using me post.reload end +(Note, the deep_* methods that do this automatically for you, including the reloads.) + or referring to created objects through associations, though he said multiple nestings get tricky: factory :post do @@ -341,8 +343,6 @@ or referring to created objects through associations, though he said multiple ne comment = FactoryGirl.create(:comment, :authored_by_post_author) comment.author == comment.post.author # true -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. - ### License Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic]. diff --git a/lib/stepford/factory_girl.rb b/lib/stepford/factory_girl.rb index 993ed10..6592820 100644 --- a/lib/stepford/factory_girl.rb +++ b/lib/stepford/factory_girl.rb @@ -27,11 +27,8 @@ def configure(&blk); class_eval(&blk); end def handle_factory_girl_method(m, *args, &block) - #puts "#{' ' * @@indent}handling Stepford::FactoryGirl.#{m}(#{args.inspect})" if ::Stepford::FactoryGirl.debug? - if args && args.size > 0 # call Stepford::FactoryGirl.* on any not null associations recursively - model_sym = args[0].to_sym model_class = args[0].to_s.camelize.constantize 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 @@ def handle_factory_girl_method(m, *args, &block) orig_options[:nesting_breadcrumbs] = [] unless orig_options[:nesting_breadcrumbs] breadcrumbs = orig_options[:nesting_breadcrumbs] breadcrumbs << [args[0]] + + orig_options[:to_reload] = [] unless orig_options[:to_reload] + to_reload = orig_options[:to_reload] if ::Stepford::FactoryGirl.debug? puts "#{breadcrumbs.join('>')} start. args=#{debugargs(args)}" @@ -88,16 +88,15 @@ def handle_factory_girl_method(m, *args, &block) blk = method_options.is_a?(Hash) ? method_args_and_options.delete(:blk) : nil begin if blk - #puts "#{' ' * @@indent}FactoryGirl.__send__(#{method_args_and_options.inspect}, &blk)" if ::Stepford::FactoryGirl.debug? options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options, &blk) else - #puts "#{' ' * @@indent}FactoryGirl.__send__(#{method_args_and_options.inspect})" if ::Stepford::FactoryGirl.debug? options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options) end + to_reload << options[assc_sym] rescue ActiveRecord::RecordInvalid => e puts "#{breadcrumbs.join('>')}: FactoryGirl.__send__(#{method_args_and_options.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}" raise e - end + end else if reflection.macro == :has_many options[assc_sym] = ::Stepford::FactoryGirl.create_list(clas_sym, 2, orig_options) @@ -122,6 +121,7 @@ def handle_factory_girl_method(m, *args, &block) if args.last.is_a?(Hash) (args.last).delete(:with_factory_options) (args.last).delete(:nesting_breadcrumbs) + (args.last).delete(:to_reload) end begin @@ -130,9 +130,21 @@ def handle_factory_girl_method(m, *args, &block) puts "#{breadcrumbs.join('>')}: FactoryGirl.#{m}(#{args.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}" if defined?(breadcrumbs) raise e end - + if args.last.is_a?(Hash) && defined?(breadcrumbs) && breadcrumbs.size > 0 + # still handling association/subassociation args.last[:nesting_breadcrumbs] = breadcrumbs + args.last[:to_reload] = to_reload + orig_options[:to_reload] << result + else + # ready to return the initially requested instances, so reload children with their parents, in reverse order added + orig_options[:to_reload].reverse.each do |i| + begin + i.reload + rescue => e + puts "#{i} reload failed: #{e}\n#{e.backtrace.join("\n")}" if ::Stepford::FactoryGirl.debug? + end + end end result diff --git a/lib/stepford/version.rb b/lib/stepford/version.rb index 7c2ce77..db36707 100644 --- a/lib/stepford/version.rb +++ b/lib/stepford/version.rb @@ -1,3 +1,3 @@ module Stepford - VERSION = '0.12.1' + VERSION = '0.12.2' end