Skip to content

Commit

Permalink
version 0.12.2. now reloads children with parents automatically, as i…
Browse files Browse the repository at this point in the history
…t should have
  • Loading branch information
garysweaver committed Nov 13, 2012
1 parent 96cb8c9 commit bba529d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
@@ -1,7 +1,7 @@
Stepford 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]. Stepford is an automatic required (non-null or presence validated) association resolver and factory generator for [FactoryGirl][factory_girl].


Expand Down Expand Up @@ -321,6 +321,8 @@ ThoughtBot's Josh Clayton provided some suggestions for this, including using me
post.reload post.reload
end 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: or referring to created objects through associations, though he said multiple nestings get tricky:


factory :post do factory :post do
Expand All @@ -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 = FactoryGirl.create(:comment, :authored_by_post_author)
comment.author == comment.post.author # true 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 ### License


Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic]. Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
Expand Down
26 changes: 19 additions & 7 deletions lib/stepford/factory_girl.rb
Expand Up @@ -27,11 +27,8 @@ def configure(&blk); class_eval(&blk); end


def handle_factory_girl_method(m, *args, &block) 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 if args && args.size > 0
# call Stepford::FactoryGirl.* on any not null associations recursively # call Stepford::FactoryGirl.* on any not null associations recursively
model_sym = args[0].to_sym
model_class = args[0].to_s.camelize.constantize 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 args = args.dup # need local version because we'll be dup'ing the options hash to add things to set prior to create/build
Expand All @@ -54,6 +51,9 @@ def handle_factory_girl_method(m, *args, &block)
orig_options[:nesting_breadcrumbs] = [] unless orig_options[:nesting_breadcrumbs] orig_options[:nesting_breadcrumbs] = [] unless orig_options[:nesting_breadcrumbs]
breadcrumbs = orig_options[:nesting_breadcrumbs] breadcrumbs = orig_options[:nesting_breadcrumbs]
breadcrumbs << [args[0]] breadcrumbs << [args[0]]

orig_options[:to_reload] = [] unless orig_options[:to_reload]
to_reload = orig_options[:to_reload]


if ::Stepford::FactoryGirl.debug? if ::Stepford::FactoryGirl.debug?
puts "#{breadcrumbs.join('>')} start. args=#{debugargs(args)}" puts "#{breadcrumbs.join('>')} start. args=#{debugargs(args)}"
Expand Down Expand Up @@ -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 blk = method_options.is_a?(Hash) ? method_args_and_options.delete(:blk) : nil
begin begin
if blk 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) options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options, &blk)
else else
#puts "#{' ' * @@indent}FactoryGirl.__send__(#{method_args_and_options.inspect})" if ::Stepford::FactoryGirl.debug?
options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options) options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options)
end end
to_reload << options[assc_sym]
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
puts "#{breadcrumbs.join('>')}: FactoryGirl.__send__(#{method_args_and_options.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}" puts "#{breadcrumbs.join('>')}: FactoryGirl.__send__(#{method_args_and_options.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}"
raise e raise e
end end
else else
if reflection.macro == :has_many if reflection.macro == :has_many
options[assc_sym] = ::Stepford::FactoryGirl.create_list(clas_sym, 2, orig_options) options[assc_sym] = ::Stepford::FactoryGirl.create_list(clas_sym, 2, orig_options)
Expand All @@ -122,6 +121,7 @@ def handle_factory_girl_method(m, *args, &block)
if args.last.is_a?(Hash) if args.last.is_a?(Hash)
(args.last).delete(:with_factory_options) (args.last).delete(:with_factory_options)
(args.last).delete(:nesting_breadcrumbs) (args.last).delete(:nesting_breadcrumbs)
(args.last).delete(:to_reload)
end end


begin begin
Expand All @@ -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) puts "#{breadcrumbs.join('>')}: FactoryGirl.#{m}(#{args.inspect}): #{e}#{::Stepford::FactoryGirl.debug? ? "\n#{e.backtrace.join("\n")}" : ''}" if defined?(breadcrumbs)
raise e raise e
end end

if args.last.is_a?(Hash) && defined?(breadcrumbs) && breadcrumbs.size > 0 if args.last.is_a?(Hash) && defined?(breadcrumbs) && breadcrumbs.size > 0
# still handling association/subassociation
args.last[:nesting_breadcrumbs] = breadcrumbs 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 end


result result
Expand Down
2 changes: 1 addition & 1 deletion lib/stepford/version.rb
@@ -1,3 +1,3 @@
module Stepford module Stepford
VERSION = '0.12.1' VERSION = '0.12.2'
end end

0 comments on commit bba529d

Please sign in to comment.