1

I'm coding a Plugin for Redmine in Ruby on Rails atm. I need to get all "Users" to link them to a "Skill". So I need all users to make a relationship to my skills. As it is a plugin, I don't want to write in the main users model in Redmine. So, I kinda want to extend or something the original user model. Anyone has a clue how I can solve this?

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
Jay Dee
  • 27
  • 4
  • Can't you just do `User.all` inside the plugin ? Or are you talking about creating a method in the User class ? – Viktor May 23 '20 at 12:50

2 Answers2

1

If you want to add logic to an already existing class (like adding new methods, relationships, validations, etc..), you can do it with 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
0

To patch models in Redmine I used to use this approach:

# 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
Yakov
  • 3,033
  • 1
  • 11
  • 22