Techioz Blog

4.0.0 と ActionText::RichText へのアップグレードを捜索する

概要

許可されたransack可能な属性を明示的に定義する必要がある3.xバージョン4.0からransackをアップグレードするのに苦労しています。すべてのプロジェクトモデルを修正しましたが、Railsコアモジュールで失敗します。

# RuntimeError:
     #   Ransack needs ActionText::RichText attributes explicitly allowlisted as
     #   searchable. Define a `ransackable_attributes` class method in your `ActionText::RichText`
     #   model, watching out for items you DON'T want searchable (for
     #   example, `encrypted_password`, `password_reset_token`, `owner` or
     #   other sensitive information). You can use the following as a base:
     #   
     #   ```ruby
     #   class ActionText::RichText < ApplicationRecord
     #   
     #     # ...
     #   
     #     def self.ransackable_attributes(auth_object = nil)
     #       ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
     #     end
     #   
     #     # ...
     #   
     #   end
     #   ```

すでにソース内でクラスを直接再開しようとしましたが、その変更はRailsによってフェッチされず、無視されました。初期化中にいくつかの設定変更を見つけようとしましたが、これも機能しません。きっと誰かがこの 3.0 から 4.x への移行をすでに解決しているに違いありません

class ActionText::RichText < ApplicationRecord

  # ...

  def self.ransackable_attributes(auth_object = nil)
    ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
  end

  # ...

end

結果 -> 単に無視され、同じ問題のレールにはそれらの変更が表示されません。

    class ActionText::RichText < ActiveRecord::Base
      def self.ransackable_attributes(auth_object = nil)
        ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
      end
    end

初期ロード中の結果エラー ->

An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `has_many_attached' for ActionText::RichText:Class
# ./config/initializers/action_text.rb:7:in `<main>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:6:in `require'
# ./spec/rails_helper.rb:6:in `<top (required)>'
No examples found. 

解決策

ログを作成する必要があるようです - 推奨されるメソッドを明示的に定義します。 ActionText::RichText < ActionText::Record < ActiveRecord::Base のような継承があるため、このメソッドを ActionText::RichText の親クラスで定義できます。

# config/initializers/action_text.rb

class ActionText::Record < ActiveRecord::Base
  def self.ransackable_attributes(auth_object = nil)
    authorizable_ransackable_attributes
  end
end

このメソッドを ActionText::RichText クラスのイニシャライザで直接定義することもできます。ただし、「method_missing」:ActionText::RichText:Class (NoMethodError) の未定義メソッド「has_many_attached」(質問テキストからのエラー)は、読み込み順序が原因で発生する可能性があります(この DSL メソッドはまだ使用できません)。

これを回避するには、ActiveSupport フックを使用できます。その名前はここで確認できます。

この場合、パッチは次のようになります

# config/initializers/action_text.rb

ActiveSupport.on_load(:action_text_rich_text) do
  class ActionText::RichText < ActionText::Record
    def self.ransackable_attributes(auth_object = nil)
      authorizable_ransackable_attributes
    end
  end
end

もちろん、authorizable_ransackable_attributes の代わりに、%w[id body name Record_id Record_type] などの必要な属性の明示的な配列 (文字列配列) を使用することもできます。