twitter-bootstrap-rails 5.4.0 → 5.5.1

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +68 -0
  3. data/README.md +104 -1
  4. data/app/helpers/badge_label_helper.rb +5 -6
  5. data/app/helpers/bootstrap_flash_helper.rb +10 -5
  6. data/app/helpers/flash_block_helper.rb +6 -4
  7. data/app/helpers/form_errors_helper.rb +3 -2
  8. data/app/helpers/modal_helper.rb +13 -13
  9. data/app/helpers/navbar_helper.rb +54 -47
  10. data/app/helpers/twitter_breadcrumbs_helper.rb +2 -2
  11. data/app/views/twitter-bootstrap/_breadcrumbs.html.erb +11 -11
  12. data/lib/generators/bootstrap/agent/agent_generator.rb +86 -0
  13. data/lib/generators/bootstrap/agent/templates/guide.md.erb +73 -0
  14. data/lib/generators/bootstrap/doctor/doctor_generator.rb +49 -0
  15. data/lib/generators/bootstrap/partial/templates/_login.html.erb +11 -15
  16. data/lib/generators/bootstrap/snippets/snippet_base.rb +23 -4
  17. data/lib/generators/bootstrap/themed/templates/_form.html.haml +7 -9
  18. data/lib/generators/bootstrap/themed/templates/_form.html.slim +8 -10
  19. data/lib/generators/bootstrap/themed/templates/edit.html.erb +1 -1
  20. data/lib/generators/bootstrap/themed/templates/edit.html.haml +1 -1
  21. data/lib/generators/bootstrap/themed/templates/edit.html.slim +1 -1
  22. data/lib/generators/bootstrap/themed/templates/index.html.haml +4 -4
  23. data/lib/generators/bootstrap/themed/templates/index.html.slim +4 -4
  24. data/lib/generators/bootstrap/themed/templates/new.html.erb +1 -1
  25. data/lib/generators/bootstrap/themed/templates/new.html.haml +1 -1
  26. data/lib/generators/bootstrap/themed/templates/new.html.slim +1 -1
  27. data/lib/generators/bootstrap/themed/templates/show.html.erb +1 -1
  28. data/lib/generators/bootstrap/themed/templates/show.html.haml +3 -3
  29. data/lib/generators/bootstrap/themed/templates/show.html.slim +3 -3
  30. data/lib/generators/bootstrap/themed/templates/simple_form/_form.html.erb +1 -1
  31. data/lib/generators/bootstrap/themed/templates/simple_form/_form.html.haml +2 -2
  32. data/lib/generators/bootstrap/themed/templates/simple_form/_form.html.slim +2 -2
  33. data/lib/twitter/bootstrap/rails/doctor.rb +238 -0
  34. data/lib/twitter/bootstrap/rails/legacy_markup.rb +119 -0
  35. data/lib/twitter/bootstrap/rails/v6_migration.rb +156 -0
  36. data/lib/twitter/bootstrap/rails/version.rb +1 -1
  37. data/spec/lib/breadcrumbs_spec.rb +8 -2
  38. data/spec/lib/generators/agent_generator_spec.rb +120 -0
  39. data/spec/lib/generators/doctor_generator_spec.rb +272 -0
  40. data/spec/lib/generators/snippet_generators_spec.rb +23 -0
  41. data/spec/lib/twitter_bootstrap_rails/badge_label_helper_spec.rb +7 -3
  42. data/spec/lib/twitter_bootstrap_rails/bootstrap_flash_helper_spec.rb +23 -45
  43. data/spec/lib/twitter_bootstrap_rails/breadcrumbs_view_spec.rb +35 -0
  44. data/spec/lib/twitter_bootstrap_rails/flash_block_helper_spec.rb +26 -0
  45. data/spec/lib/twitter_bootstrap_rails/form_errors_helper_spec.rb +2 -2
  46. data/spec/lib/twitter_bootstrap_rails/legacy_markup_spec.rb +74 -0
  47. data/spec/lib/twitter_bootstrap_rails/modal_helper_spec.rb +21 -8
  48. data/spec/lib/twitter_bootstrap_rails/navbar_helper_spec.rb +94 -92
  49. data/spec/lib/twitter_bootstrap_rails/v6_migration_spec.rb +98 -0
  50. data/spec/spec_helper.rb +0 -1
  51. data/spec/support/generator_spec_helper.rb +2 -0
  52. metadata +24 -3
@@ -0,0 +1,238 @@
1
+ require 'twitter/bootstrap/rails/version'
2
+ require 'twitter/bootstrap/rails/config'
3
+ require 'twitter/bootstrap/rails/legacy_markup'
4
+ require 'twitter/bootstrap/rails/v6_migration'
5
+
6
+ module Twitter
7
+ module Bootstrap
8
+ module Rails
9
+ # Inspects a host app and reports how Bootstrap is set up. Reads files only.
10
+ # Every check is { :id, :status, :message } plus :fix when there is one, and
11
+ # status is "ok", "warn" or "error".
12
+ class Doctor
13
+ VENDORED_CSS = "app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css".freeze
14
+ VENDORED_JS = "app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js".freeze
15
+ SCANNED_EXTENSIONS = %w[.erb .haml .slim .rb].freeze
16
+ PLACEHOLDER_IMAGES = ["bootstrap-docs.png", "bootstrap-themes.png", "/docs/5.3/assets/", "unsplash-photo-"].freeze
17
+ SEVERITY = %w[ok warn error].freeze
18
+ # What `--v6` scans on top of views and helpers. The gem's own vendored
19
+ # Bootstrap files are skipped: replacing them is the first migration step.
20
+ V6_EXTRA_FILES = "app/{javascript,assets/javascripts,assets/stylesheets}/**/*".freeze
21
+ V6_EXTENSIONS = %w[.js .mjs .ts .css .scss .sass].freeze
22
+ VENDORED_DIR = %r{/twitter/bootstrap/}
23
+
24
+ def initialize(root)
25
+ @root = root
26
+ end
27
+
28
+ # v6: true adds a v6_migration check and list for apps moving to Bootstrap 6.
29
+ def report(v6: false)
30
+ checks = [css_check, js_check, version_check, sprite_check, placeholder_check, legacy_check]
31
+ checks << v6_check if v6
32
+
33
+ result = facts.merge(
34
+ :status => checks.map { |c| c[:status] }.max_by { |s| SEVERITY.index(s) },
35
+ :checks => checks,
36
+ :legacy_markup => legacy_markup
37
+ )
38
+ v6 ? result.merge(:v6_migration => v6_migration) : result
39
+ end
40
+
41
+ def facts
42
+ {
43
+ :bootstrap_version => BOOTSTRAP_VERSION,
44
+ :gem_version => VERSION,
45
+ :rails_version => ::Rails.version,
46
+ :asset_pipeline => pipeline.to_s,
47
+ :asset_mode => asset_mode,
48
+ :template_engine => template_engine
49
+ }
50
+ end
51
+
52
+ private
53
+
54
+ def pipeline
55
+ Twitter::Bootstrap::Rails.asset_pipeline
56
+ end
57
+
58
+ def asset_mode
59
+ initializer = read("config/initializers/bootstrap.rb")
60
+ cdn = initializer.match?(/asset_mode\s*=\s*:cdn/) || layouts.match?(%r{cdn\.jsdelivr\.net/npm/bootstrap@})
61
+ cdn ? "cdn" : "static"
62
+ end
63
+
64
+ def template_engine
65
+ app = ::Rails.application
66
+ (app && app.config.generators.options.dig(:rails, :template_engine) || :erb).to_s
67
+ end
68
+
69
+ def css_check
70
+ asset_check("bootstrap_css", "stylesheet",
71
+ :cdn => %r{bootstrap@[\d.]+/dist/css/bootstrap},
72
+ :vendored_name => "twitter/bootstrap/bootstrap.min",
73
+ :vendored_file => VENDORED_CSS,
74
+ :manifest => "app/assets/stylesheets/application.css",
75
+ :manifest_entry => %r{bootstrap_and_overrides|twitter/bootstrap|require_tree},
76
+ :layout_tag => /stylesheet_link_tag[^\n]*application/)
77
+ end
78
+
79
+ def js_check
80
+ asset_check("bootstrap_js", "JavaScript",
81
+ :cdn => %r{bootstrap@[\d.]+/dist/js/bootstrap},
82
+ :vendored_name => "twitter/bootstrap/bootstrap.bundle.min",
83
+ :vendored_file => VENDORED_JS,
84
+ :manifest => "app/assets/javascripts/application.js",
85
+ :manifest_entry => %r{twitter/bootstrap|require_tree},
86
+ :layout_tag => /javascript_include_tag[^\n]*application/)
87
+ end
88
+
89
+ def asset_check(id, label, source)
90
+ return check(id, "ok", "Bootstrap #{label} is linked from the CDN.") if layouts.match?(source[:cdn])
91
+
92
+ if linked_through_pipeline?(source)
93
+ return check(id, "ok", "Bootstrap #{label} is linked from the asset pipeline.") if exist?(source[:vendored_file])
94
+
95
+ return check(id, "error", "A layout links the Bootstrap #{label}, but #{source[:vendored_file]} does not exist.",
96
+ "Run `rails g bootstrap:install static`.")
97
+ end
98
+
99
+ if bundled_elsewhere?
100
+ return check(id, "warn", "Bootstrap appears to come from package.json or the importmap, so the #{label} can't be verified.")
101
+ end
102
+
103
+ check(id, "error", "No layout loads the Bootstrap #{label}, so the page will render unstyled or without behaviour.",
104
+ "Run `rails g bootstrap:install static` (or `cdn`), then `rails g bootstrap:layout`, " \
105
+ "or link it from your own layout (`rails g bootstrap:cdn` prints the tags).")
106
+ end
107
+
108
+ def linked_through_pipeline?(source)
109
+ propshaft_link = layouts.include?(source[:vendored_name])
110
+ sprockets_link = layouts.match?(source[:layout_tag]) && read(source[:manifest]).match?(source[:manifest_entry])
111
+
112
+ case pipeline
113
+ when :propshaft then propshaft_link
114
+ when :sprockets then sprockets_link
115
+ else propshaft_link || sprockets_link
116
+ end
117
+ end
118
+
119
+ def bundled_elsewhere?
120
+ read("package.json").match?(/"bootstrap"\s*:/) || read("config/importmap.rb").match?(/pin\s+["']bootstrap["']/)
121
+ end
122
+
123
+ def version_check
124
+ found = layouts.scan(/bootstrap@(\d+\.\d+\.\d+)/).flatten
125
+ [VENDORED_CSS, VENDORED_JS].each do |file|
126
+ found.concat(read(file)[0, 400].to_s.scan(/Bootstrap\s+v(\d+\.\d+\.\d+)/).flatten)
127
+ end
128
+ stale = found.uniq - [BOOTSTRAP_VERSION]
129
+
130
+ return check("bootstrap_version", "ok", "No Bootstrap version other than #{BOOTSTRAP_VERSION} found.") if stale.empty?
131
+
132
+ check("bootstrap_version", "warn",
133
+ "Found Bootstrap #{stale.join(', ')}, but this gem ships #{BOOTSTRAP_VERSION}.",
134
+ "Run `rails g bootstrap:install static --force` or update the CDN tags with `rails g bootstrap:cdn`.")
135
+ end
136
+
137
+ def sprite_check
138
+ sprite = view_files.find { |file| File.basename(file) == "_bootstrap_icons.html.erb" }
139
+ return check("icon_sprite", "ok", "The icon sprite is not installed.") unless sprite
140
+
141
+ renders = view_files.sum { |file| read(file).scan(/render\b[^\n]*bootstrap_icons/).size }
142
+ case renders
143
+ when 0
144
+ render_path = sprite.sub(%r{\Aapp/views/}, "").sub("_bootstrap_icons.html.erb", "bootstrap_icons")
145
+ check("icon_sprite", "error", "The icon sprite is installed but never rendered, so snippet icons come out empty.",
146
+ %{Add <%= render "#{render_path}" %> once, near the top of <body> in your layout.})
147
+ when 1
148
+ check("icon_sprite", "ok", "The icon sprite is rendered once.")
149
+ else
150
+ check("icon_sprite", "warn", "The icon sprite is rendered #{renders} times, which repeats its symbol ids.",
151
+ "Render it once, in your layout.")
152
+ end
153
+ end
154
+
155
+ def placeholder_check
156
+ files = view_files.flat_map do |file|
157
+ read(file).each_line.with_index(1).filter_map do |line, number|
158
+ "#{file}:#{number}" if PLACEHOLDER_IMAGES.any? { |image| line.include?(image) }
159
+ end
160
+ end
161
+ return check("placeholder_images", "ok", "No docs-site placeholder images found.") if files.empty?
162
+
163
+ check("placeholder_images", "warn", "#{files.size} reference(s) to placeholder images that only exist on the Bootstrap docs site.",
164
+ "Point them at your own images.").merge(:files => files)
165
+ end
166
+
167
+ def legacy_check
168
+ count = legacy_markup.size
169
+ return check("legacy_markup", "ok", "No older Bootstrap markup found.") if count.zero?
170
+
171
+ check("legacy_markup", "warn", "#{count} use(s) of markup from older Bootstrap versions, listed in legacy_markup.",
172
+ "Apply each replacement. Plain names like `close` or `well` may be your own CSS classes, so check before changing those.")
173
+ end
174
+
175
+ def legacy_markup
176
+ @legacy_markup ||= scanned_files.flat_map do |file|
177
+ LegacyMarkup.scan(read(file), File.extname(file)).map { |finding| { :file => file }.merge(finding) }
178
+ end
179
+ end
180
+
181
+ def v6_check
182
+ count = v6_migration.size
183
+ return check("v6_migration", "ok", "Nothing found that changes in Bootstrap 6.") if count.zero?
184
+
185
+ check("v6_migration", "warn",
186
+ "#{count} use(s) of Bootstrap 5 markup, Sass or JavaScript that change in Bootstrap 6, listed in v6_migration. " \
187
+ "This gem still ships Bootstrap #{BOOTSTRAP_VERSION}, so only change them as part of moving the app to Bootstrap 6.",
188
+ "Install the upstream migration skill (`#{V6Migration::SKILL_COMMAND}`) and follow it, " \
189
+ "or read #{V6Migration::GUIDE_URL}. Switch the Bootstrap assets first.")
190
+ end
191
+
192
+ def v6_migration
193
+ @v6_migration ||= v6_files.flat_map do |file|
194
+ V6Migration.scan(read(file), File.extname(file)).map { |finding| { :file => file }.merge(finding) }
195
+ end
196
+ end
197
+
198
+ def v6_files
199
+ extra = relative_files(V6_EXTRA_FILES, V6_EXTENSIONS).reject { |file| file.match?(VENDORED_DIR) }
200
+ (scanned_files + extra).uniq.sort
201
+ end
202
+
203
+ def check(id, status, message, fix = nil)
204
+ result = { :id => id, :status => status, :message => message }
205
+ result[:fix] = fix if fix
206
+ result
207
+ end
208
+
209
+ def layouts
210
+ @layouts ||= view_files.select { |file| file.start_with?("app/views/layouts/") }.map { |file| read(file) }.join("\n")
211
+ end
212
+
213
+ def view_files
214
+ @view_files ||= relative_files("app/views/**/*").reject { |file| file.end_with?(".rb") }
215
+ end
216
+
217
+ def scanned_files
218
+ relative_files("app/{views,helpers,components}/**/*")
219
+ end
220
+
221
+ def relative_files(pattern, extensions = SCANNED_EXTENSIONS)
222
+ Dir[File.join(@root, pattern)]
223
+ .select { |file| File.file?(file) && extensions.include?(File.extname(file)) }
224
+ .map { |file| file.delete_prefix("#{@root}/") }
225
+ .sort
226
+ end
227
+
228
+ def exist?(relative)
229
+ File.file?(File.join(@root, relative))
230
+ end
231
+
232
+ def read(relative)
233
+ exist?(relative) ? File.read(File.join(@root, relative)).scrub : ""
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,119 @@
1
+ module Twitter
2
+ module Bootstrap
3
+ module Rails
4
+ # Finds Bootstrap 2-4 markup that does nothing on the Bootstrap 5 this gem
5
+ # ships. bootstrap:doctor reports what it finds, bootstrap:agent turns the
6
+ # same tables into guidance, and a spec holds the gem's own templates to it.
7
+ module LegacyMarkup
8
+ DATA_ATTRIBUTES = %w[toggle target dismiss ride slide slide-to parent spy].freeze
9
+
10
+ CLASSES = {
11
+ "btn-default" => "btn-secondary",
12
+ "btn-xs" => "btn-sm",
13
+ "btn-block" => "w-100",
14
+ "navbar-default" => "bg-body-tertiary",
15
+ "navbar-inverse" => "bg-dark with data-bs-theme=\"dark\"",
16
+ "navbar-toggle" => "navbar-toggler",
17
+ "navbar-header" => "nothing (remove the wrapper)",
18
+ "navbar-left" => "me-auto",
19
+ "navbar-right" => "ms-auto",
20
+ "icon-bar" => "navbar-toggler-icon",
21
+ "caret" => "nothing (dropdown-toggle draws it)",
22
+ "divider" => "dropdown-divider on an hr",
23
+ "dropdown-submenu" => "dropdown-header (Bootstrap 5 has no nested dropdowns)",
24
+ "panel" => "card",
25
+ "panel-default" => "card",
26
+ "panel-heading" => "card-header",
27
+ "panel-body" => "card-body",
28
+ "panel-footer" => "card-footer",
29
+ "well" => "card",
30
+ "jumbotron" => "p-5 bg-body-tertiary rounded-3",
31
+ "page-header" => "border-bottom pb-2 mb-4",
32
+ "label" => "badge",
33
+ "badge-pill" => "rounded-pill",
34
+ "form-group" => "mb-3",
35
+ "form-inline" => "row with col-auto columns",
36
+ "form-horizontal" => "row with col-* columns",
37
+ "control-group" => "mb-3",
38
+ "control-label" => "form-label",
39
+ "controls" => "nothing (remove the wrapper)",
40
+ "has-error" => "is-invalid on the input",
41
+ "help-block" => "form-text",
42
+ "input-group-addon" => "input-group-text",
43
+ "custom-select" => "form-select",
44
+ "sr-only" => "visually-hidden",
45
+ "close" => "btn-close",
46
+ "in" => "show",
47
+ "hidden" => "d-none",
48
+ "pull-left" => "float-start",
49
+ "pull-right" => "float-end",
50
+ "float-left" => "float-start",
51
+ "float-right" => "float-end",
52
+ "text-left" => "text-start",
53
+ "text-right" => "text-end",
54
+ "font-weight-bold" => "fw-bold"
55
+ }.freeze
56
+
57
+ # [pattern, block turning the match into its replacement, example for docs]
58
+ CLASS_PATTERNS = [
59
+ [/\Acol-xs-(\d+)\z/, ->(m) { "col-#{m[1]}" }, "col-xs-6 becomes col-6"],
60
+ [/\Aglyphicon(-[\w-]+)?\z/, ->(_) { "Bootstrap Icons" }, "glyphicon-* becomes Bootstrap Icons"],
61
+ [/\A(?:label|badge)-(default|primary|secondary|success|info|warning|danger)\z/,
62
+ ->(m) { "text-bg-#{m[1] == 'default' ? 'secondary' : m[1]}" }, "label-warning and badge-warning become text-bg-warning"],
63
+ [/\A([mp])([lr])(-(?:sm|md|lg|xl|xxl))?-(\d|auto)\z/,
64
+ ->(m) { "#{m[1]}#{m[2] == 'l' ? 's' : 'e'}#{m[3]}-#{m[4]}" }, "ml-3 and pr-md-2 become ms-3 and pe-md-2"]
65
+ ].freeze
66
+
67
+ DATA_ATTRIBUTE = /(?<![\w-])data-(#{DATA_ATTRIBUTES.join('|')})(?![\w-])/
68
+ RUBY_DATA_HASH = /(?:\bdata:\s*|:data\s*=>\s*)\{([^}]*)\}/
69
+ RUBY_DATA_KEY = /(?<![\w-]):?(#{DATA_ATTRIBUTES.join('|')})(?:\s*:|\s*=>)/
70
+ CLASS_VALUE = /(?:\bclass\s*=\s*|\bclass:\s*|:class\s*=>\s*|["']class["']\s*=>\s*)(["'])(.*?)\1/
71
+ EMBEDDED_CODE = /<%.*?%>|#\{.*?\}/
72
+ SHORTHAND = {
73
+ ".haml" => /\A\s*(?:%[\w:-]+)?(?:#[\w-]+)?((?:\.[\w-]+)+)/,
74
+ ".slim" => /\A\s*(?:[a-z][\w:-]*)?(?:#[\w-]+)?((?:\.[\w-]+)+)/
75
+ }.freeze
76
+
77
+ module_function
78
+
79
+ # Returns [{ :line, :found, :replacement }, ...] for a file's contents.
80
+ # extension (".erb", ".haml", ...) decides whether to read class shorthand.
81
+ def scan(text, extension)
82
+ text.each_line.with_index(1).flat_map do |line, number|
83
+ scan_line(line, extension).map { |found, replacement| { :line => number, :found => found, :replacement => replacement } }
84
+ end
85
+ end
86
+
87
+ def scan_line(line, extension)
88
+ data_attributes(line) + class_names(line, extension).filter_map do |name|
89
+ replacement = replacement_for(name)
90
+ [name, replacement] if replacement
91
+ end
92
+ end
93
+
94
+ def data_attributes(line)
95
+ names = line.scan(DATA_ATTRIBUTE).flatten
96
+ line.scan(RUBY_DATA_HASH).flatten.each { |pairs| names.concat(pairs.scan(RUBY_DATA_KEY).flatten) }
97
+ names.map { |name| ["data-#{name}", "data-bs-#{name}"] }
98
+ end
99
+
100
+ def class_names(line, extension)
101
+ names = line.scan(CLASS_VALUE).flat_map { |_quote, value| value.gsub(EMBEDDED_CODE, " ").split }
102
+ shorthand = SHORTHAND[extension] && line[SHORTHAND[extension], 1]
103
+ names.concat(shorthand.split(".").reject(&:empty?)) if shorthand
104
+ names
105
+ end
106
+
107
+ def replacement_for(name)
108
+ return CLASSES[name] if CLASSES.key?(name)
109
+
110
+ CLASS_PATTERNS.each do |pattern, replacement, _example|
111
+ match = pattern.match(name)
112
+ return replacement.call(match) if match
113
+ end
114
+ nil
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,156 @@
1
+ require 'twitter/bootstrap/rails/legacy_markup'
2
+
3
+ module Twitter
4
+ module Bootstrap
5
+ module Rails
6
+ # Finds Bootstrap 5 markup, Sass and JavaScript that changes in Bootstrap 6,
7
+ # following the upstream migration skill
8
+ # (npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration).
9
+ # The gem still ships Bootstrap 5, so this is a worklist for an app that is
10
+ # moving to 6, not markup to avoid today. `bootstrap:doctor --v6` reports it.
11
+ module V6Migration
12
+ SKILL_COMMAND = "npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration".freeze
13
+ GUIDE_URL = "https://getbootstrap.com/docs/6.0/guides/migration/".freeze
14
+
15
+ BREAKPOINTS = { "sm" => "sm", "md" => "md", "lg" => "lg", "xl" => "xl", "xxl" => "2xl" }.freeze
16
+ THEMES = %w[primary secondary success danger warning info].freeze
17
+ FONT_SIZES = { "1" => "4xl", "2" => "3xl", "3" => "2xl", "4" => "xl", "5" => "lg", "6" => "md" }.freeze
18
+ RENAMED_COMPONENTS = { "modal" => "dialog", "offcanvas" => "drawer", "dropdown" => "menu" }.freeze
19
+ # Classes that take a breakpoint as a suffix in v5 (navbar-expand-lg).
20
+ SUFFIXED = %w[container navbar-expand offcanvas table-responsive list-group-horizontal vstack hstack col].freeze
21
+
22
+ CLASSES = {
23
+ "modal" => "dialog on a <dialog> element",
24
+ "modal-dialog" => "nothing (remove the wrapper; use one <dialog class=\"dialog\">)",
25
+ "modal-content" => "nothing (remove the wrapper; use one <dialog class=\"dialog\">)",
26
+ "offcanvas" => "drawer on a <dialog> element",
27
+ "dropdown" => "nothing (remove the wrapper; the toggle and .menu are siblings)",
28
+ "dropdown-menu" => "menu",
29
+ "dropdown-item" => "menu-item",
30
+ "dropdown-divider" => "menu-divider",
31
+ "dropdown-header" => "menu-header",
32
+ "dropdown-toggle" => "nothing (the menu toggle needs no class)",
33
+ "dropdown-toggle-split" => "nothing (remove it)",
34
+ "text-muted" => "fg-secondary",
35
+ "form-select" => "form-control (on the select)",
36
+ "form-check" => "form-field",
37
+ "form-check-input" => "check or radio (on the input)",
38
+ "form-check-label" => "nothing (a plain label)",
39
+ "form-switch" => "a switch wrapper around the input",
40
+ "needs-validation" => "data-bs-validate on the form",
41
+ "was-validated" => "nothing (:user-invalid handles it)",
42
+ "has-validation" => "nothing (remove it)",
43
+ "clearfix" => "d-flow-root",
44
+ "lh-base" => "lh-md",
45
+ "lead" => "fs-* and fw-* utilities",
46
+ "placeholder-glow" => "placeholder-pulse (or placeholder-wave)",
47
+ "btn-close-white" => "nothing (set the text colour instead)",
48
+ "card-link" => "a plain link",
49
+ "accordion-button" => "nothing (put the title in summary.accordion-header)",
50
+ "accordion-collapse" => "nothing (use details.accordion-item)",
51
+ "navbar-light" => "nothing (remove it)",
52
+ "navbar-dark" => "nothing (use data-bs-theme=\"dark\")",
53
+ "carousel-control-prev" => "btn-icon with data-bs-slide=\"prev\"",
54
+ "carousel-control-next" => "btn-icon with data-bs-slide=\"next\"",
55
+ "carousel-control-prev-icon" => "carousel-icon-prev",
56
+ "carousel-control-next-icon" => "carousel-icon-next",
57
+ "carousel-caption" => "your own markup",
58
+ "carousel-dark" => "data-bs-theme=\"dark\""
59
+ }.freeze
60
+
61
+ # [pattern, block turning the match into its replacement, example for docs]
62
+ CLASS_PATTERNS = [
63
+ [/\Amodal-(header|body|footer|title|sm|lg|xl|fullscreen)\z/, ->(m) { "dialog-#{m[1]}" }, "modal-body becomes dialog-body"],
64
+ [/\Aoffcanvas-(header|body|title|start|end|top|bottom)\z/, ->(m) { "drawer-#{m[1]}" }, "offcanvas-start becomes drawer-start"],
65
+ [/\Adropdown-menu-(start|end)\z/, ->(m) { "menu-#{m[1]}" }, "dropdown-menu-end becomes menu-end"],
66
+ [/\Amodal-fullscreen-(sm|md|lg|xl|xxl)-down\z/,
67
+ ->(m) { "#{BREAKPOINTS[m[1]]}-down:dialog-fullscreen" }, "modal-fullscreen-sm-down becomes sm-down:dialog-fullscreen"],
68
+ [/\A([a-z][\w-]*)-hover\z/,
69
+ ->(m) { "hover:#{V6Migration.class_replacement(m[1])}" }, "opacity-50-hover becomes hover:opacity-50"],
70
+ [/\Abtn-(outline-)?(#{THEMES.join('|')})\z/,
71
+ ->(m) { "btn-#{m[1] ? 'outline' : 'solid'} theme-#{m[2]}" }, "btn-primary and btn-outline-primary become btn-solid theme-primary and btn-outline theme-primary"],
72
+ [/\Aalert-(#{THEMES.join('|')})\z/, ->(m) { "alert theme-#{m[1]}" }, "alert-info becomes alert theme-info"],
73
+ [/\Atext-bg-(#{THEMES.join('|')})\z/, ->(m) { "bg-#{m[1]} fg-contrast-#{m[1]}" }, "text-bg-warning becomes bg-warning fg-contrast-warning"],
74
+ [/\Atext-(#{THEMES.join('|')})\z/, ->(m) { "fg-#{m[1]}" }, "text-danger becomes fg-danger"],
75
+ [/\A(?:btn|btn-outline|alert|text|text-bg)-(light|dark)\z/,
76
+ ->(_) { "a neutral surface, a colour mode or theme-inverse (v6 has no theme-light or theme-dark)" },
77
+ "btn-light and text-dark become a neutral surface, a colour mode or theme-inverse"],
78
+ [/\Afs-([1-6])\z/, ->(m) { "fs-#{FONT_SIZES[m[1]]}" }, "fs-1 and fs-6 become fs-4xl and fs-md"],
79
+ [/\Adisplay-[1-6]\z/, ->(_) { "fs-* and fw-* utilities" }, "display-1 becomes fs-* and fw-* utilities"],
80
+ [/\Am(h|w)-(.+)\z/, ->(m) { "max-#{m[1]}-#{m[2]}" }, "mw-100 becomes max-w-100"],
81
+ [/\Alink-(offset|underline)(-.+)?\z/,
82
+ ->(m) { "underline#{m[1] == 'offset' ? '-offset' : ''}#{m[2]}" }, "link-offset-2 becomes underline-offset-2"],
83
+ [/\Am[tbsexy]?-n\d\z/, ->(_) { "ms--1, ms--2, me--1, me--2 or your own style" }, "mt-n1 becomes ms--1 / me--1 or your own style"],
84
+ [/\A(#{SUFFIXED.join('|')})-(sm|md|lg|xl|xxl)\z/,
85
+ ->(m) { "#{BREAKPOINTS[m[2]]}:#{V6Migration.class_replacement(m[1])}" }, "navbar-expand-lg becomes lg:navbar-expand"],
86
+ [/\A([a-z][\w-]*?)-print-([\w-]+)\z/,
87
+ ->(m) { "print:#{V6Migration.class_replacement("#{m[1]}-#{m[2]}")}" }, "d-print-none becomes print:d-none"],
88
+ [/\A([a-z][\w-]*?)-(sm|md|lg|xl|xxl)-([\w-]+)\z/,
89
+ ->(m) { "#{BREAKPOINTS[m[2]]}:#{V6Migration.class_replacement("#{m[1]}-#{m[3]}")}" },
90
+ "col-md-6 and d-xxl-none become md:col-6 and 2xl:d-none"]
91
+ ].freeze
92
+
93
+ TOGGLE = /(?:data-bs-|\bbs[_-])(toggle|dismiss)["']?\s*(?:=>|=|:)\s*(["'])(#{RENAMED_COMPONENTS.keys.join('|')})\2/
94
+
95
+ # [pattern, replacement, example for docs]. Applied to every scanned file.
96
+ LINE_PATTERNS = [
97
+ [/(?:data-bs-|\bbs[_-])ride(?![\w-])/, %{data-bs-autoplay="true" (carousels no longer start on their own)}],
98
+ [/@import\s+(["'])bootstrap/, %{@use "bootstrap/scss/bootstrap" (add `as *` to keep v5 names)}],
99
+ [/\bbootstrap\.(?:Modal|Offcanvas|Dropdown)\b/, "import { Dialog, Drawer, Menu } from Bootstrap's module build"],
100
+ [/\bpopperConfig\b/, "floatingConfig"],
101
+ [/@popperjs\/core/, "@floating-ui/dom"],
102
+ [/--bs-[\w-]+-rgb\b/, "color-mix(in oklab, var(--bs-…), transparent N%)"]
103
+ ].freeze
104
+
105
+ # Files whose class attributes and shorthand are read for class names.
106
+ MARKUP_EXTENSIONS = %w[.erb .haml .slim .rb .html].freeze
107
+
108
+ module_function
109
+
110
+ # Returns [{ :line, :found, :replacement }, ...] for a file's contents.
111
+ def scan(text, extension)
112
+ text.each_line.with_index(1).flat_map do |line, number|
113
+ scan_line(line, extension).map { |found, replacement| { :line => number, :found => found, :replacement => replacement } }
114
+ end
115
+ end
116
+
117
+ def scan_line(line, extension)
118
+ classes = MARKUP_EXTENSIONS.include?(extension) ? LegacyMarkup.class_names(line, extension) : []
119
+ renamed = classes.filter_map do |name|
120
+ replacement = replacement_for(name)
121
+ [name, replacement] if replacement
122
+ end
123
+
124
+ toggles = line.scan(TOGGLE).map do |attribute, _quote, value|
125
+ [%{data-bs-#{attribute}="#{value}"}, %{data-bs-#{attribute}="#{RENAMED_COMPONENTS[value]}"}]
126
+ end
127
+
128
+ other = LINE_PATTERNS.flat_map do |pattern, replacement|
129
+ matches = []
130
+ line.scan(pattern) { matches << [Regexp.last_match(0).sub(/\Abs[_-]/, "data-bs-"), replacement] }
131
+ matches
132
+ end
133
+
134
+ renamed + toggles + other
135
+ end
136
+
137
+ def replacement_for(name)
138
+ return CLASSES[name] if CLASSES.key?(name)
139
+
140
+ CLASS_PATTERNS.each do |pattern, replacement, _example|
141
+ match = pattern.match(name)
142
+ return replacement.call(match) if match
143
+ end
144
+ nil
145
+ end
146
+
147
+ # The v6 name for a class when it is a plain rename, otherwise the class itself.
148
+ # Used under a breakpoint prefix, where a sentence would make no sense.
149
+ def class_replacement(name)
150
+ replacement = RENAMED_COMPONENTS[name] || replacement_for(name)
151
+ replacement && replacement.match?(/\A[\w:-]+\z/) ? replacement : name
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -1,7 +1,7 @@
1
1
  module Twitter
2
2
  module Bootstrap
3
3
  module Rails
4
- VERSION = "5.4.0"
4
+ VERSION = "5.5.1"
5
5
 
6
6
  # The Bootstrap release this gem vendors and links to. The gem version no
7
7
  # longer tracks it; this constant is the authoritative statement.
@@ -44,6 +44,12 @@ describe Twitter::Bootstrap::Breadcrumbs do
44
44
  def test_models_path
45
45
  '/test_model'
46
46
  end
47
+
48
+ # polymorphic_path looks up custom mappings here before calling the named
49
+ # route helper above. A bare AbstractController has no route set of its own.
50
+ def _routes
51
+ @_routes ||= ActionDispatch::Routing::RouteSet.new
52
+ end
47
53
  end
48
54
 
49
55
  describe 'Breadcrumbs generation' do
@@ -51,12 +57,12 @@ describe Twitter::Bootstrap::Breadcrumbs do
51
57
  before do
52
58
  options = { :scope => [:breadcrumbs, 'test'] }
53
59
  [:class_i18n, :instance_i18n, :show, :symbolized].each do |name|
54
- I18n.expects(:t).with(name, options).returns(name.to_s)
60
+ expect(I18n).to receive(:t).with(name, **options).and_return(name.to_s)
55
61
  end
56
62
 
57
63
  name = :base_i18n
58
64
  options = { :scope => [:breadcrumbs, 'base_test'] }
59
- I18n.expects(:t).with(name, options).returns(name)
65
+ expect(I18n).to receive(:t).with(name, **options).and_return(name)
60
66
 
61
67
  @controller = TestController.new
62
68
  @controller.process(:show)
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bootstrap::Generators::AgentGenerator do
4
+ let(:rails_mod) { Twitter::Bootstrap::Rails }
5
+ let(:skill_path) { ".claude/skills/bootstrap-rails/SKILL.md" }
6
+
7
+ before { stub_rails_application }
8
+
9
+ def skill
10
+ read_destination(skill_path)
11
+ end
12
+
13
+ def agents
14
+ read_destination("AGENTS.md")
15
+ end
16
+
17
+ it "writes a Claude skill with the frontmatter Claude needs" do
18
+ run_generator(described_class)
19
+
20
+ expect(skill).to start_with("---\nname: bootstrap-rails\ndescription: ")
21
+ end
22
+
23
+ it "writes the guide to AGENTS.md" do
24
+ run_generator(described_class)
25
+
26
+ expect(agents).to include("Bootstrap #{rails_mod::BOOTSTRAP_VERSION}")
27
+ end
28
+
29
+ it "keeps what is already in AGENTS.md" do
30
+ write_destination("AGENTS.md", "# My rules\n\nUse tabs.\n")
31
+
32
+ run_generator(described_class)
33
+
34
+ expect(agents).to start_with("# My rules\n\nUse tabs.\n")
35
+ expect(agents).to include("Bootstrap #{rails_mod::BOOTSTRAP_VERSION}")
36
+ end
37
+
38
+ it "replaces its own section when run again instead of adding another" do
39
+ write_destination("AGENTS.md", "# My rules\n")
40
+ rails_mod.asset_pipeline = :sprockets
41
+ run_generator(described_class)
42
+ rails_mod.asset_pipeline = :propshaft
43
+ run_generator(described_class)
44
+
45
+ expect(agents.scan("bootstrap-rails:start").size).to eq(1)
46
+ expect(agents).to include("Propshaft")
47
+ expect(agents).not_to include("Sprockets directives")
48
+ expect(agents).to start_with("# My rules\n")
49
+ end
50
+
51
+ it "does not stop to ask when the skill already exists" do
52
+ run_generator(described_class)
53
+ rails_mod.asset_pipeline = :propshaft
54
+ allow($stdin).to receive(:gets).and_raise("prompted for input")
55
+
56
+ expect { run_generator(described_class) }.not_to raise_error
57
+ expect(skill).to include("Propshaft")
58
+ end
59
+
60
+ it "skips each file on request" do
61
+ run_generator(described_class, %w[--no-claude])
62
+ expect(destination_exist?(skill_path)).to be false
63
+ expect(destination_exist?("AGENTS.md")).to be true
64
+
65
+ FileUtils.rm(destination_path("AGENTS.md"))
66
+ run_generator(described_class, %w[--no-agents])
67
+ expect(destination_exist?(skill_path)).to be true
68
+ expect(destination_exist?("AGENTS.md")).to be false
69
+ end
70
+
71
+ it "writes nothing with --pretend" do
72
+ write_destination("AGENTS.md", "# Mine\n")
73
+
74
+ run_generator(described_class, %w[--pretend])
75
+
76
+ expect(agents).to eq("# Mine\n")
77
+ expect(destination_exist?(skill_path)).to be false
78
+ end
79
+
80
+ describe "the guide" do
81
+ it "describes this app's setup" do
82
+ rails_mod.asset_pipeline = :propshaft
83
+ stub_rails_application(:haml)
84
+ run_generator(described_class)
85
+
86
+ expect(skill).to include("Propshaft").and include("Template engine: haml")
87
+ end
88
+
89
+ it "lists every snippet generator and variant from the catalog" do
90
+ run_generator(described_class)
91
+ catalog = Bootstrap::Generators::SnippetCatalog
92
+
93
+ catalog::GENERATORS.each do |generator, category|
94
+ expect(skill).to include("bootstrap:#{generator}")
95
+ catalog.variant_names(category).each { |variant| expect(skill).to include(variant) }
96
+ end
97
+ end
98
+
99
+ it "names the old markup to avoid, from the same table the doctor uses" do
100
+ run_generator(described_class)
101
+
102
+ expect(skill).to include("data-toggle").and include("btn-default").and include("btn-secondary")
103
+ end
104
+
105
+ it "tells agents how to check their work" do
106
+ run_generator(described_class)
107
+
108
+ expect(skill).to include("rails g bootstrap:doctor --json")
109
+ end
110
+
111
+ it "keeps agents on the shipped Bootstrap and points them at the upstream skill for Bootstrap 6" do
112
+ run_generator(described_class)
113
+ section = skill[/^## Bootstrap 6\n.*?(?=^## |\z)/m]
114
+
115
+ expect(section).to include("Don't write Bootstrap 6 markup")
116
+ expect(section).to include(Twitter::Bootstrap::Rails::V6Migration::SKILL_COMMAND)
117
+ expect(section).to include("rails g bootstrap:doctor --v6 --json")
118
+ end
119
+ end
120
+ end