<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Devise on MoskitoHero</title><link>/tags/devise/</link><description>Recent content in Devise on MoskitoHero</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 26 Jan 2020 00:00:00 +0000</lastBuildDate><atom:link href="/tags/devise/index.xml" rel="self" type="application/rss+xml"/><item><title>Prevent Devise from sending confirmation emails</title><link>/post/2020-01-26-prevent-devise-from-sending-confirmation-emails/</link><pubDate>Sun, 26 Jan 2020 00:00:00 +0000</pubDate><guid>/post/2020-01-26-prevent-devise-from-sending-confirmation-emails/</guid><description>When you need to import users into a new Rails app using Devise, you may want to skip sending confirmation emails. Here’s how to do it by overriding an instance method in the User model.</description><content:encoded><![CDATA[<div class="imageblock">
<div class="content">
<img src="/assets/img/message-in-a-bottle.jpg" alt="Image par Antonios Ntoumas de Pixabay"/>
</div>
</div>
<div class="paragraph">
<p>I am about to release a huge rails project I have been working on for quite a
while. This project is meant to replace an old app that manages a database of
customers with emails. The new rails project uses Devise for authentication.</p>
</div>
<div class="paragraph">
<p>Before we push the final update to the production server, we need to find a way to import the users without sending confirmation emails, but still keep them unconfirmed until they actually login to the new system and provide a password. New registrations should also still be confirmable.</p>
</div>
<div class="paragraph">
<p>So I had to find a way to override an instance method. This is easy to do with a
simple module:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># Module to be included in a User instance whenever we don&#39;t want to send an email</span>
<span class="c1"># Useful when importing users or for testing</span>
<span class="k">module</span> <span class="nn">SkipConfirmationEmail</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">included</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
    <span class="n">base</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:include</span><span class="p">,</span> <span class="no">InstanceMethods</span><span class="p">)</span>

    <span class="n">base</span><span class="p">.</span><span class="nf">class_eval</span> <span class="k">do</span>
      <span class="kp">alias_method</span> <span class="ss">:send_confirmation_instructions</span><span class="p">,</span> <span class="ss">:return_false</span>
    <span class="k">end</span>
  <span class="k">end</span>

  <span class="c1"># Instance methods</span>
  <span class="k">module</span> <span class="nn">InstanceMethods</span>
    <span class="k">def</span> <span class="nf">return_false</span>
      <span class="kp">false</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>What this basically does is alias the <code>send_confirmation_instructions</code> method to
a new <code>return_false</code> instance method we can inject into our model like this :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">new</span>
<span class="n">user</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:include</span><span class="p">,</span> <span class="no">SkipConfirmationEmail</span><span class="p">)</span>

<span class="n">user</span><span class="p">.</span><span class="nf">send_confirmation_instructions</span>
<span class="c1">#&gt; false</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Therefore, my users will still have to confirm their account at first login on
the new system, but won’t be notified when I import the database a few days
before launch.</p>
</div>
<div class="paragraph">
<p>This can easily be adapted to serveral use cases, whenever you need to override
an instance method in an exisiting Ruby class, as seen <a href="https://gist.github.com/bluefuton/5851093">in this GitHub gist</a>.</p>
</div>
]]></content:encoded></item></channel></rss>