为什么我会收到此错误:请确保包含 ActiveModel::Naming

发布于 2024-11-17 14:05:09 字数 348 浏览 0 评论 0原文

我正在安装一个名为 Completeness-fu 的 gem。

这是我收到的错误:

completeness-fu-0.6.0/lib/completeness-fu/active_model_additions.rb:12:in `define_completeness_scoring': please make sure ActiveModel::Naming is included so completeness_scoring can translate messages correctly (CompletenessFu::CompletenessFuError)

这是什么意思?

I am installing a gem called completeness-fu.

This is the error I get:

completeness-fu-0.6.0/lib/completeness-fu/active_model_additions.rb:12:in `define_completeness_scoring': please make sure ActiveModel::Naming is included so completeness_scoring can translate messages correctly (CompletenessFu::CompletenessFuError)

What does that mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-11-24 14:05:09

ActiveModel::Naming 创建一个 #model_name 对象上的方法。 Completeness-fu gem 似乎需要它来用于 #define_completeness_scoring 方法。

来自自述文件

Completeness-Fu 过去仅适用于 ActiveRecord,但最新版本已更改,仅需要 ActiveModel 作为 gem 依赖项(以及一点 ActiveSupport)。这意味着最新版本适用于以下 ORM:

  • ActiveRecord
  • 蒙古人
  • Cassandra 对象

默认情况下,define_completeness_scoring 方法 混合到上述 ORM 中,但如果您想在不属于其中之一的 ActiveModel 模型中使用 Completeness-Fu在 ORM 之上,请确保包含 ActiveModel::Naming。如果您想缓存完整性分数,请确保还包括 ActiveModel::ValidationsActiveModel::Validations::Callbacks

更新

见评论;根本问题似乎是 gem 中的一个错误(截至 2011 年 6 月 29 日)。这可以使用以下代码进行猴子修补(假设 Ruby 1.9)。唯一的变化是使用 self.singleton_class.include?(ActiveModel::Naming) 来获得正确的响应。

# lib/completeness-fu.rb
module CompletenessFu
  module ActiveModelAdditions
    def self.included(base)
      base.class_eval do
        def self.define_completeness_scoring(&checks_block)
          # The check below is causing the problem; the simplest fix in Ruby 1.8
          # would be to simply remove it by commenting out the following 3 lines.
          unless self.singleton_class.include?(ActiveModel::Naming)
            raise CompletenessFuError, 'please make sure ActiveModel::Naming is included so completeness_scoring can translate messages correctly'
          end

          class_inheritable_array :completeness_checks
          cattr_accessor :default_weighting
          cattr_accessor :model_weightings

          self.send :extend, ClassMethods
          self.send :include, InstanceMethods

          checks_results = CompletenessFu::ScoringBuilder.generate(self, &checks_block)

          self.default_weighting   = checks_results[:default_weighting]
          self.completeness_checks = checks_results[:completeness_checks]
          self.model_weightings    = checks_results[:model_weightings]

          if checks_results[:cache_score_details]
            unless self.include?(ActiveModel::Validations::Callbacks)
              raise CompletenessFuError, 'please make sure ActiveModel::Validations::Callbacks is included before define_completeness_scoring if you want to cache competeness scoring'
            end
            self.before_validation checks_results[:cache_score_details]
          end
        end
      end
    end
  end
end

警告!!!

这是一个超级临时(且脆弱)的猴子补丁,我真的不建议在生产能力中实际使用这样的代码。

更好的选择是为此找到一个不同的 gem,与作者合作修复它,分叉该项目并自行修复它,然后在您的项目中使用该分叉(更多信息),或者只是使用 gem 作为起始设计在您的应用程序中本地重建此功能。

ActiveModel::Naming creates a #model_name method on your object. The completeness-fu gem seems to require it for the #define_completeness_scoring method.

From the readme:

Completeness-Fu used to be for ActiveRecord only, but this has changed with the latest version with only ActiveModel needed as a gem dependency (and a little ActiveSupport). This means that the latest version works with the following ORMs :

  • ActiveRecord
  • Mongoid
  • Cassandra Object

The define_completeness_scoring method is mixed into the above mentioned ORMs by default, but if you want to use Completeness-Fu in an ActiveModel model which isn't one of the above ORMs, make sure you include ActiveModel::Naming. If you want to cache the completeness score make sure you also include ActiveModel::Validations and ActiveModel::Validations::Callbacks.

Updated

See comments; the underlying issue seems to be a bug in the gem (as of 29 June, 2011). This could be monkey-patched using the following code (assuming Ruby 1.9). The only change is using self.singleton_class.include?(ActiveModel::Naming) to get the proper response.

# lib/completeness-fu.rb
module CompletenessFu
  module ActiveModelAdditions
    def self.included(base)
      base.class_eval do
        def self.define_completeness_scoring(&checks_block)
          # The check below is causing the problem; the simplest fix in Ruby 1.8
          # would be to simply remove it by commenting out the following 3 lines.
          unless self.singleton_class.include?(ActiveModel::Naming)
            raise CompletenessFuError, 'please make sure ActiveModel::Naming is included so completeness_scoring can translate messages correctly'
          end

          class_inheritable_array :completeness_checks
          cattr_accessor :default_weighting
          cattr_accessor :model_weightings

          self.send :extend, ClassMethods
          self.send :include, InstanceMethods

          checks_results = CompletenessFu::ScoringBuilder.generate(self, &checks_block)

          self.default_weighting   = checks_results[:default_weighting]
          self.completeness_checks = checks_results[:completeness_checks]
          self.model_weightings    = checks_results[:model_weightings]

          if checks_results[:cache_score_details]
            unless self.include?(ActiveModel::Validations::Callbacks)
              raise CompletenessFuError, 'please make sure ActiveModel::Validations::Callbacks is included before define_completeness_scoring if you want to cache competeness scoring'
            end
            self.before_validation checks_results[:cache_score_details]
          end
        end
      end
    end
  end
end

Warning!!!

This is a super temporary (and fragile) monkey-patch, and I really don't recommend actually using code like this in a production capacity.

The better options would be to find a different gem for this, work with the author to get it fixed, fork the project and fix it yourself then use that fork in your project (more info) or simply rebuild this functionality in your app locally using the gem as a starting design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文