ruby Ransack升级到4.0.0和Ransack Text::RichText

bqf10yzr  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(98)

我正在努力从3.x版本4.0升级ransack,在4.0版本中,允许的ransackable属性应该被显式定义,我已经修复了所有的项目模型,但现在它在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的迁移问题

已经测试了什么

1.在app/models/action_text/rich_text.rb下创建模型文件

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

Result -> just ignored,same issue rails does not see those changes.
1.在config/initializers/action_text.rb下更新初始化器

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.
ykejflvf

ykejflvf1#

看起来你需要让日志说-显式定义建议的方法。既然有这样的传承: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': undefined method 'has_many_attached' for ActionText::RichText:Class (NoMethodError)(来自问题文本的错误)可能会因为加载顺序而引发(此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]等。

相关问题