README.md in resque_mailer-2.2.7 vs README.md in resque_mailer-2.3.0

- old
+ new

@@ -22,13 +22,15 @@ ## Usage Include Resque::Mailer in your ActionMailer subclass(es) like this: - class MyMailer < ActionMailer::Base - include Resque::Mailer - end +```ruby +class MyMailer < ActionMailer::Base + include Resque::Mailer +end +``` Now, when `MyMailer.subject_email(params).deliver` is called, an entry will be created in the job queue. Your Resque workers will be able to deliver this message for you. The queue we're using is imaginatively named `mailer`, so just make sure your workers know about it and are loading your environment: @@ -36,32 +38,37 @@ QUEUE=mailer rake environment resque:work Note that you can still have mail delivered synchronously by using the bang method variant: - MyMailer.subject_email(params).deliver! +```ruby +MyMailer.subject_email(params).deliver! +``` Oh, by the way. Don't forget that **your async mailer jobs will be processed by a separate worker**. This means that you should resist the temptation to pass database-backed objects as parameters in your mailer and instead pass record identifiers. Then, in your delivery method, you can look up the record from -the id and use it as needed. +the id and use it as needed. If you'd like, you can write your own serializer +to automate such things; see the section on serializers below. If you want to set a different default queue name for your mailer, you can change the `default_queue_name` property like so: - # config/initializers/resque_mailer.rb - Resque::Mailer.default_queue_name = 'application_specific_mailer' +```ruby +# config/initializers/resque_mailer.rb +Resque::Mailer.default_queue_name = 'application_specific_mailer' +``` This is useful when you are running more than one application using resque_mailer in a shared environment. You will need to use the new queue name when starting your workers. QUEUE=application_specific_mailer rake environment resque:work Custom handling of errors that arise when sending a message is possible by -assigning a lambda to the `error_hander` attribute. There are two supported +assigning a lambda to the `error_handler` attribute. There are two supported lambdas for backwards compatiability. The first lamba will be deprecated in a future release: ```ruby @@ -87,49 +94,65 @@ ### Resque::Mailer as a Project Default If you have a variety of mailers in your application and want all of them to use Resque::Mailer by default, you can subclass ActionMailer::Base and have your other mailers inherit from an AsyncMailer: +```ruby +# config/initializers/resque_mailer.rb +class AsyncMailer < ActionMailer::Base + include Resque::Mailer +end - # config/initializers/resque_mailer.rb - class AsyncMailer < ActionMailer::Base - include Resque::Mailer - end +# app/mailers/example_mailer.rb +class ExampleMailer < AsyncMailer + def say_hello(user_id) + # ... + end +end +``` - # app/mailers/example_mailer.rb - class ExampleMailer < AsyncMailer - def say_hello(user_id) - # ... - end - end +### Writing an Argument Serializer +By default, the arguments you pass to your mailer are passed as-is to Resque. This +means you cannot pass things like database-backed objects. If you'd like to write +your own serializer to enable such things, simply write a class that implements +the class methods `self.serialize(*args)` and `self.deserialize(data)` and set +`Resque::Mailer.argument_serializer = YourSerializerClass` in your resque_mailer +initializer. + +There's also Active Record serializer which allows you to pass AR +models directly as arguments. To use it just do: +`Resque::Mailer.argument_serializer = Resque::Mailer::Serializers::ActiveRecordSerializer` + ### Using with Resque Scheduler If [resque-scheduler](https://github.com/bvandenbos/resque-scheduler) is installed, two extra methods will be available: `deliver_at` and `deliver_in`. These will enqueue mail for delivery at a specified time in the future. - # Delivers on the 25th of December, 2014 - MyMailer.reminder_email(params).deliver_at(Time.parse('2014-12-25')) +```ruby +# Delivers on the 25th of December, 2014 +MyMailer.reminder_email(params).deliver_at(Time.parse('2014-12-25')) - # Delivers in 7 days - MyMailer.reminder_email(params).deliver_in(7.days) +# Delivers in 7 days +MyMailer.reminder_email(params).deliver_in(7.days) - # Unschedule delivery - MyMailer.reminder_email(params).unschedule_delivery - +# Unschedule delivery +MyMailer.reminder_email(params).unschedule_delivery +``` ## Testing You don't want to be sending actual emails in the test environment, so you can configure the environments that should be excluded like so: +```ruby +# config/initializers/resque_mailer.rb +Resque::Mailer.excluded_environments = [:test, :cucumber] +``` - # config/initializers/resque_mailer.rb - Resque::Mailer.excluded_environments = [:test, :cucumber] - Note: Define `current_env` if using Resque::Mailer in a non-Rails project: - - Resque::Mailer.current_env = :production - +```ruby +Resque::Mailer.current_env = :production +``` ## Note on Patches / Pull Requests * Fork the project. * Make your feature addition or bug fix.