Class: StimulusPdfViewer::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_pdf_worker_meta_tagObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generators/stimulus_pdf_viewer/install_generator.rb', line 55

def add_pdf_worker_meta_tag
  say "Adding PDF.js worker meta tag..."

  meta_tag = '<meta name="pdf-worker-src" content="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.379/pdf.worker.min.js">'

  layout_file = "app/views/layouts/application.html.erb"

  if File.exist?(layout_file)
    inject_into_file layout_file, "    #{meta_tag}\n", after: "<head>\n"
    say "Added PDF worker meta tag to #{layout_file}", :green
  else
    say "Could not find #{layout_file}. Please manually add to your <head>:", :yellow
    say meta_tag
  end
end

#add_stylesheet_importObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/stimulus_pdf_viewer/install_generator.rb', line 32

def add_stylesheet_import
  say "Adding stylesheet import..."

  stylesheet_import = '@import "stimulus-pdf-viewer";'

  # Try common stylesheet locations
  stylesheet_files = [
    "app/assets/stylesheets/application.scss",
    "app/assets/stylesheets/application.css.scss",
    "app/assets/stylesheets/application.sass.scss"
  ]

  stylesheet_file = stylesheet_files.find { |f| File.exist?(f) }

  if stylesheet_file
    append_to_file stylesheet_file, "\n#{stylesheet_import}\n"
    say "Added stylesheet import to #{stylesheet_file}", :green
  else
    say "Could not find a SCSS stylesheet. Please manually add:", :yellow
    say stylesheet_import
  end
end

#copy_example_partialsObject



71
72
73
74
75
76
# File 'lib/generators/stimulus_pdf_viewer/install_generator.rb', line 71

def copy_example_partials
  if yes?("Would you like to copy example view partials? (y/n)")
    directory "views", "app/views/pdf_viewer"
    say "Copied example partials to app/views/pdf_viewer/", :green
  end
end

#register_stimulus_controllerObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/stimulus_pdf_viewer/install_generator.rb', line 10

def register_stimulus_controller
  say "Registering Stimulus controller..."

  controller_registration = <<~JS

    // PDF Viewer
    import { PdfViewerController, PdfDownloadController } from "stimulus-pdf-viewer"
    application.register("pdf-viewer", PdfViewerController)
    application.register("pdf-download", PdfDownloadController)
  JS

  controllers_file = "app/javascript/controllers/index.js"

  if File.exist?(controllers_file)
    append_to_file controllers_file, controller_registration
    say "Added controller registration to #{controllers_file}", :green
  else
    say "Could not find #{controllers_file}. Please manually register the controllers:", :yellow
    say controller_registration
  end
end

#show_next_stepsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/stimulus_pdf_viewer/install_generator.rb', line 78

def show_next_steps
  say ""
  say "=" * 60, :green
  say "stimulus-pdf-viewer installed successfully!", :green
  say "=" * 60, :green
  say ""
  say "Next steps:"
  say "1. Create an Annotation model and controller for your app"
  say "2. Set up routes for annotations REST API"
  say "3. Add the PDF viewer partial to your views"
  say ""
  say "See the README for detailed integration instructions:"
  say "https://github.com/jhubert/stimulus-pdf-viewer-rails"
  say ""
end