<commit_before>class ArchivesController < ApplicationController
  before_action :find_year, :find_account, only: :index
  before_action :find_archive, only: :update

  def index
    @archives = (1..12).map do |month|
      Archive.find_or_create_by!(bank_account: @account, year: @year, month: month)
    end
  end

  def update
    #TODO: check if archive belong to user
    @archive.transition_to :generating
    CreateArchive.perform_async(@archive.id)
    redirect_to :back
  end

  private

  def find_archive
    @archive = Archive.find_by(id: params[:id])
    redirect_to archives_path if @archive.nil?
  end

  def find_year
    @year = (params[:year] || Date.today.year).to_i
  end

  def find_account
    @account = params[:account].present? ? current_user.bank_accounts.find(params[:account]) : current_user.bank_accounts.first
    redirect_to bank_accounts_path, notice: "Please, track at least one bank account" if @account.nil?
  end
end<commit_msg>Refactor ArchivesController to memoize database records<commit_after>class ArchivesController < ApplicationController
  before_action :find_account, only: :index
  before_action :find_archive, only: :update

  def index
    @archives = (1..12).map do |month|
      Archive.find_or_create_by!(bank_account: account, year: year, month: month)
    end
  end

  def update
    #TODO: check if archive belong to user
    archive.transition_to :generating
    CreateArchive.perform_async(archive.id)
    redirect_to :back
  end

private

  def find_archive
    redirect_to archives_path unless archive.present?
  end

  def find_account
    redirect_to bank_accounts_path, notice: "Please, track at least one bank account" unless account.present?
  end

  def account
    @account ||= params[:account].present? ? current_user.bank_accounts.find(params[:account]) : current_user.bank_accounts.first
  end

  def archive
    @archive ||= Archive.find_by(id: params[:id])
  end

  def year
    @year ||= (params[:year] || Date.today.year).to_i
  end
end
{'fraction_non_alphanumeric': 0.09367214748380667, 'fraction_numerical': 0.0029895366218236174, 'mean_word_length': 4.161953727506427, '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': '7233739', 'n_tokens_mistral': 643, 'n_tokens_neox': 617, 'n_words': 165}