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

Observers and models does not have access to mails from the mail generator #1770

Closed
wikimatze opened this issue Sep 20, 2014 · 2 comments
Closed

Comments

@wikimatze
Copy link
Member

I'm using the latest padrino version 0.12.3 and have the following defined mailer:

JobVacancy::App.mailer :password_forget do

  email :password_forget_email do |name, email|
    from "admin@job-vacancy.de"
    subject "Password reset"
    to email
    locals :name => name
    user.password_forget
    render 'password_forget/password_forget_email'
  end
end

Using it in a controller like the following works fine:

JobVacancy::App.controllers :forget_password do

  get :new, :map => 'forget_password'  do
    deliver(:password_forget, :password_forget_email, "Matthias", "lordmatze@gmail.com")
    render 'new'
  end

But trying to call it in an observer with a callback function:

class UserObserver < ActiveRecord::Observer

  def before_save(user)
    if user.new_record?
      encrypt_confirmation_code(user)
      deliver(:password_forget, :password_forget_email, "Matthias", "lordmatze@gmail.com")
    end
  end
...
end

or within a model:

class User < ActiveRecord::Base
  def save_forget_password_token(user)
    self.password_reset_token = generate_authentity_token
    self.password_reset_token = 1
    self.save

    # does not work :(
    deliver(:password_forget, :password_forget_email, "Matthias", "lordmatze@gmail.com") 
  end
end

gives me the following error: undefined methoddeliver' for #UserObserver:0x007fc197881d18`

I added the observer in the app/app.rb file with the following line:

module JobVacancy
  class App < Padrino::Application
    ...

    # Activating the user_observer
    ActiveRecord::Base.add_observer UserObserver.instance
  end
end

Did I miss something? Or is there a missing loading path in Padrino core for observers and models?

@wikimatze wikimatze changed the title Observers does not have access to mails from the mail generator Observers and models does not have access to mails from the mail generator Sep 20, 2014
@dariocravero
Copy link

The issue you're facing is that your model doesn't have access to the deliver helper your controller knows about. Calling JobVacancy::App.deliver(...) from your model or observer should do the job. We should probably document that better @padrino/core-members; sorry about that.

@wikimatze
Copy link
Member Author

It works, I updated the part in the book and what you have written.

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

No branches or pull requests

2 participants