Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll address the issue of checking if a report has been sent later, but first I'll cover the question of how to call the controller action from ActiveAdmin.</p> <p>While you can call <code>ReportsController#send_status</code> by creating an <code>ActionController::Base::ReportsController</code> and then calling the desired method, e.g.</p> <pre><code>ActionController::Base::ReportsController.new.send_status </code></pre> <p>this isn't a good idea. You probably should refactor this to address a couple potential issues.</p> <p><code>app/controllers/reports_controller.rb</code>:</p> <pre><code>class ReportsController &lt; ApplicationController ... # rest of controller methods def send_status if current_user # or whatever your conditional is ReportMailer.status_email(current_user).deliver response = :ok else response = :bad_request end head response end end </code></pre> <p><code>app/models/user.rb</code>:</p> <pre><code>class User &lt; ActiveRecord::Base ... # rest of user model def reports_for_date(date) reports.for_date(date) end end </code></pre> <p><code>app/mailers/reports_mailer.rb</code></p> <pre><code>class ReportsMailer &lt; ActionMailer::Base ... # rest of mailer def status_email(user) @user = user @date = Date.today @reports = @user.reports_for_date(@date) ... # rest of method end end </code></pre> <p>This could obviously be refactored further, but provides a decent starting point.</p> <p>An important thing to consider is that this controller action is not sending the email asynchronously, so in the interest of concurrency and user experience, you should strongly consider using a queuing system. DelayedJob would be an easy implementation with the example I've provided (look into the DelayedJob RailsCast).</p> <p>As far as checking if the report has been sent, you could implement an ActionMailer Observer and register that observer:</p> <p><em>This requires that the <code>User</code> model have a BOOLEAN column <code>status_sent</code> and that users have unique email address.</em></p> <p><code>lib/status_sent_mail_observer.rb</code>:</p> <pre><code>class StatusSentMailObserver self.delivered_email(message) user = User.find_by_email(message.to) user.update_attribute(:status_sent, true) end end </code></pre> <p><code>config/intializer/setup_mail.rb</code>:</p> <pre><code>... # rest of initializer Mail.register_observer(StatusSentMailObserver) </code></pre> <p>If you are using DelayedJob (or almost any other queuing system) you could implement a callback method to be called on job completion (i.e. sending the status email) that updates a column on the user.</p> <p>If you want to track the status message for every day, you should consider creating a <code>Status</code> model that belongs to the <code>User</code>. The status model could be created every time the user sends the email, allowing you to check if the email has been sent simply by checking if a status record exists. This strategy is one I would seriously consider adopting over just a simple <code>status_sent</code> column.</p> <p><strong>tl;dr</strong> <code>ActionController::Base::ReportsController.new.send_status</code> &amp; implement an observer that updates a column on the user that tracks the status. But you really don't want to do that. Look into refactoring like I've mentioned above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload