Class: ActionText::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#append_javascript_dependenciesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/action_text/install/install_generator.rb', line 18

def append_javascript_dependencies
  destination = Pathname(destination_root)

  if (application_javascript_path = destination.join("app/javascript/application.js")).exist?
    insert_into_file application_javascript_path.to_s, %(import "trix"\nimport "@rails/actiontext"\n)
  else
    say <<~INSTRUCTIONS, :green
      You must import the @rails/actiontext and trix JavaScript modules in your application entrypoint.
    INSTRUCTIONS
  end

  if (importmap_path = destination.join("config/importmap.rb")).exist?
    append_to_file importmap_path.to_s, %(pin "trix"\npin "@rails/actiontext", to: "actiontext.js"\n)
  end
end

#create_actiontext_filesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/action_text/install/install_generator.rb', line 34

def create_actiontext_files
  destination = Pathname(destination_root)

  template "actiontext.css", "app/assets/stylesheets/actiontext.css"

  unless destination.join("app/assets/application.css").exist?
    if (stylesheets = Dir.glob "#{destination_root}/app/assets/stylesheets/application.*.{scss,css}").length > 0
      insert_into_file stylesheets.first.to_s, %(@import 'actiontext.css';)
    else
      say <<~INSTRUCTIONS, :green
        To use the Trix editor, you must require 'app/assets/stylesheets/actiontext.css' in your base stylesheet.
      INSTRUCTIONS
    end
  end

  gem_root = "#{__dir__}/../../../.."

  copy_file "#{gem_root}/app/views/active_storage/blobs/_blob.html.erb",
    "app/views/active_storage/blobs/_blob.html.erb"

  copy_file "#{gem_root}/app/views/layouts/action_text/contents/_content.html.erb",
    "app/views/layouts/action_text/contents/_content.html.erb"
end

#create_migrationsObject



65
66
67
# File 'lib/generators/action_text/install/install_generator.rb', line 65

def create_migrations
  rails_command "railties:install:migrations FROM=active_storage,action_text", inline: true
end

#enable_image_processing_gemObject



58
59
60
61
62
63
# File 'lib/generators/action_text/install/install_generator.rb', line 58

def enable_image_processing_gem
  if (gemfile_path = Pathname(destination_root).join("Gemfile")).exist?
    say "Ensure image_processing gem has been enabled so image uploads will work (remember to bundle!)"
    uncomment_lines gemfile_path, /gem "image_processing"/
  end
end

#install_javascript_dependenciesObject



11
12
13
14
15
16
# File 'lib/generators/action_text/install/install_generator.rb', line 11

def install_javascript_dependencies
  if Pathname(destination_root).join("package.json").exist?
    say "Installing JavaScript dependencies", :green
    run "yarn add @rails/actiontext trix"
  end
end