How to I create a preview for devise_intiable mailer? - ruby-on-rails

I hooked up devise_invitable gem
And everything is working correctly in my dev environment. I can view the mailer html in the terminal, I can copy the URL with the token it sends. And when I put that in browser and go to that path it works perfectly.
I would however like to create a preview for this so I can preview the mailer as I get that part how I want it.
Here's what I've tried so far.
In test/mailers/previews
and my code is something like this:
class InviteAdminPreview < ActionMailer::Preview
def invite
InviteAdminMailer.invite(Admin.last)
end
end
but this isn't really working because I don't know what the actual invite mailer is called. Because in app/mailers where I would have expected devise_invitable to place this mailer, there is nothing there.
So should I create my own mailer here called InviteAdminMailer? Or is there some other way to do it?
Also will i have to create this .invite method myself in this mailer so this preview will work?

First of all, to create a customized mail for devise_invitable usage, you can reference the article "Customizing for different Invite use cases (emails etc.)" https://github.com/scambra/devise_invitable/wiki/Customizing-for-different-Invite-use-cases-(emails-etc.) from devise_invitable wiki.
According to devise_invitable source code ( https://github.com/scambra/devise_invitable/blob/cf77519ca9d02c112d99fd176a8852272c1b8d99/lib/devise_invitable/models.rb#L210 ), it uses the mailer provided by devise, so there is no devise_inviatble mailer. If you want to preview devise mails, you can do something as below:
From: https://gist.github.com/ThawanFidelis/ac4a215b841619eae7d8
# config/enviroments/development.rb
config.action_mailer.preview_path = "#{Rails.root}/app/mailer_previews"
# app/mailer_previews/devise_mailer_preview.rb
class Devise::MailerPreview < ActionMailer::Preview
def confirmation_instructions
Devise::Mailer.confirmation_instructions(User.first, {})
end
def unlock_instructions
Devise::Mailer.unlock_instructions(User.first, "faketoken")
end
def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end
end

This worked for me
def invitation_instructions
Devise::Mailer.invitation_instructions(User.all.sample, 'faketoken')
end

Related

Fixtures in mail preview

Having the following mailer previewer code:
class RegistrationMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/registration_mailer/welcome
def welcome
RegistrationMailer.welcome users(:one)
end
end
(full file).
Which is unable to reach my fixtures (users(:one)), return a 500 error status and print out the following error:
NoMethodError: undefined method `users' for #RegistrationMailerPreview
Can we get fixtures entries from mailer previewer?
If yes, I would like to know how to do that.
I have seen that should be possible here, but I can't require test_helper in this file (I don't know why) and I don't understand the difference between ActionMailer::TestCase and ActionMailer::Preview.
If no, is there a way to preview the mail without sending as a parameter User.first, since I could do my tests on a machine on which there is no data filled in the database.
I don't know about the default fixture framework, but I can say using FactoryBot for my fixtures that I was able to use them in my mailer previews simply by prepending the build/create methods with FactoryBot. I didn't need to require anything at the top of the file. i.e.:
class RegistrationMailerPreview < ActionMailer::Preview
def welcome
user = FactoryBot.create(:user)
RegistrationMailer.welcome(user)
end
end
To answer your second question, you could also simply replace the fixture line above with user = User.new(firstname: "Joe"). That would create a new user to use in the preview without persisting it to the database.

special-delivery gem rails configuration?

I can't get my EmailCallback::MailgunEmail < SpecialDelivery::Callback class to be included in execution. My problem is I'm not clear on where and how to create and call the new class mentioned in the readme. Currently when I fire of a POST request to the mount SpecialDelivery::Engine => "/email_events" endpoint with all correct params, it is processed by SpecialDelivery::EventsController#create. An object is instantiated but not saved.
For reference; the gem readme section: https://github.com/vigetlabs/special-delivery#your-custom-callback-class-file
I ran the undocumented rake task (special_delivery:install:migrations) and migrated to new migration file.
I currently have:
# /lib/email_callback/mailgun_email.rb
module EmailCallback
class MailgunEmail < SpecialDelivery::Callback
def opened
require 'pry'; binding.pry
end
def create
require 'pry'; binding.pry
end
end
end
Note: I'm trying to hit the pry debugger to confirm the request is hitting the appropriate method and to see what I have to work with.
Thanks in advance for any help.
The new class should be referenced in your invocation of the #special_delivery method that wraps your email sending.
def send_your_email(user)
special_delivery(
callback_class: EmailCallback::MailgunEmail, # Here!
callback_record: user
) do
mail(to: user.email, subject: 'Hello from Kit!')
end
end
Let me know if that works for you. If not we can dive in a bit deeper and get this sorted out for you.

Rails 4.1: access current_user in ActionMailer::Preview

Rails 4.1 has a nice way to preview mailers with ActionMailer::Preview. All of my mailers take a user parameter, and I would like to pass in current_user (from Devise) for the preview.
If I try this, it doesn't work.
class SubscriptionsMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/subscriptions_mailer/new
def new
SubscriptionsMailer.new(current_user)
end
end
It returns undefined local variable or method 'current_user' for #<SubscriptionsMailerPreview:0xa6d4ee4>.
I suspect this is because current_user is defined by Devise in ApplicationController, and according to the docs, ActionMailer uses AbstractController::Base. In that case, would storing current_user in a class variable be a bad idea?
Does anyone know how I can use the current_user helper in ActionMailer::Preview?
What would happen if you move your mailer job to the background? How would you get the current user then?
The mailer and its preview should not know about the current_user. The mailer's job is to send the mail to a user it receives. The preview is there to visually demonstrate its behaviour.
Create a new user in your mailer preview, and pass it to the mailer.
def new
user = User.create! # etc...
SubscriptionsMailer.new(user)
end
It doesn't matter who the user is. It matters that it's a user object.
If you want to test that the application will send a mail to the current_user, write a functional test for that.
You are right method defined in Controller won't be available in helper.
These posts can help you:
Where do I put helper methods for ActionMailer views? Access helpers from mailer?
https://www.ruby-forum.com/topic/168949

User scope with Devise and Rails 4.1

I'm doing some proof of concepts with Rails. I created a Customer, and apply to it DEVISE.
Everythig works fine, but now i'm trying to logging with a customer and enter directly into his scope.
localhost:3000/customers/1
After this hide the /customers/1 with some word, for example: myProfile.
I'm trying to do the first part with
module ApplicationHelper
protected
def after_sign_in_path_for(resource)
#path to redirect
end
end
I was trying with definitions from routes.rb
redirect_to(customers_path)
or something like:
#customer= Customer.find_by_email(current_customer.email)
redirect_to(customer_path(#customer))
But nothing is working yet.
I'm not sure how to send messages to the console of the server (like in Java with some System.out.println) to check the contents...
Thanks a lot!

Devise Confirmable - Welcome Message

So I am setting up a welcome message when a user signs up the website - previously I had set it up using gmail (http://stackoverflow.com/questions/5793296/rails-actionmailer-w-devise-google-apps-in-development-mode), but it's going to be using google apps - so if I'm correct another stackoverflow user claimed the set up is similar so that's not a problem. But since I only want a welcome email, I was thinking can I just use the confirmable set up so they get an email, and then in the config set it so that the user doesn't have to confirm till after say 1000 years or something large so basically it's not really a confirmation email? (If there is a better way to do this I'd appreciate such input)
you don't need to twist the Confirmable feature to achieve this, you can do it more elegantly with an ActiveRecord::Observer. Basically when you register/save a user the observer will get notified and from there you can call a mailer. You can see an example below.
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "something#something.com"
def welcome_mail(email)
mail(:to => email, :subject => "Welcome to Something").deliver
end
end
app/models/user_observer.rb
class UserObserver < ActiveRecord::Observer
# We check if it's a new user
def before_save(user)
#is_new_record = user.new_record?
true
end
def after_save(user)
# If it's not a new user we don't want to send them another welcome email
if #is_new_record then
UserMailer.welcome_mail(user.email)
end
end
end
Finally you need to configure rails to register the observer.
config/application.rb (just a extract)
config.active_record.observers = :user_observer
it's probably awfully late to answer, but i think there's after_create callback to shrink the solution above since you don't need to check if it's a new record!

Resources