I want to pass a method from the application controller to a mailer to send shopping cart contents to email.
Method in the application_controller.rb:
def current_order
if session[:order_id].present?
Order.find(session[:order_id])
else
Order.new
end
end
Mailer:
class CartMailer < ApplicationMailer
default from: "from#example.com"
def send_cart_contents
#order = current_order
mail(to: "to#example.com", subject: 'Order from the site')
end
end
And the view:
Order from the site
<% #order.order_items.each do |oi| %>
<%= oi.product.name %>
<% end %>
I'm getting an error: undefined local variable or method 'current_order'.
What am I doing wrong? Thank you.
UPDATE
If I'm passing it as a parameter:
# Preview all emails at http://localhost:3000/rails/mailers/cart_mailer
class CartMailerPreview < ActionMailer::Preview
def cart_mailer_preview
CartMailer.send_cart_contents(current_order)
end
end
I'm also getting NameError.
UPDATE 2
CartMailerPreview don't have access to the current_order, so to test it just pass an id with the parameter. When you use it normally all works well.
The CartMailer is not going to have visibility to the current_order defined in application_controller.rb. This is a good thing.
Best practices is to have the send_cart_contents method accept the order so that it can mail it out:
class CartMailer < ApplicationMailer
default from: "from#example.com"
def send_cart_contents(order)
#order = order
mail(to: "to#example.com", subject: 'Order from the site')
end
end
This way you can mail out a cart from a background job and isolates your mailers from your controllers. Relying on a global current_order is not a good practice.
You should pass current_order to the mailer as a parameter.
Related
I have a before action in a user mailer file, which is supposed to stop mailers sending if a column on user is set to true or false. However current user is currently unavailable. I understand why, but was wondering if there was a way to do this.
I want to avoid adding the check_if_users_can_receive_mailers at the top of each mailer method.
before_action :check_if_users_can_receive_mailers
#methods that send mailers
private
def check_if_users_can_receive_mailers
current_user.send_mailers?
end
You have to make the current user available as a attribute or class variable. The most straight forward method is something like this:
class MailerBase < ActionMailer::Base
before_action :check_if_users_can_receive_mailers
attr_accessor :user
def initialize(user)
#user = user
end
private
def check_if_users_can_receive_mailers
user.send_mailers?
end
end
class SomeMailerClass < MailerBase
end
In Rails only your controller and views are request aware. Mailers and models and other classes in your application are not and they cannot get the current user since they can't access the session nor the method current_user which is a helper method mixed into your controller (and the view context).
If your mailers need to know about the current user the most logical approach is to pass that information into the mailer:
class UserMailer < ApplicationMailer
def intialize(user)
#user = user
end
end
However a mailer should only have one job - to send emails and it shouldn't be questioning if it should do the job or not. Determining if you should send an email to the user should be done elsewhere. You can place this logic in the controller or even better in a service object:
# app/notifiers/user_notifier.rb
class UserNotifier
def initialize(user, event:)
#user = user
#event = event
end
def notify
if #user.wants_email?
spam_user!
end
send_in_app_notification
end
def self.notify(user, event:)
new(user, event:)
end
private
def spam_user!
# ...
end
def send_in_app_notification
# ...
end
end
class ThingsController
def create
#thing = Thing.new
if #thing.save
UserNotifier.notify(current_user, event: :thing_created)
redirect_to #thing
else
render :new
end
end
end
There is a before_action callback method in my ActionMailer object which is responsible for setting some instance variables.
class TestMailer < ApplicationMailer
before_action :set_params
def send_test_mail
mail(to: #email, subject: subject)
end
def set_params
#account = account.email
#date = some_action(account.updated_at)
end
end
The question is How one can test these variables in a rspec test?
some thing like:
describe TestMailer do
describe '#set_params' do
described_class.with(account: account, subject: subject).send_test_mail.deliver_now
expect(#date).to eq(Date.today)
end
end
any clue would be highly appreciated.
I think that instead of testing the instance variables, it would be better to test the email body, for example:
expect(mail.body.encoded).to include(account.updated_at)
you could setup a spy inside a mock method instance_variable_set, then validate that spy
class TestMailer < ApplicationMailer
attr_accessor :day
# ...
end
describe TestMailer do
let(:freeze_today) { Time.now.utc }
it '#set_params' do
# freeze today
allow_any_instance_of(TestMailer).to receive(:some_action)
.with(account.updated_at)
.and_return(freeze_today)
# spy
#spy = nil
allow_any_instance_of(TestMailer).to receive(:day=) do |time|
#spy = time
end
described_class.with(account: account, subject: subject)
.send_test_mail
.deliver_now
expect(#spy).to eq(freeze_today)
# or just simple like this
expect_any_instance_of(TestMailer).to receive(:day=).with(freeze_today)
end
end
I'm trying to get access to some of my application_helper methods within my mailer, but nothing seems to be working from these SO posts:
View Helpers in Mailers
Access Helpers from mailer
In app/helpers/application_helper.rb I have the following:
module ApplicationHelper
def get_network_hosts
.. stuff get get a #network_hosts object
end
end
In my mailer at app/mailers/user_notifier.rb I have the following:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
add_template_helper(ApplicationHelper)
def trial_notifier(user)
get_network_hosts
#user = user
#total = user.company.subscription.per_device_charge * #network_hosts.count
if #total < 501
#message = "You'd pay #{#total}/month if you converted to full-access now!"
else
#message = "You'd pay #{#total}/month if you converted to full-access now, but we have a better deal for you!"
end
#url = edit_user_registration_url
mail(to: #user.email, subject: 'What's up?')
end
end
In my mailer I've tried all of the suggestions in the above SO posts, but I'm still getting this error:
`NameError: undefined local variable or method `get_network_hosts' for #<UserMailer:0x007fe756c67c18`>
I'm currently using Rails 4.1.7.
So what do I have to actually do to be able to use my application_helper methods within a mailer?
You can try to do this as following:
In your mailer at app/mailers/user_notifier.rb:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
helper :application
or you can try this:
helper ApplicationHelper
I have this weird thing going on in my rails4 app:
I created event.rb in the lib folder.
In there, I call a mailer:
def whatever
puts 'here'
UserMailer.welcome(user)
puts 'there'
end
which is calling
class UserMailer < ActionMailer::Base
def welcome(user)
#user = user
mail(to: #user.mailer, subject: 'Welcome to my app').deliver
end
end
The weird thing is that the method welcome is never called, while whatever is called, without raising any error (the logs are there).
But if I call UserMailer.welcome(User.first) in the console, it is sent.
What am I doing wrong? Is it that it is not possible to send an email from a module? I should move this code to a model? That would be weird.
Thanks in advance
IMO mailer should look like this:
class UserMailer < ActionMailer::Base
def welcome(user)
#user = user
mail(to: #user.mailer, subject: 'Welcome to my app') #.deliver removed
end
end
and should be invoked with this manner:
def whatever
puts 'here'
UserMailer.welcome(user).deliver_now # and added here
puts 'there'
end
I can use Devise helper methods in regular views but don't know how to use it within my Mailer. I need to determine if user is signed in to construct proper email message.
class UserMailer < ActionMailer::Base
def receipt
end
end
receipt.text.erb
<% if user_signed_in? %> #Error: undefined method `user_signed_in?' for #<#<Class:0x35695fc>
Secret link
<% end %>
Actually, you can't and most of all, you shouldn't use this kind of Devise helper on your mailer.
Why ? Well, if you search Devise's code base for the user_signed_in? helper, you will find it in Devise::Controllers::Helpers module, as you can see here. This means that it is supposed to be used in a controller context, as it uses method such as request and warden that is only available on controllers.
If you must make any decision in your mail view based on whether a user is signed in or not, I would recommend you to pass this information from your controller to your mailer:
Your controller:
class UserController < ApplicationController
def your_action
UserMailer.receipt(user_signed_in?).deliver
#....
end
end
Your mailer:
class UserMailer < ActionMailer::Base
def receipt(signed_in)
#signed_in = singed_in
#....
end
end
Your mailer view:
<% if #signed_in %>
Secret link
<% end %>
I hope it helps !
You can pass it over from helper. Something like this
class UserMailer < ActionMailer::Base
def receipt
#is_signed = user_signed_in?
end
end
and
<% if #is_signed %>
Secret link
<% end %>