-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support alternative ORMs (DataMapper) #372
Comments
There's currently no way to override it for all factories at once (yet), but you can override |
In the meantime, instead of defining class CreateForDataMapper
def initialize
@default_strategy = FactoryGirl::Strategy::Create.new
end
delegate :association, to: :@default_strategy
def result(evaluation)
evaluation.singleton_class.send :define_method, :create do |instance|
instance.save ||
raise(instance.errors.send(:errors).map{|attr,errors| "- #{attr}: #{errors}" }.join("\n"))
end
@default_strategy.result(evaluation)
end
end
FactoryGirl.register_strategy(:create, CreateForDataMapper) I'm not 100% sure that this code would work, but I'm guessing it will. So, what's it doing? I'm overriding FactoryGirl's create strategy with a new class that follows 'Composition Over Inheritance' to piggy-back off of This allows for the behavior of FactoryGirl's default create strategy to change (in case we add any more callbacks, etc.) without impeding your use of those features. The downside to this is that it'll effectively disable So, the final answer is:
Make sense? There's a lot of options here, and all of them will hopefully reduce the amount of strain on you and get rid of those nasty monkey-patches. Let me know if you have any questions! |
Thanks for the detailed explanation. (1) is my preferred solution, (2) is not practical since i have ~30 factories and still adding more. (3) is something i have thought about too (But in DM #save! saves the record without executing callbacks and validations). |
I can confirm that Joshua's |
This is now supported in the current (as of this writing) version of FactoryGirl by using a custom persistence method globally. An example of what would work here for DataMapper specifically:
Hope that helps someone who lands here from a Google search. |
Hi,
i like FactoryGirl and i am using it together with DataMapper. But making FG work with DM is sometimes a bit messy (monkey patching) and time consuming (e.g. when upgrading FG). My last monkey-patches looked like this:
It would be nice to have a configuration option for the default create-method (or is there already a solution hidden somewhere? could not find one yet). Thanks :)
The text was updated successfully, but these errors were encountered: