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", <<~RUBY, after: /@rails\/webpacker.*\n/
      const webpack = require("webpack")

      environment.plugins.append("Provide", new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        Popper: ['popper.js', 'default']
      }))
    RUBY
  else
    say <<~WARNING, :red
      ERROR: Looks like the webpacker installation is incomplete. Could not find environment.js in config/webpack.
    WARNING
  end
end

#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 = <<~RUBY
      \n
      import "bootstrap"
      import "stylesheets/application"
      import { Tooltip, Popover } from "bootstrap"

      document.addEventListener("turbolinks:load", () => {
        // Both of these are from the Bootstrap 5 docs
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
        var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {
            return new Tooltip(tooltipTriggerEl)
        })

        var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
        var popoverList = popoverTriggerList.map(function(popoverTriggerEl) {
            return new Popover(popoverTriggerEl)
        })
      })
    RUBY
    append_to_file "app/javascript/packs/application.js", application_js_content
  else
    say <<~WARNING, :red
      ERROR: Looks like the webpacker installation is incomplete. Could not find application.js in app/javascript/packs.
    WARNING
  end
end

#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", <<~RUBY, after: /stylesheet_link_tag.*\n/
    \t\t<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  RUBY
end