Created
June 9, 2019 09:20
-
-
Save danimal141/6a1af6141d35f6a224ea2249a126e770 to your computer and use it in GitHub Desktop.
factory_bot DSL for ActiveModelSerializers::Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if defined?(FactoryBot) | |
module FactoryBot | |
module Syntax | |
module Default | |
class DSL | |
# Custom DSL for ActiveModelSerializers::Model | |
# Original: https://github.com/thoughtbot/factory_bot/blob/v5.0.2/lib/factory_bot/syntax/default.rb#L15-L26 | |
def serializers_model_factory(name, options = {}, &block) | |
factory = Factory.new(name, options) | |
proxy = FactoryBot::DefinitionProxy.new(factory.definition) | |
if block_given? | |
proxy.instance_eval do | |
skip_create | |
initialize_with { new(attributes) } | |
instance_eval(&block) | |
end | |
end | |
FactoryBot.register_factory(factory) | |
proxy.child_factories.each do |(child_name, child_options, child_block)| | |
parent_factory = child_options.delete(:parent) || name | |
serializers_model_factory(child_name, child_options.merge(parent: parent_factory), &child_block) | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment