高木のブログ

既存のRailsアプリからAction CableやAction Mailerなどの不要な機能を取り除く

2021/12/15

既存のRailsアプリからAction CableやAction Mailerなどの不要な機能を取り除いたのでその手順を記録として残す

初めから不要なのがわかっていれば、rails new をするタイミングでスキップするオプションを付ければ良い

不要な機能をスキップするオプション:

$ rails new -h | grep Skip
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
  -G, [--skip-git], [--no-skip-git]                          # Skip .gitignore file
      [--skip-keeps], [--no-skip-keeps]                      # Skip source control .keep files
  -M, [--skip-action-mailer], [--no-skip-action-mailer]      # Skip Action Mailer files
      [--skip-action-mailbox], [--no-skip-action-mailbox]    # Skip Action Mailbox gem
      [--skip-action-text], [--no-skip-action-text]          # Skip Action Text gem
  -O, [--skip-active-record], [--no-skip-active-record]      # Skip Active Record files
      [--skip-active-job], [--no-skip-active-job]            # Skip Active Job
      [--skip-active-storage], [--no-skip-active-storage]    # Skip Active Storage files
  -P, [--skip-puma], [--no-skip-puma]                        # Skip Puma related files
  -C, [--skip-action-cable], [--no-skip-action-cable]        # Skip Action Cable files
  -S, [--skip-sprockets], [--no-skip-sprockets]              # Skip Sprockets files
  -J, [--skip-javascript], [--no-skip-javascript]            # Skip JavaScript files
      [--skip-turbolinks], [--no-skip-turbolinks]            # Skip turbolinks gem
      [--skip-jbuilder], [--no-skip-jbuilder]                # Skip jbuilder gem
  -T, [--skip-test], [--no-skip-test]                        # Skip test files
      [--skip-system-test], [--no-skip-system-test]          # Skip system test files
      [--skip-bootsnap], [--no-skip-bootsnap]                # Skip bootsnap gem
      [--no-rc], [--no-no-rc]                                # Skip loading of extra configuration options from .railsrc file
  -s, [--skip], [--no-skip]        # Skip files that already exist

手順

正式な取り除き方がわからなかったので、スキップするオプションを付けるパターン、付けないパターンで rails new して、その差分を見て機能を削除した

この記事ではAction Cableを取り除いた手順を載せる

使用した Rails のバージョンは 6.1.4

スキップするオプションを付けた時と付けない時の差分を確認する

アプリを作成

$ rails new sample_app -G -B
$ mv sample_app rails-all
$ rails new sample_app -G -B -C
$ mv sample_app rails-without-action-cable

アプリ名とデータベース名が違うと差分が見づらくなるので、「sample_app」というアプリ名で作成したあとにディレクトリ名をわかりやすい名前に変更する

-G は Git管理 をスキップ
-Bbundle install をスキップ
-C は Action Cable をスキップ

差分を確認

diff コマンドでディレクトリ単位の差分を確認する
credentials.yml.encmaster.key は必ず差分が出てしまうので除外

$ diff -r -x credentials.yml.enc -x master.key rails-all rails-without-action-cable
$ diff -r -x credentials.yml.enc -x master.key rails-all rails-without-action-cable
diff -r -x credentials.yml.enc -x master.key rails-all/Gemfile rails-without-action-cable/Gemfile
20,21d19
< # Use Redis adapter to run Action Cable in production
< # gem 'redis', '~> 4.0'
Only in rails-all/app: channels
Only in rails-all/app/javascript: channels
diff -r -x credentials.yml.enc -x master.key rails-all/app/javascript/packs/application.js rails-without-action-cable/app/javascript/packs/application.js
9d8
< import "channels"
diff -r -x credentials.yml.enc -x master.key rails-all/config/application.rb rails-without-action-cable/config/application.rb
3c3,16
< require "rails/all"
---
> require "rails"
> # Pick the frameworks you want:
> require "active_model/railtie"
> require "active_job/railtie"
> require "active_record/railtie"
> require "active_storage/engine"
> require "action_controller/railtie"
> require "action_mailer/railtie"
> require "action_mailbox/engine"
> require "action_text/engine"
> require "action_view/railtie"
> # require "action_cable/engine"
> require "sprockets/railtie"
> require "rails/test_unit/railtie"
Only in rails-all/config: cable.yml
diff -r -x credentials.yml.enc -x master.key rails-all/config/environments/production.rb rails-without-action-cable/config/environments/production.rb
43,47d42
<   # Mount Action Cable outside main process or domain.
<   # config.action_cable.mount_path = nil
<   # config.action_cable.url = 'wss://example.com/cable'
<   # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
<
diff -r -x credentials.yml.enc -x master.key rails-all/package.json rails-without-action-cable/package.json
7,8c7
<     "@rails/activestorage": "^6.0.0",
<     "@rails/actioncable": "^6.0.0"
---
>     "@rails/activestorage": "^6.0.0"
Only in rails-all/test: channels

不要な記述、ディレクトリ、ファイルを削除する

差分を見ながら不要なものを削除していく

不要な記述の削除

Gemfile
 gem 'turbolinks', '~> 5'
 # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
 gem 'jbuilder', '~> 2.10'
-# Use Redis adapter to run Action Cable in production
-# gem 'redis', '~> 4.0'
 # Use Active Model has_secure_password
 # gem 'bcrypt', '~> 3.1.7'
config/application.rb
 require_relative 'boot'

-require 'rails/all'
+require 'rails'
+require 'active_model/railtie'
+require 'active_job/railtie'
+require 'active_record/railtie'
+require 'active_storage/engine'
+require 'action_controller/railtie'
+require 'action_mailer/railtie'
+require 'action_mailbox/engine'
+require 'action_text/engine'
+require 'action_view/railtie'
+require 'sprockets/railtie'
+require 'rails/test_unit/railtie'

 # Require the gems listed in Gemfile, including any gems
 # you've limited to :test, :development, or :production.
package.json
   "name": "masamune",
   "private": true,
   "dependencies": {
-    "@rails/actioncable": "^6.0.4",
     "@rails/activestorage": "^6.0.4",
     "@rails/ujs": "^6.0.4",
     "@rails/webpacker": "5.1.1",
app/javascript/packs/application.js
 require("@rails/ujs").start()
 require("turbolinks").start()
 require("@rails/activestorage").start()
-require("channels")

 require("chartkick/chart.js")
 require("admin-lte")
config/environments/production.rb
   # Store uploaded files on the local file system (see config/storage.yml for options).
   config.active_storage.service = :local

-  # Mount Action Cable outside main process or domain.
-  # config.action_cable.mount_path = nil
-  # config.action_cable.url = 'wss://example.com/cable'
-  # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
-
   # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
   # config.force_ssl = true

不要なディレクトリとファイルの削除

  • app/channels
    • app/channels/application_cable/channel.rb
    • app/channels/application_cable/connection.rb
  • app/javascript/channels
    • app/javascript/channels/consumer.js
    • app/javascript/channels/index.js
  • test/channels
    • test/channels/application_cable/connection_test.rb
  • config/cable.yml

上記を取り除きたい機能の数だけ繰り返す

リポジトリ

他のパターンで rails new したものもGitHubに上げてある
ytkg/rails-6-1-4-diff - GitHub

削除する順番

  • Action Cable
  • Action Mailer
  • Action Mailbox
  • Action Text
  • Active Job
  • Active Storage

これらを削除する場合、依存関係の都合でこの順番でやると良さそう


ytkg

Written by ytkg, Twitter, GitHub