Class: Blacklight::Assets::ImportmapGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/blacklight/assets/importmap_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_javascript_dependenciesObject

This could be skipped if you want to use webpacker



9
10
11
12
# File 'lib/generators/blacklight/assets/importmap_generator.rb', line 9

def add_javascript_dependencies
  gem 'bootstrap', options[:'bootstrap-version'].presence # in rails 7, only for stylesheets
  gem 'jquery-rails' if bootstrap_4? # Bootstrap 4 has a dependency on jquery
end

#add_stylesheetObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/blacklight/assets/importmap_generator.rb', line 40

def add_stylesheet
  gem "sassc-rails", "~> 2.1" if Rails.version > '7'

  create_file 'app/assets/stylesheets/blacklight.scss' do
    <<~CONTENT
      @import 'bootstrap';
      @import 'blacklight/blacklight';
    CONTENT
  end
end

#append_blacklight_javascriptObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/blacklight/assets/importmap_generator.rb', line 30

def append_blacklight_javascript
  append_to_file 'app/javascript/application.js' do
    <<~CONTENT
      import bootstrap from "bootstrap"
      import githubAutoCompleteElement from "@github/auto-complete-element"
      import Blacklight from "blacklight"
    CONTENT
  end
end

#bootstrap_4?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/generators/blacklight/assets/importmap_generator.rb', line 51

def bootstrap_4?
  options[:'bootstrap-version'].match?(/\A[^0-9]*4\./)
end

#import_javascript_assetsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generators/blacklight/assets/importmap_generator.rb', line 14

def import_javascript_assets
  append_to_file 'config/importmap.rb' do
    <<~CONTENT
      pin "@github/auto-complete-element", to: "https://cdn.skypack.dev/@github/auto-complete-element"
      pin "@popperjs/core", to: "https://ga.jspm.io/npm:@popperjs/core@2.11.6/dist/umd/popper.min.js"
      pin "bootstrap", to: "https://ga.jspm.io/npm:bootstrap@#{(defined?(Bootstrap) && Bootstrap::VERSION) || '5.3.2'}/dist/js/bootstrap.js"
    CONTENT
  end

  append_to_file 'app/assets/config/manifest.js' do
    <<~CONTENT
      //= link blacklight/manifest.js
    CONTENT
  end
end