ruby-on-rails - Rails-FeatureEnvy : ApplicationHelper#full_title refers to 'page_title' more than self (maybe move it to another class?)

标签 ruby-on-rails ruby reek feature-envy

我正在学习 Michael Hartl 的 Rails 类(class),on chapter 4 , 我的函数 full_title 如下:

app/helpers/application_helper.rb

module ApplicationHelper
  def full_title page_title = ""
    base_title = t "app_name"
    page_title.empty? ? base_title : page_title + " | " + base_title
  end
end

运行 reek(代码审查工具),我收到警告:

app/helpers/application_helper.rb -- 1 warning:
  [4, 4]:FeatureEnvy: ApplicationHelper#full_title refers to 'page_title' more than self (maybe move it to another class?) [https://github.com/troessner/reek/blob/master/docs/Feature-Envy.md]

那么什么是FeatureEnvy以及在这种情况下如何修复它?

最佳答案

documentation of reek解释功能嫉妒如下:

Feature Envy occurs when a code fragment references another object more often than it references itself, or when several clients do the same series of manipulations on a particular type of object.

在这种特殊情况下,我认为这个问题可以忽略。辅助方法很自然地会接受参数并对其进行操作,而不是引用 self(在本例中是 View 对象)。

如何从 reek 的分析中排除助手:

您应该能够通过将其添加到 config.reek 来排除所有助手是否符合“功能羡慕”规则。 文件:

"app/helpers":
  FeatureEnvy:
    enabled: false

关于ruby-on-rails - Rails-FeatureEnvy : ApplicationHelper#full_title refers to 'page_title' more than self (maybe move it to another class?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45039413/

相关文章:

ruby-on-rails - `only:` 处的 `before_action` 在 Rails 中如何工作?

javascript - 有效的 geoJson 未填充 Google map 标记

ruby-on-rails - 设计 Rspec 注册 Controller 测试在更新时失败,就像它试图确认电子邮件地址一样

ruby-on-rails - 如果我们将参数缓存到操作中的本地变量中,它会有所帮助还是相同?

Ruby:使用 Reek 作为训练工具

ruby - NilCheck 修复了安全导航运算符 (&.)

ruby-on-rails - 生产/heroku 失败:WHERE a.attrelid = '"schools"'::regclass

ruby-on-rails - 无法测试应该属于_to,在 Rails 上缺少 id 外键

ruby - Python 类似 __getattr__ 用于 Ruby/JRuby

ruby-on-rails - 无法解密存储的加密数据