Skip to content

Commit

Permalink
Separation of inputs into separate more maintainable files (bootstrap…
Browse files Browse the repository at this point in the history
…-ruby#518)

* TextField example separation

* Move all form inputs to separate files

* Revert Gemfile

* Add rich_text_area support back in, rebase against master

* Add changelog entry

* Only include `rich_text_area` if Rails version >= 6

* Add an unwrapped field test
  • Loading branch information
simmerz authored and lcreid committed Feb 18, 2019
1 parent 78cb00b commit 3a825cf
Show file tree
Hide file tree
Showing 53 changed files with 863 additions and 352 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
### New features

* [#508] Support `rich_text_area` AKA the Trix editor on Rails 6+.
* Your contribution here!
* [#518] Move all inputs to separate, more maintainable files.

### Bugfixes

Expand Down
2 changes: 1 addition & 1 deletion bootstrap_deferred_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "./test_helper"

class BootstrapDeferredBuilderTest < ActionView::TestCase
include BootstrapForm::Helper
include BootstrapForm::ActionViewExtensions::FormHelper

setup :setup_test_fixture

Expand Down
2 changes: 1 addition & 1 deletion bootstrap_error_rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "test_helper"

class BootstrapErrorRenderingTest < ActionView::TestCase
include BootstrapForm::Helper
include BootstrapForm::ActionViewExtensions::FormHelper

def setup
setup_test_fixture
Expand Down
3 changes: 2 additions & 1 deletion bootstrap_form.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 2.2.2"

s.add_dependency "rails", ">= 5.0"
s.add_dependency("actionpack", ">= 5.0")
s.add_dependency("activemodel", ">= 5.0")
end
31 changes: 23 additions & 8 deletions lib/bootstrap_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@
require Gem::Specification.find_by_name("actiontext").gem_dir + # rubocop:disable Rails/DynamicFindBy
"/app/helpers/action_text/tag_helper"
end
require "bootstrap_form/form_builder"
require "bootstrap_form/helper"
require "action_view"
require "action_pack"
require "bootstrap_form/action_view_extensions/form_helper"

module BootstrapForm
module Rails
class Engine < ::Rails::Engine
end
extend ActiveSupport::Autoload

eager_autoload do
autoload :FormBuilder
autoload :Inputs
autoload :Helpers
end

def self.eager_load!
super
BootstrapForm::Helpers.eager_load!
BootstrapForm::Inputs.eager_load!
end
end

ActiveSupport.on_load(:action_view) do
include BootstrapForm::Helper
mattr_accessor :field_error_proc
# rubocop:disable Style/ClassVars
@@field_error_proc = proc do |html_tag, _instance_tag|
html_tag
end
# rubocop:enable Style/ClassVars
end

require "bootstrap_form/engine" if defined?(Rails)
71 changes: 71 additions & 0 deletions lib/bootstrap_form/action_view_extensions/form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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)

options = process_options(options)

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)

options = process_options(options)

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

private

def process_options(options)
options[:html] ||= {}
options[:html][:role] ||= "form"

options[:layout] == :inline &&
options[:html][:class] = [options[:html][:class], "form-inline"].compact.join(" ")

options
end

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
37 changes: 0 additions & 37 deletions lib/bootstrap_form/aliasing.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/bootstrap_form/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "rails/railtie"

module BootstrapForm
class Engine < Rails::Engine
config.eager_load_namespaces << BootstrapForm
config.autoload_paths << File.expand_path("lib", __dir__)
end
end
Loading

0 comments on commit 3a825cf

Please sign in to comment.