Last active
September 8, 2015 16:44
-
-
Save betesh/ef3fb6e87cc7fb37b94c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dequeueing ActiveJob tasks in the context of your Rails application? | |
# An alternative is to run a standalone ActiveJob process. | |
# But making it dequeue and send devise emails isn't so simple. | |
# Devise is designed to be integrated into a Rails application, not a standalone ActiveJob process. | |
# Before we address configuring the standalone ActiveJob process, | |
# there's some code that needs to go into your Rails app. | |
# Put this in each model in your Rails application: | |
# | |
# def send_devise_notification(notification, *args) | |
# devise_mailer.send(notification, self, *args).deliver_later | |
# end | |
# | |
# Now, here's what it takes to get the standalone ActiveJob process working: | |
# 1. Configure your Queue Adapter. This examples uses Delayed Job. | |
# See ActiveJob documentation for alternatives. | |
# Make sure you pick a Queue Adapter that queues jobs using the database. | |
# Sidekiq, for instance, will not work for this. | |
require "delayed_job_active_record" | |
class Delayed::Worker | |
self.sleep_delay = 20 | |
self.max_attempts = 11 | |
end | |
# 2. Configure devise | |
# devise.rb requires a few files that we don't want to load, such as Rails. | |
# So we need to preempt it on the load path. | |
# See further instructions in rails.rb below. | |
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) | |
# There are a few methods that don't make sense outside of the Rails application, | |
# making it difficult to load devise. | |
# So we override them with empty implementation. | |
require "devise" | |
module Devise | |
class << self | |
def configure_warden! | |
end | |
end | |
class Mapping | |
def default_failure_app(arg) | |
end | |
end | |
end | |
Devise.setup do |config| | |
require 'devise/orm/active_record' | |
# This means that all the url helper methods will be delegated to a method called `routes` | |
# See #7 below, where we define this method | |
config.router_name = :routes | |
end | |
# Since we're not using a Railtie, devise's app folder won't be added to the LOAD_PATH. | |
# So we need to used some Bundler internals to get there. | |
require "action_mailer" | |
require File.join( | |
Bundler.load.specs["devise"].first.full_gem_path, | |
"app", | |
"mailers", | |
"devise", | |
"mailer" | |
) | |
# 3. Configure ActionMailer | |
class ActionMailer::Base | |
default | |
from: "[email protected]", | |
protocol: "https", | |
host: "mydomain.com", | |
smtp_settings: { address: "localhost", port: 25, enable_starttls_auto: false } | |
self.raise_delivery_errors = true | |
self.view_paths << File.join(Bundler.load.specs["devise"].first.full_gem_path, "app", "views") | |
end | |
# 4. Configure ActiveRecord to support ActiveJob serialization | |
require "global_id" | |
GlobalID.app = "MyApp" | |
class ActiveRecord::Base | |
include GlobalID::Identification | |
end | |
# 5. Define your routes | |
ROUTES = begin | |
require "action_dispatch/routing" | |
require "devise/rails/routes" | |
ActionDispatch::Routing::Mapper.send(:define_method, :raise_no_secret_key) {} | |
route_set = ::ActionDispatch::Routing::RouteSet.new | |
route_set.draw do | |
default_url_options( | |
host: "#{ActionMailer::Base.default_params[:protocol]}://#{ActionMailer::Base.default_params[:host]}" | |
) | |
devise_for :users, skip: :sessions | |
end | |
route_set.url_helpers(false) | |
end | |
# 6. Load all your models: | |
class User < ActiveRecord::Base | |
devise :confirmable, :recoverable, :database_authenticatable, :lockable | |
end | |
# 7. Make all the routes into helper methods in your mailer views: | |
require "action_view" | |
class ActionView::Base | |
include Devise::Controllers::UrlHelpers | |
def routes | |
@routes ||= ROUTES | |
end | |
end | |
# 8. Load ActiveJob | |
require "active_job" | |
# 9. Configure the loggers: | |
ActiveJob::Base.logger = | |
ActionMailer::Base.logger = | |
ActiveRecord::Base.logger = | |
Delayed::Worker.logger = | |
::Logger.new("/path/to/log.log") | |
# 10. Connect to the database | |
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']) | |
# 11. Make ActionView use the correct content type by default | |
# This is necessary so that we automatically set the Content Type based on the template's file extension. | |
# See https://github.com/rails/rails/issues/11157 | |
autoload :Mime, 'action_dispatch/http/mime_type' | |
ActionView::Base.default_formats ||= Mime::SET.symbols | |
ActionView::Template::Types.delegate_to Mime | |
# 12. Run your delayed job worker | |
Delayed::Worker.new(queues: [:mailers]).start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file prevents devise from loading another file with the same name by pre-empting it on the load path | |
# Similar files must be created called responders.rb and devise/rails.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment