Module: FactoryHen
- Defined in:
- lib/factory_hen.rb,
lib/factory_hen/version.rb
Overview
FactoryHen is a factory for creating valid models
Inspired by FactoryGirl and done because the Rails 5 support wasnt implemented yet.
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .configure {|@config| ... } ⇒ Object
- .create(model, params = {}) ⇒ Object
- .new(model, params = {}) ⇒ Object
- .params(model, params = {}) ⇒ Object
Class Method Details
.configure {|@config| ... } ⇒ Object
21 22 23 24 |
# File 'lib/factory_hen.rb', line 21 def self.configure @config ||= Configuration.new yield @config end |
.create(model, params = {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/factory_hen.rb', line 26 def self.create(model, params={}) obj = build(model, params) obj.save obj end |
.new(model, params = {}) ⇒ Object
32 33 34 |
# File 'lib/factory_hen.rb', line 32 def self.new(model, params={}) build(model, params) end |
.params(model, params = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/factory_hen.rb', line 36 def self.params(model, params={}) defaults = @config.factories[model].call || {} combined = defaults.merge(params) # Check if any params needs to be saved saved = combined.each_with_object({}) do |(l, v), collection| if v.respond_to?(:save) key = "#{v.class.name.downcase}_id".to_s v.save collection[key] = v.id end end combined.merge(saved) end |