1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
# frozen_string_literal: true
module BootstrapForm
module ActionViewExtensions
# This module creates BootstrapForm wrappers around the default form_with
# and form_for methods
#
# Example:
#
# bootstrap_form_for @user do |f|
# f.text_field :name
# end
#
# Example:
#
# bootstrap_form_with model: @user do |f|
# f.text_field :name
# end
module FormHelper
def bootstrap_form_for(record, options={}, &block)
options.reverse_merge!(builder: BootstrapForm::FormBuilder)
with_bootstrap_form_field_error_proc do
form_for(record, options, &block)
end
end
def bootstrap_form_with(options={}, &block)
options.reverse_merge!(builder: BootstrapForm::FormBuilder)
with_bootstrap_form_field_error_proc do
form_with(**options, &block)
end
end
def bootstrap_form_tag(options={}, &block)
options[:acts_like_form_tag] = true
bootstrap_form_for("", options, &block)
end
def bootstrap_fields_for(record_name, record_object=nil, options={}, &block)
options[:builder] = BootstrapForm::FormBuilder
fields_for(record_name, record_object, options, &block)
end
def bootstrap_fields(scope=nil, model: nil, **options, &block)
options[:builder] = BootstrapForm::FormBuilder
fields(scope, model: model, **options, &block)
end
private
def with_bootstrap_form_field_error_proc
original_proc = ActionView::Base.field_error_proc
ActionView::Base.field_error_proc = BootstrapForm.field_error_proc
yield
ensure
ActionView::Base.field_error_proc = original_proc
end
end
end
end
ActiveSupport.on_load(:action_view) do
include BootstrapForm::ActionViewExtensions::FormHelper
end
|