reform icon indicating copy to clipboard operation
reform copied to clipboard

Polymorphic Association

Open repomaa opened this issue 9 years ago • 5 comments

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.

repomaa avatar Sep 03 '14 09:09 repomaa

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.

repomaa avatar Sep 03 '14 14:09 repomaa

Cool idea!!! :heart:

I'll try to get it into 1.1.1?

apotonick avatar Sep 03 '14 22:09 apotonick

Looks like this has been added to Reform so closing this.

fran-worley avatar Aug 09 '16 16:08 fran-worley

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 :)

rickdrumond avatar Aug 08 '17 20:08 rickdrumond

I'm also interested @fran-worley could you point us towards a way to achieve this?

Systho avatar Oct 05 '17 07:10 Systho