reform
reform copied to clipboard
Polymorphic Association
I'm trying to build a form object for a quite nested structure:
class Page
include Mongoid::Document
embeds_many :sections
end
class Section
include Mongoid::Document
embedded_in :page
embeds_many :page_modules
end
class PageModule
include Mongoid::Document
embedded_in :section
end
class SomePageModule < PageModule
end
The form for this structure looks something like this:
class PageForm < Reform::Form
collection :sections do
collection :page_modules do
# need to set up properties depending on the class of the current page module
end
end
end
If this is possible (for example something like...)
collection :page_modules, form: lambda {|model| "#{model.class.name}Form".constantize}
then i've overlooked it. This would be an awesome feature.
I managed to get around this problem by defining this:
class PageModuleForm
def self.new(object)
"#{object.class.name}Form".constantize.new
end
end
But this is a dirty hack for something that should be pretty straight forward to implement in reform. I'll try to find the time to implement it and send you a pull request.
Cool idea!!! :heart:
I'll try to get it into 1.1.1?
Looks like this has been added to Reform so closing this.
hi @fran-worley, could you please tell me where this has been added? I'm on Reform 2.2.4 (Disposable 0.4.3) and still can't use anything other than an actual class as a parameter for form:
.
Thanks :)
I'm also interested @fran-worley could you point us towards a way to achieve this?