<commit_before>class Admin::EmailsController < Admin::BaseController
  include ActionView::Helpers::UrlHelper
  include ApplicationHelper
  
  def create
    @email = Email.new(email_params)
    if @email.save!
      flash[:notice] = 'Email draft saved.'
      redirect_to admin_emails_path
    end
  end
  
  def edit
    @email = Email.friendly.find(params[:id])
  end
  
  def new
    @email = Email.new
  end
  
  def index
    @emails = Email.all.order(:sent_at)
  end
  
  def send_test
    @email = Email.friendly.find(params[:id])
    @user = User.find(1)
    body = ERB.new(@email.body).result(binding).html_safe
    EmailsMailer.announcement(@user, @email, body).deliver_now
    flash[:notice] = 'Email sent to ' + @user.email
    redirect_to admin_emails_path
  end
  
  def send_to_list
    @email = Email.friendly.find(params[:id])
    if @email.sent == true
      flash[:notice] = 'Email already sent'
      redirect_to admin_emails_path
    else
      mailing_list = User.where(opt_in: true).where("email not like 'change@me%'")
      if Rails.env.development?
        mailing_list[3..7].each do |recipient|
          @user = recipient
          body = ERB.new(@email.body).result(binding).html_safe
          EmailsMailer.announcement(recipient, @email, body).deliver_later
        end
      else
        mailing_list.each do |recipient|
          @user = recipient
          body = ERB.new(@email.body).result(binding).html_safe
          EmailsMailer.announcement(recipient, @email, body).deliver_later
        end
      end
      flash[:notice] = 'Sending emails to ' + mailing_list.size.to_s + " users"
      redirect_to admin_emails_path
    end
  end
  
  def update
    @email = Email.friendly.find(params[:id])
    if @email.update_attributes(email_params)
      flash[:notice] = 'Email draft updated.'
      redirect_to admin_emails_path
    end
  end
  
  private
  
  def email_params<commit_msg>mark email as sent after sending<commit_after>class Admin::EmailsController < Admin::BaseController
  include ActionView::Helpers::UrlHelper
  include ApplicationHelper
  
  def create
    @email = Email.new(email_params)
    if @email.save!
      flash[:notice] = 'Email draft saved.'
      redirect_to admin_emails_path
    end
  end
  
  def edit
    @email = Email.friendly.find(params[:id])
  end
  
  def new
    @email = Email.new
  end
  
  def index
    @emails = Email.all.order(:sent_at)
  end
  
  def send_test
    @email = Email.friendly.find(params[:id])
    @user = User.find(1)
    body = ERB.new(@email.body).result(binding).html_safe
    EmailsMailer.announcement(@user, @email, body).deliver_now
    flash[:notice] = 'Email sent to ' + @user.email
    redirect_to admin_emails_path
  end
  
  def send_to_list
    @email = Email.friendly.find(params[:id])
    if @email.sent == true
      flash[:notice] = 'Email already sent'
      redirect_to admin_emails_path
    else
      mailing_list = User.where(opt_in: true).where("email not like 'change@me%'")
      if Rails.env.development?
        mailing_list[3..7].each do |recipient|
          @user = recipient
          body = ERB.new(@email.body).result(binding).html_safe
          EmailsMailer.announcement(recipient, @email, body).deliver_later
        end
      else
        mailing_list.each do |recipient|
          @user = recipient
          body = ERB.new(@email.body).result(binding).html_safe
          EmailsMailer.announcement(recipient, @email, body).deliver_later
        end
        @email.sent = true
        @email.save
      end
      flash[:notice] = 'Sending emails to ' + mailing_list.size.to_s + " users"
      redirect_to admin_emails_path
    end
  end
  
  def update
    @email = Email.friendly.find(params[:id])
    if @email.update_attributes(email_params)
      flash[:notice] = 'Email draft updated.'
      redirect_to admin_emails_path
    end
  end
  
  private
  
  def email_params
{'fraction_non_alphanumeric': 0.10506134969325154, 'fraction_numerical': 0.0015337423312883436, 'mean_word_length': 2.777027027027027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7379980', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1265, 'n_words': 263}