如何让所有用户在 Rails 上使用 Redmine/Ruby 中的插件?

How to get all Users using a Plugin in Redmine/Ruby on Rails?

我正在 Rails atm Ruby 中为 Redmine 编写插件。 我需要将所有 "Users" 到 link 他们都变成 "Skill"。所以我需要 所有用户都与我的技能有关系。因为它是一个插件,我不想写在 Redmine 中的主要用户模型。所以,我有点想扩展原始用户模型。 任何人都知道我该如何解决这个问题?

如果你想向已经存在的 class 添加逻辑(比如添加新方法、关系、验证等),你可以使用 Ruby Module#class_eval :

User.class_eval do
  # Inside this block we add the new logic that we want to add to the User class

  def new_method
  end
end

在 Redmine 中修补模型我曾经使用这种方法:

# plugins/your_plugin_name/lib/your_plugin_name/user_path.rb
module YourPluginName
  module UserPatch
    extend ActiveSupport::Concern

    included do
      has_many :skills
    end

    def some_new_method 
    end
  end
end

User.include YourPluginName::UserPatch