Class: Boring::Bootstrap::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/boring/bootstrap/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_bootstrap_packageObject



9
10
11
12
# File 'lib/generators/boring/bootstrap/install/install_generator.rb', line 9

def add_bootstrap_package
  say "Adding bootstrap packages", :green
  run "yarn add bootstrap jquery @popperjs/core"
end

#add_jquery_plugin_provider_to_webpack_environmentObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/boring/bootstrap/install/install_generator.rb', line 14

def add_jquery_plugin_provider_to_webpack_environment
  say "Adding jQuery and popper JS plugin in the webpack", :green
  if File.exist?("config/webpack/environment.js")
    insert_into_file "config/webpack/environment.js", "      const webpack = require(\"webpack\")\n\n      environment.plugins.append(\"Provide\", new webpack.ProvidePlugin({\n        $: 'jquery',\n        jQuery: 'jquery',\n        Popper: ['popper.js', 'default']\n      }))\n    RUBY\n  else\n    say <<~WARNING, :red\n      ERROR: Looks like the webpacker installation is incomplete. Could not find environment.js in config/webpack.\n    WARNING\n  end\nend\n", after: /@rails\/webpacker.*\n/

#add_or_import_stylesheet_for_bootstrapObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/boring/bootstrap/install/install_generator.rb', line 33

def add_or_import_stylesheet_for_bootstrap
  if File.exist?("app/javascript/stylesheets/application.scss")
    say "Add bootstrap imports to the application.scss", :green
    append_to_file "app/javascript/stylesheets/application.scss" do
      '@import "~bootstrap/scss/bootstrap";'
    end
  else
    say "Copying application.scss with bootstrap imports", :green
    template("application.scss", "app/javascript/stylesheets/application.scss")
  end
end

#insert_stylesheet_in_the_applicationObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/boring/bootstrap/install/install_generator.rb', line 45

def insert_stylesheet_in_the_application
  if File.exist?("app/javascript/packs/application.js")
    application_js_content = "      \\n\n      import \"bootstrap\"\n      import \"stylesheets/application\"\n      import { Tooltip, Popover } from \"bootstrap\"\n\n      document.addEventListener(\"turbolinks:load\", () => {\n        // Both of these are from the Bootstrap 5 docs\n        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle=\"tooltip\"]'))\n        var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {\n            return new Tooltip(tooltipTriggerEl)\n        })\n\n        var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle=\"popover\"]'))\n        var popoverList = popoverTriggerList.map(function(popoverTriggerEl) {\n            return new Popover(popoverTriggerEl)\n        })\n      })\n    RUBY\n    append_to_file \"app/javascript/packs/application.js\", application_js_content\n  else\n    say <<~WARNING, :red\n      ERROR: Looks like the webpacker installation is incomplete. Could not find application.js in app/javascript/packs.\n    WARNING\n  end\nend\n"

#insert_stylesheet_packs_tagObject



74
75
76
77
78
# File 'lib/generators/boring/bootstrap/install/install_generator.rb', line 74

def insert_stylesheet_packs_tag
  insert_into_file "app/views/layouts/application.html.erb", "    \\t\\t<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>\n  RUBY\nend\n", after: /stylesheet_link_tag.*\n/