rails_bootstrap_form 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/app/assets/stylesheets/rails_bootstrap_form.css +1 -0
  4. data/demo/.byebug_history +4 -0
  5. data/demo/.ruby-version +1 -0
  6. data/demo/Rakefile +10 -0
  7. data/demo/app/assets/config/manifest.js +1 -0
  8. data/demo/app/assets/stylesheets/application.scss +3 -0
  9. data/demo/app/channels/application_cable/channel.rb +8 -0
  10. data/demo/app/channels/application_cable/connection.rb +8 -0
  11. data/demo/app/controllers/application_controller.rb +6 -0
  12. data/demo/app/controllers/users_controller.rb +70 -0
  13. data/demo/app/helpers/application_helper.rb +6 -0
  14. data/demo/app/jobs/application_job.rb +6 -0
  15. data/demo/app/mailers/application_mailer.rb +8 -0
  16. data/demo/app/models/address.rb +10 -0
  17. data/demo/app/models/application_record.rb +7 -0
  18. data/demo/app/models/country.rb +17 -0
  19. data/demo/app/models/fruit.rb +14 -0
  20. data/demo/app/models/skill.rb +21 -0
  21. data/demo/app/models/user.rb +20 -0
  22. data/demo/app/models/user_skill.rb +8 -0
  23. data/demo/app/views/layouts/application.html.erb +15 -0
  24. data/demo/app/views/users/_form.html.erb +5 -0
  25. data/demo/app/views/users/_form_without_bootstrap_helpers.html.erb +49 -0
  26. data/demo/app/views/users/_vertical_form.html.erb +33 -0
  27. data/demo/app/views/users/edit.html.erb +1 -0
  28. data/demo/app/views/users/index.html.erb +42 -0
  29. data/demo/app/views/users/new.html.erb +1 -0
  30. data/demo/bin/bundle +111 -0
  31. data/demo/bin/rails +8 -0
  32. data/demo/bin/rake +8 -0
  33. data/demo/bin/setup +37 -0
  34. data/demo/config/application.rb +28 -0
  35. data/demo/config/boot.rb +9 -0
  36. data/demo/config/cable.yml +10 -0
  37. data/demo/config/database.yml +25 -0
  38. data/demo/config/environment.rb +9 -0
  39. data/demo/config/environments/development.rb +74 -0
  40. data/demo/config/environments/production.rb +97 -0
  41. data/demo/config/environments/test.rb +64 -0
  42. data/demo/config/initializers/assets.rb +16 -0
  43. data/demo/config/initializers/content_security_policy.rb +29 -0
  44. data/demo/config/initializers/filter_parameter_logging.rb +12 -0
  45. data/demo/config/initializers/inflections.rb +20 -0
  46. data/demo/config/initializers/permissions_policy.rb +17 -0
  47. data/demo/config/initializers/rails_bootstrap_form.rb +10 -0
  48. data/demo/config/locales/en.rb +16 -0
  49. data/demo/config/puma.rb +47 -0
  50. data/demo/config/routes.rb +9 -0
  51. data/demo/config/storage.yml +34 -0
  52. data/demo/config.ru +10 -0
  53. data/demo/db/migrate/20230514054308_create_countries.rb +12 -0
  54. data/demo/db/migrate/20230514054851_create_fruits.rb +12 -0
  55. data/demo/db/migrate/20230514055237_create_users.rb +21 -0
  56. data/demo/db/migrate/20230514055840_create_addresses.rb +17 -0
  57. data/demo/db/migrate/20230514060556_create_skills.rb +12 -0
  58. data/demo/db/migrate/20230514061100_create_user_skills.rb +13 -0
  59. data/demo/db/schema.rb +69 -0
  60. data/demo/db/seeds.rb +15 -0
  61. data/demo/public/favicon.ico +0 -0
  62. data/lib/generators/rails_bootstrap_form/install_generator.rb +25 -0
  63. data/lib/generators/rails_bootstrap_form/templates/install.rb +10 -0
  64. data/lib/rails_bootstrap_form/action_view_extensions/bootstrap_form_helper.rb +54 -0
  65. data/lib/rails_bootstrap_form/bootstrap_form_builder.rb +24 -0
  66. data/lib/rails_bootstrap_form/bootstrap_form_options.rb +71 -0
  67. data/lib/rails_bootstrap_form/configuration.rb +26 -0
  68. data/lib/rails_bootstrap_form/version.rb +1 -1
  69. data/lib/rails_bootstrap_form.rb +20 -1
  70. metadata +66 -1
data/demo/db/seeds.rb ADDED
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ ::Country::DEFAULT_OPTIONS.each do |country|
6
+ ::Country.find_or_create_by(name: country)
7
+ end
8
+
9
+ ::Fruit::DEFAULT_OPTIONS.each do |fruit|
10
+ ::Fruit.find_or_create_by(name: fruit)
11
+ end
12
+
13
+ ::Skill::DEFAULT_OPTIONS.each do |skill|
14
+ ::Skill.find_or_create_by(name: skill)
15
+ end
File without changes
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ require "rails/generators"
6
+
7
+ module RailsBootstrapForm
8
+ module Generators
9
+ class InstallGenerator < Rails::Generators::Base
10
+ desc "This generator creates an initializer file for configuration of RailsBootstrapForm"
11
+
12
+ source_root File.expand_path("../templates", __FILE__)
13
+
14
+ def create_initializer_file
15
+ template "install.rb", initializer_path
16
+ end
17
+
18
+ private
19
+
20
+ def initializer_path
21
+ File.join("config/initializers", "rails_bootstrap_form.rb")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ # Be sure to restart your server when you modify this file.
6
+
7
+ RailsBootstrapForm.configure do |config|
8
+ # to make forms non-compliant with W3C.
9
+ config.default_form_attributes = {role: "form", novalidate: true}
10
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module ActionViewExtensions
7
+ # This module creates `RailsBootstrapForm` wrappers around the default form_with
8
+ # and form_for methods.
9
+ #
10
+ # Example:
11
+ #
12
+ # bootstrap_form_for @user do |f|
13
+ # f.text_field :name
14
+ # end
15
+ #
16
+ # Example:
17
+ #
18
+ # bootstrap_form_with model: @user do |f|
19
+ # f.text_field :name
20
+ # end
21
+ #
22
+ module BootstrapFormHelper
23
+ def bootstrap_form_for(record, options = {}, &block)
24
+ options.reverse_merge!(builder: RailsBootstrapForm::BootstrapFormBuilder)
25
+
26
+ supress_form_field_errors do
27
+ form_for(record, options, &block)
28
+ end
29
+ end
30
+
31
+ def bootstrap_form_with(options = {}, &block)
32
+ options.reverse_merge!(builder: RailsBootstrapForm::BootstrapFormBuilder)
33
+
34
+ supress_form_field_errors do
35
+ form_with(**options, &block)
36
+ end
37
+ end
38
+
39
+ def supress_form_field_errors
40
+ original_proc = ActionView::Base.field_error_proc
41
+ ActionView::Base.field_error_proc = RailsBootstrapForm.field_error_proc
42
+ yield
43
+ ensure
44
+ ActionView::Base.field_error_proc = original_proc
45
+ end
46
+
47
+ private :supress_form_field_errors
48
+ end
49
+ end
50
+ end
51
+
52
+ ActiveSupport.on_load(:action_view) do
53
+ include RailsBootstrapForm::ActionViewExtensions::BootstrapFormHelper
54
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
7
+ delegate :capture, :concat, :tag, to: :@template
8
+
9
+ attr_accessor :bootstrap_form_options
10
+
11
+ def initialize(object_name, object, template, options)
12
+ @bootstrap_form_options = RailsBootstrapForm::BootstrapFormOptions.new(options.delete(:bootstrap_form))
13
+ apply_default_form_options(options)
14
+ super(object_name, object, template, options)
15
+ end
16
+
17
+ def apply_default_form_options(options)
18
+ options[:html] ||= {}
19
+ options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
20
+ end
21
+
22
+ private :apply_default_form_options
23
+ end
24
+ end
@@ -0,0 +1,71 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ # Container for bootstrap specific form builder options. It controls options
7
+ # that define form layout, grid sizing, and few other configurable options.
8
+ # They are passed-in into form builder helper and field helpers via
9
+ # `:bootstrap_form` option.
10
+ #
11
+ # For example:
12
+ #
13
+ # bootstrap_form_with model: @user, bootstrap_form: {layout: :inline} do |f|
14
+ # f.text_field :email, bootstrap_form: {label: {text: "Your email"}}
15
+ # end
16
+ #
17
+ class BootstrapFormOptions
18
+
19
+ # Controls form and field layout. It can be "vertical, "horizontal", or "inline".
20
+ # It can also be "floating" if field should have floating labels.
21
+ attr_accessor :layout
22
+
23
+ def initialize(options = {})
24
+ set_defaults
25
+ set_bootstrap_form_options(options)
26
+ end
27
+
28
+ def horizontal?
29
+ @layout.to_s == "horizontal"
30
+ end
31
+
32
+ def inline?
33
+ @layout.to_s == "inline"
34
+ end
35
+
36
+ def vertical?
37
+ @layout.to_s == "vertical"
38
+ end
39
+
40
+ def floating?
41
+ @layout.to_s == "floating"
42
+ end
43
+
44
+ # This will return a copy of `BootstrapFormOptions` object with new options set
45
+ # that don't affect original object. This way we can have options specific
46
+ # to a given form field. For example, we can change grid just for one field:
47
+ #
48
+ # bootstrap_form_with model: @user do |f|
49
+ # f.text_field :email, bootstrap_form: {label_col_class: "col-md-6", control_col_class: "col-md-6"}
50
+ # f.password_field :password
51
+ # end
52
+ #
53
+ def scoped(options = {})
54
+ scope = clone
55
+ scope.set_bootstrap_form_options(options)
56
+ scope
57
+ end
58
+
59
+ def set_bootstrap_form_options(options)
60
+ options.is_a?(Hash) && options.each do |key, value|
61
+ public_send("#{key}=", value)
62
+ end
63
+ end
64
+
65
+ def set_defaults
66
+ @layout = "vertical"
67
+ end
68
+
69
+ private :set_defaults
70
+ end
71
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ class Configuration
7
+ # Default HTML attributes which will applied to all forms build
8
+ # using RailsBootstrapForm.
9
+ def default_form_attributes=(attributes)
10
+ case attributes
11
+ when nil
12
+ @default_form_attributes = {}
13
+ when Hash
14
+ @default_form_attributes = attributes
15
+ else
16
+ raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}"
17
+ end
18
+ end
19
+
20
+ def default_form_attributes
21
+ return @default_form_attributes if defined?(@default_form_attributes)
22
+
23
+ {}
24
+ end
25
+ end
26
+ end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.1.0".freeze
6
+ VERSION = "0.2.0".freeze
7
7
  REQUIRED_RAILS_VERSION = "~> 7.0".freeze
8
8
  end
@@ -4,19 +4,38 @@
4
4
 
5
5
  require "action_view"
6
6
  require "action_pack"
7
+ require "rails_bootstrap_form/action_view_extensions/bootstrap_form_helper"
7
8
 
8
9
  module RailsBootstrapForm
9
10
  extend ActiveSupport::Autoload
10
11
 
11
12
  eager_autoload do
12
-
13
+ autoload :Configuration
14
+ autoload :BootstrapFormOptions
15
+ autoload :BootstrapFormBuilder
13
16
  end
14
17
 
15
18
  class << self
16
19
  def eager_load!
17
20
  super
18
21
  end
22
+
23
+ def config
24
+ @config ||= RailsBootstrapForm::Configuration.new
25
+ end
26
+
27
+ def configure
28
+ yield config
29
+ end
30
+ end
31
+
32
+ # Override `field_error_proc` to suppress errors coming from user defined
33
+ # `field_error_proc`.
34
+ mattr_accessor :field_error_proc
35
+ @@field_error_proc = proc do |html_tag, _instance_tag|
36
+ html_tag
19
37
  end
38
+
20
39
  end
21
40
 
22
41
  require "rails_bootstrap_form/engine" if defined?(Rails)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_bootstrap_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)
@@ -40,9 +40,74 @@ files:
40
40
  - LICENSE.txt
41
41
  - README.md
42
42
  - Rakefile
43
+ - app/assets/stylesheets/rails_bootstrap_form.css
44
+ - demo/.byebug_history
45
+ - demo/.ruby-version
46
+ - demo/Rakefile
47
+ - demo/app/assets/config/manifest.js
48
+ - demo/app/assets/stylesheets/application.scss
49
+ - demo/app/channels/application_cable/channel.rb
50
+ - demo/app/channels/application_cable/connection.rb
51
+ - demo/app/controllers/application_controller.rb
52
+ - demo/app/controllers/users_controller.rb
53
+ - demo/app/helpers/application_helper.rb
54
+ - demo/app/jobs/application_job.rb
55
+ - demo/app/mailers/application_mailer.rb
56
+ - demo/app/models/address.rb
57
+ - demo/app/models/application_record.rb
58
+ - demo/app/models/country.rb
59
+ - demo/app/models/fruit.rb
60
+ - demo/app/models/skill.rb
61
+ - demo/app/models/user.rb
62
+ - demo/app/models/user_skill.rb
63
+ - demo/app/views/layouts/application.html.erb
64
+ - demo/app/views/users/_form.html.erb
65
+ - demo/app/views/users/_form_without_bootstrap_helpers.html.erb
66
+ - demo/app/views/users/_vertical_form.html.erb
67
+ - demo/app/views/users/edit.html.erb
68
+ - demo/app/views/users/index.html.erb
69
+ - demo/app/views/users/new.html.erb
70
+ - demo/bin/bundle
71
+ - demo/bin/rails
72
+ - demo/bin/rake
73
+ - demo/bin/setup
74
+ - demo/config.ru
75
+ - demo/config/application.rb
76
+ - demo/config/boot.rb
77
+ - demo/config/cable.yml
78
+ - demo/config/database.yml
79
+ - demo/config/environment.rb
80
+ - demo/config/environments/development.rb
81
+ - demo/config/environments/production.rb
82
+ - demo/config/environments/test.rb
83
+ - demo/config/initializers/assets.rb
84
+ - demo/config/initializers/content_security_policy.rb
85
+ - demo/config/initializers/filter_parameter_logging.rb
86
+ - demo/config/initializers/inflections.rb
87
+ - demo/config/initializers/permissions_policy.rb
88
+ - demo/config/initializers/rails_bootstrap_form.rb
89
+ - demo/config/locales/en.rb
90
+ - demo/config/puma.rb
91
+ - demo/config/routes.rb
92
+ - demo/config/storage.yml
93
+ - demo/db/migrate/20230514054308_create_countries.rb
94
+ - demo/db/migrate/20230514054851_create_fruits.rb
95
+ - demo/db/migrate/20230514055237_create_users.rb
96
+ - demo/db/migrate/20230514055840_create_addresses.rb
97
+ - demo/db/migrate/20230514060556_create_skills.rb
98
+ - demo/db/migrate/20230514061100_create_user_skills.rb
99
+ - demo/db/schema.rb
100
+ - demo/db/seeds.rb
101
+ - demo/public/favicon.ico
43
102
  - gemfiles/7.0.gemfile
44
103
  - gemfiles/common.gemfile
104
+ - lib/generators/rails_bootstrap_form/install_generator.rb
105
+ - lib/generators/rails_bootstrap_form/templates/install.rb
45
106
  - lib/rails_bootstrap_form.rb
107
+ - lib/rails_bootstrap_form/action_view_extensions/bootstrap_form_helper.rb
108
+ - lib/rails_bootstrap_form/bootstrap_form_builder.rb
109
+ - lib/rails_bootstrap_form/bootstrap_form_options.rb
110
+ - lib/rails_bootstrap_form/configuration.rb
46
111
  - lib/rails_bootstrap_form/engine.rb
47
112
  - lib/rails_bootstrap_form/version.rb
48
113
  - sig/rails_bootstrap_form.rbs