Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method before_deliver when included #56

Closed
EleanorRagone opened this issue Aug 30, 2017 · 10 comments
Closed

Undefined method before_deliver when included #56

EleanorRagone opened this issue Aug 30, 2017 · 10 comments

Comments

@EleanorRagone
Copy link

EleanorRagone commented Aug 30, 2017

Hi there -

Following the instructions on the readme, I added include MailForm::Delivery on an existing class I had. I then received undefined method `before_deliver' when running my tests. I eventually solved it by also adding include MailForm::Shim. I'm not sure if that's the "right" way to do it, but I figured I'd post here in case 1: Someone else has this issue; 2: It's the wrong way and you can correct me; or 3: It can be added to the README

@will-r
Copy link

will-r commented Sep 18, 2017

Same error and fix here. Thanks for the tip. Mailform::Shim has to be included before Mailform::Delivery so that the callbacks are defined.

@colinatkins
Copy link

+1

Same error, same fix:

class SupportMail < ApplicationRecord
  include MailForm::Shim
  include MailForm::Delivery
end

@t3k4y
Copy link

t3k4y commented Feb 5, 2018

Same error, but including MailForm::Shim is not solving ist. Leaving my Object #<... not initialized>
Any hints on this?

@t3k4y
Copy link

t3k4y commented Feb 9, 2018

Question posted at Stackoverflow

@stevenkolstad
Copy link

including MailForm::Shim gives this error:

undefined method `[]' for nil:NilClass
<%= simple_form_for(@feedback)

@francescob
Copy link

I have the same problem as t3k4y , did you find a solution?

@mad42
Copy link

mad42 commented Jul 24, 2018

undefined method `name' for #<Contact not initialized>

same problem, any update on this ?

@will-r
Copy link

will-r commented Aug 20, 2018

It seems that MailForm::Shim is meant to make a non-ActiveModel behave more like an ActiveModel, and including it into an ActiveRecord model now causes various troubles. A better fix is to add

define_model_callbacks :deliver

before you

include MailForm::Delivery

@mkolodziej
Copy link

mkolodziej commented May 6, 2020

mail_form is broken if you try to use it with ActiveRecord, which used to work before Rails 5.1. The culprit is this code fragment in delivery.rb:

# not_spam? simply returns false, but when used as a callback, we have to throw(:abort)
# to halt the callback chain, beginning with Rails 5.1.
if ActionPack.respond_to?(:version) && ActionPack.version >= Gem::Version.new('5.1')
  before_deliver :check_not_spam
  after_deliver  :deliver!
elsif respond_to?(:before_deliver) && respond_to?(:after_deliver)
  before_deliver :not_spam?
  after_deliver  :deliver!
else # For ActiveRecord compatibility
  before_create :not_spam?
  after_create  :deliver!
  alias :deliver :save
end

It used to add abefore_create and after_create callbacks for AR, but now it doesn't. It adds callbacks around deliver method, which is missing, because it is defined in the MailForm::Shim, which should not be used with AR.

My solution is to add the method and after_create callback before including MailForm::Delivery. The whole fix looks like this:

define_model_callbacks :deliver
# Create just check validity, and if so, trigger callbacks.
def deliver
  if valid?
    run_callbacks :deliver
  else
    false
  end
end
after_create :deliver
include MailForm::Delivery

@carlosantoniodasilva
Copy link
Member

carlosantoniodasilva commented Aug 28, 2020

@mkolodziej appreciate you sharing this snippet! I just pushed a fix for this AR integration in MailForm, it's been broken for quite a while 😅. I'll look into pushing a new version in the next few days, just want to wrap up a few other things here. In the meantime, if anyone here wants to try out master, please let me know how that worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

9 participants