ruby-on-railsrubypass-by-referencescoping

Rails Mailer: Pass `attachments` to other methods


I'm working on a standard Rails app that uses a mailer to send email.

Each mailer class has an attachments attribute [src on GitHub] that aliases an instance variable.

I'm trying to pass that attachments reference to a function defined in helpers/yeet.rb, but inside the helper attachments does not behave the way it does in the mailer scope.

My Question: How can I interact with attachments and do things like attachments.inline['a.png'] = URI.open('a.png').read in the helper function?


Solution

  • Ah, given the Github source for attachments I just needed to add:

    attr_reader :_message
    attr_writer :_message
    

    Then I could pass self as an argument to the function defined in the helper, and inside that helper function, I was able to access mailer_self._message.attachments and mutate the attachments directly. (mailer_self is the variable name of the positional argument in the helper function to which I passed self in the mailer function itself).