-1

Я хочу сделать следующее:Динамически генерировать завод атрибуты

classes = ["WelcomeMailing", "NoticeMailing", "FeedbackMailing"] #...... 

FactoryGirl.define do 
    classes.each do |tclass| 

    cl_attributes = ["body", "subject", "description", "title"] 

    # this registers dynamically sequences, which i can use later (this works already!) 
    define_sequences(cl_attributes.map{|a| tclass.underscore + "_" + a}) 

    factory tclass.underscore.to_sym do 
     cl_attributes.each do |aattr| 
     # here i want to generate the attributes of the factory class dynamically.. 

     aattr { generate (tclass.underscore + "_" + aattr).to_sym } 
     # but it doesnt work 
     # => pry(main)> FactoryGirl.create(:custom_mailing_draft) 
     # FactoryGirl::AttributeDefinitionError: Attribute already defined: aattr 

     # or 
     eval(aattr) { generate (tclass.underscore + "_" + aattr).to_sym }   # also not with 
     # =>factory_girl/definition_proxy.rb:36:in `add_attribute': Both value and block given (FactoryGirl::AttributeDefinitionError) 
     end 
    end 
    end 
end 

В конце я хочу, чтобы динамически создавать заводы (так как они почти все одинаковые структуры (наследуемые классы)). Но, как вы видите в коде, настройка атрибутов не работает.

Любые предложения? Спасибо!

ответ

0

решаемые его самостоятельно с:

classes.each do |tclass| 
    FactoryGirl.define do 
     define_sequences(attributes.map{|a| tclass.underscore + "_" + a}) 

     clstr = "factory :#{tclass.underscore} do;" 
     attributes.each do |aattr| 
     clstr += "#{aattr} { generate :#{(tclass.underscore + "_" + aattr).to_sym} };" 
     end 
     clstr += "end" 

     eval(clstr) 
    end 
    end 
Смежные вопросы