Reputation: 10111
I just copied factory_bot's model_generator to my file #lib/generators/factory_bot/model/model_generator.rb
This was going to be my baseline to start making changes. I was able to prove that it was reading my file by changing
require "generators/factory_bot" require "factory_bot_rails"
module FactoryBot
module Generators
class ModelGenerator < FactoryBot::Generators::Base
puts 'my file'
def factory_definition
<<~RUBY
factory :#{factory_name}#{explicit_class_option} do
#{factory_attributes.gsub(/^/, " ")} # HERE IS WHERE I AM CALLING factory_attributes
end
RUBY
end
def factory_attributes # THIS METHOS IS NOT FOUND
attributes.map { |attribute|
"#{attribute.name} { #{attribute.default.inspect} }"
}.join("\n")
end
end
end
end
I am able to get my puts value but I also get this error
undefined local variable or method `factory_name' for #FactoryBot::Generators::ModelGenerator:0x00007f90c367b7f8 (NameError)
I am unsure how to move forward.
Upvotes: 0
Views: 634
Reputation: 10111
you should try not do add to lib/generators/factory_bot/model/model_generator.rb
file but you should create custom factories at lib/templates/factory_bot/model/factories.erb
FactoryBot.define do
factory :<%= "#{class_name.gsub("::", "").underscore}#{explicit_class_option}" %> do
<%=
attributes.map { |attribute|
results = " #{attribute.name} "
if attribute.name == 'name'
results = results + "{ Faker::Name.name }"
elsif
...
else
results = results + "{ #{attribute.default.inspect} }"
end
results
}.join("\n")
%>
end
end
Upvotes: 3