ruby-on-railsrubymailboxer

RoR Mailboxer Monkey Patch works only a few times


I am adding a method to Mailboxer::Conversation to retrieve a link using mailboxer's emails to reply (i.e. reply_link).

I've decided to monkey patch mailboxer within my application. What I've done exactly is the following:

  1. Created the folder structure lib/mailboxer/extensions.
  2. Added files lib/mailboxer/extensions/conversation.rb, lib/mailboxer/extensions.rb, lib/mailboxer.rb.

The following is the content of the files:

# lib/mailboxer/extensions/conversation.rb
module Mailboxer
  module Extensions
    module Conversation
      def reply_link
        "/mail?notif_id=#{id}"
      end
    end
  end
end

# lib/mailboxer/extensions.rb
require 'mailboxer/extensions/conversation'

# lib/mailboxer.rb
require 'mailboxer/extensions'

My config/application.rb has the following:

config.autoload_paths += %W(#{config.root}/lib)

Which gives me access to my lib folder. Then what I do is include Mailboxer::Extensions::Conversation to Mailboxer::Conversation within the mailboxer initializer file initalizers/mailboxer.rb:

Mailboxer.setup do |config|
  # ...
end

Mailboxer::Conversation.include Mailboxer::Extensions::Conversation

In my rails console, the code always works. However in the website, the reply_link method works at first, then becomes undefined randomly.

enter image description here

Couple of attempts later....

enter image description here

and it stops working until I restart the server...

Whenever I get an unrelated exception (i.e. typo, refactoring, etc.) the reply_link method becomes undefined. Could this be a development thing?

I could fork mailboxer, make my changes then go on. But the method is so custom to my application that I'd rather just patch.

If anyone has any suggestions, advice or questions I truly appreciate the advice.


Solution

  • First, I am still convinced that this is a development issue only. Whenever I have time to spare, I will test this out and post here.

    Second, to ensure this never happened again I copied the source for the Mailboxer's Conversation and added an inclusion include MailboxerExt::Conversation.

    I also structured my extension to not collide, reload Mailboxer's namespace.

    The final result has the folders app/models/mailboxer, lib/mailboxer_ext.

    The files are app/models/mailboxer/conversation.rb, lib/mailboxer_ext.rb and lib/mailboxer_ext/conversation.rb.