Open link in a new tab

Content

Expand project menu

How to implement associations between plugin tables and existing OpenProject tables?

I'm developing a Service Pack plugin allowing user to track units from purchased service pack when user log time for tasks. My issues is the new tables must relate to the existing OpenProject tables. In this case, we have to implement two "has-many through" associations, which are between ServicePacks and Projects, and between ServicePack and Enumerations,  that requires modifying the OpenProject code. Is there any way to implement these associations from the plugin without modifying the existing OP core?

Message actions

Hi,

with our Rails plugins hooks, you can patch the OpenProject core by simply extending modules within a plugin due to Ruby's metaprogramming.

See the following easy example: The Documents plugin patches the Project class  with the following code:

module OpenProject::Documents::Patches
  module ProjectPatch
    def self.included(base)
      base.class_eval do

        has_many :documents, dependent: :destroy
      end
    end
  end
end

Project.send(:include, OpenProject::Documents::Patches::ProjectPatch)

Source: https://github.com/opf/openproject/blob/dev/modules/documents/lib/open_project/documents/patches/project_patch.rb

This patch is registered at the engine of the plugin here: https://github.com/opf/openproject/blob/dev/modules/documents/lib/open_project/documents/engine.rb#L65

Best,

Oliver

Message actions

thank you, I have successfully implement the associations following your guide. However, i'm having a new problem. I want use Action Mailer to send email to user if the Service Pack has expired. I have generate the ServicePackMailer following rails guide but I can not preview the ServicePackMailer when I start the OP. It said the mailer not found. The same after I try re-bundle install. Do I have to register something in the engine file like the last problem?

Message actions

This patch is registered at the engine of the plugin here: https://github.com/opf/openproject/blob/dev/modules/documents/lib/open_project/documents/engine.rb#L65 uno online

This is what I am looking for, Thanks for sharing it.