lib/hermes/builders.rb in hermes-0.5.1 vs lib/hermes/builders.rb in hermes-0.6.0

- old
+ new

@@ -1,24 +1,24 @@ -# Rails developers have had bad experience with fixtures since a long time -# due to several reasons, including misuse. +# Rails developers have had bad experience with fixtures for a long time +# due to misuse. # # This misuse of fixtures is often characterized by a huge ammount of # fixtures, causing a lot of data to maintain and dependence between tests. # In my experience working (and rescueing) different applications, 80% of # these fixtures are used only by 20% of tests. # # An example of such tests is a test that assures a given SQL query with # GROUP BY and ORDER BY conditions returns the correct result set. As expected, # we need a huge amount of data in this test which we usually don't need in -# order tests. +# other tests. # # For such scenarios, factories are a fine solution. They won't clutter # all your database since they are created for these specific tests and they # are also easier to maintain. # # I believe this was the primary reason for the Rails community to strongly -# adopt factories builders as we saw in the couple two years ago. +# adopt factories builders as we saw in the last years. # # However, factories are also misused. It is common to see people creating # a huge amount of data with factories before each test in their integration # suite, causing their whole test suite to be slow, while fixtures would # work great for this purpose. @@ -103,11 +103,15 @@ def retrieve(scope, name, method, options) if builder = @@builders[name.to_sym] klass, block = builder - hash = block ? block.bind(scope).call : {} + hash = if block + scope.instance_exec(&block) + else + {} + end hash.symbolize_keys! hash.merge!(options || {}) hash.delete(:id) [klass, hash] @@ -121,11 +125,11 @@ def load_attributes_from_fixture(fixture_type, fixture_name, block) lambda { send(fixture_type, fixture_name).attributes.symbolize_keys.merge!(block ? block.call : {}) } end end - def respond_to?(method) + def respond_to?(method, include_private=false) case method.to_s when BUILDER_REGEX Builders.has_builder?($2) when ATTRIBUTES_REGEX Builders.has_builder?($1) @@ -138,10 +142,10 @@ case method.to_s when BUILDER_REGEX return super unless Builders.has_builder?($2) klass, hash = Builders.retrieve(self, $2, method, args.first) object = klass.new - object.assign_attributes hash, :without_protection => true + object.assign_attributes hash object.send("save#{$3}") if $1 == "create" object when ATTRIBUTES_REGEX return super unless Builders.has_builder?($1) Builders.retrieve(self, $1, method, args.first)[1]