heartcombo / simple_form-bootstrap

Example application with SimpleForm and Twitter Bootstrap
https://simple-form-bootstrap.herokuapp.com
MIT License
795 stars 284 forks source link

Running with Bootstrap 3 & 4 in the same app, insane or possible? #350

Open prognostikos opened 2 years ago

prognostikos commented 2 years ago

We have a mature app using simple_form with Bootstrap 3. We are developing a new set of features where we would like to use Bootstrap 4 or maybe even 5 depending on when we launch the features, with the goal of eventually transitioning the old v3 views to use whatever new version we settle on.

Is there any suggested way of doing this? One option I guess would be to customize all of the wrappers in the generated simpleform bootstrap initializer (e.g. add a `bs4prefix or something like that and then explicity addwrapper: :bs4_vertical_form` or whatever in each call to simple form. I would like to avoid this if possible, but not if it's the only sane way.

I guess another option would be to remove the initializers and create two separate form builders and move all the initialization configuration code into the initialize method of each form builder. That's probably not great for performance but it may not be enough of an issue to matter.

Does anyone have any other ideas or suggestions? Thanks in advance.

metade commented 2 years ago

Hey @prognostikos, we're looking to do something similar, i.e. have an app running both Bootstrap 3 and 5 while we transition pages in version 3 to version 5.

Did you come up with a solution?

I've been scratching my head looking at https://github.com/heartcombo/simple_form/blob/main/lib/simple_form.rb#L287-L290 but as the configuration is defined on the SimpleForm module it's not easy to have multiple configurations running at the same time.

prognostikos commented 2 years ago

@metade we ended up doing the second option - moving config out of initializers and into separate form builders. It doesn't seem to have much of an impact on performance.

It's not pretty, but here's what has worked:

class Bootstrap4FormBuilder < SimpleForm::FormBuilder
  def initialize(*)
    configure_boostrap4_wrappers
    super
    @object = convert_to_model(@object)
    @defaults = options[:defaults]
    @wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
  end

  def configure_boostrap4_wrappers
    # all of the config code from the initializers
  end
end
metade commented 2 years ago

Thank you @prognostikos - that leaves us plenty to think about!