<commit_before>class DonationsController < ApplicationController
  # We load the resources in before_filters so that they are not re-loaded
  # by Cancan, which won't use the correct methods.
  #before_filter :simple_load, only: [:track, :remove_item, :edit, :update, :destroy]
  #before_filter :eager_load_single, only: [:show]
  #before_filter :load_collection, only: [:index]
  #before_filter :load_new, only: [:new]

  # Cancan authorization
  #load_and_authorize_resource

  skip_before_action :verify_authenticity_token, only: [:scale_intake, :scale]
  skip_before_action :authenticate_user!, only: [:scale_intake, :scale]
  skip_before_action :authorize_user, only: [:scale_intake, :scale]

#  def add_item
#    @donation = current_organization.donations.find(params[:id])
#    if (donation_item_params.has_key?(:barcode_id))
#      donation_item_params[:item_id] = BarcodeItem.find!(donation_item_params[:barcode_id]).item_id
#    end
#    @donation.track(donation_item_params[:item_id], donation_item_params[:quantity])
#  end

#  def remove_item
#    @donation = current_organization.donations.find(params[:id])
#    @donation.remove(donation_item_params[:item_id])
#  end

  def index
    @donations = current_organization.donations
                                      .includes(:line_items, :storage_location, :dropoff_location, :diaper_drive_participant)
                                      .order(created_at: :desc)
                                      .filter(filter_params)
    # Are these going to be inefficient with large datasets?
    # Using the @donations allows drilling down instead of always starting with the total dataset
    @storage_locations = @donations.collect { |d| d.storage_location }.compact.uniq
    @selected_storage_location = filter_params[:at_storage_location]
    @sources = @donations.collect { |d| d.source }.uniq
    @selected_source = filter_params[:by_source]
    @dropoff_locations = @donations.collect { |d| d.dropoff_location }.compact.uniq
    @selected_dropoff_location = filter_params[:from_dropoff_location]
    @diaper_drives = @donations.collect { |d| next unless d.source ==  Donation::SOURCES[:diaper_drive]; d.diaper_drive_participant }.compact.uniq
    @selected_diaper_drive = filter_params[:by_diaper_drive_participant]
  end

  def scale
    @donation = Donation.new(issued_at: Date.today)
    @donation.line_items.build
    load_form_collections
  end

  def scale_intake
    @donation = Donation.create( organization: current_organization,
                                 source: "Misc. Donation",
                                 storage_location_id: current_organization.intake_location,
                                 issued_at: Date.today,
                                 line_items_attributes:{"0"=>{"item_id"=>params["diaper_type"],
                                                              "quantity"=>params["number_of_diapers"],
                                                              "_destroy"=>"false"}}
      )
    render status: 200, json: @donation.to_json
  end

  def create
    @donation = Donation.new(donation_params.merge(organization: current_organization))
    if (@donation.save)
      @donation.storage_location.intake! @donation
      redirect_to donations_path
    else
      load_form_collections
      @donation.line_items.build if @donation.line_items.count == 0
      flash[:alert] = "There was an error starting this donation, try again?"
      Rails.logger.info "ERROR: #{@donation.errors}"<commit_msg>After review I saw the scale function was not using the intake! method so stuff was not showing up in the the storage locations.<commit_after>class DonationsController < ApplicationController
  # We load the resources in before_filters so that they are not re-loaded
  # by Cancan, which won't use the correct methods.
  #before_filter :simple_load, only: [:track, :remove_item, :edit, :update, :destroy]
  #before_filter :eager_load_single, only: [:show]
  #before_filter :load_collection, only: [:index]
  #before_filter :load_new, only: [:new]

  # Cancan authorization
  #load_and_authorize_resource

  skip_before_action :verify_authenticity_token, only: [:scale_intake, :scale]
  skip_before_action :authenticate_user!, only: [:scale_intake, :scale]
  skip_before_action :authorize_user, only: [:scale_intake, :scale]

#  def add_item
#    @donation = current_organization.donations.find(params[:id])
#    if (donation_item_params.has_key?(:barcode_id))
#      donation_item_params[:item_id] = BarcodeItem.find!(donation_item_params[:barcode_id]).item_id
#    end
#    @donation.track(donation_item_params[:item_id], donation_item_params[:quantity])
#  end

#  def remove_item
#    @donation = current_organization.donations.find(params[:id])
#    @donation.remove(donation_item_params[:item_id])
#  end

  def index
    @donations = current_organization.donations
                                      .includes(:line_items, :storage_location, :dropoff_location, :diaper_drive_participant)
                                      .order(created_at: :desc)
                                      .filter(filter_params)
    # Are these going to be inefficient with large datasets?
    # Using the @donations allows drilling down instead of always starting with the total dataset
    @storage_locations = @donations.collect { |d| d.storage_location }.compact.uniq
    @selected_storage_location = filter_params[:at_storage_location]
    @sources = @donations.collect { |d| d.source }.uniq
    @selected_source = filter_params[:by_source]
    @dropoff_locations = @donations.collect { |d| d.dropoff_location }.compact.uniq
    @selected_dropoff_location = filter_params[:from_dropoff_location]
    @diaper_drives = @donations.collect { |d| next unless d.source ==  Donation::SOURCES[:diaper_drive]; d.diaper_drive_participant }.compact.uniq
    @selected_diaper_drive = filter_params[:by_diaper_drive_participant]
  end

  def scale
    @donation = Donation.new(issued_at: Date.today)
    @donation.line_items.build
    load_form_collections
  end

  def scale_intake
    @donation = Donation.create( organization: current_organization,
                                 source: "Misc. Donation",
                                 storage_location_id: current_organization.intake_location,
                                 issued_at: Date.today,
                                 line_items_attributes:{"0"=>{"item_id"=>params["diaper_type"],
                                                              "quantity"=>params["number_of_diapers"],
                                                              "_destroy"=>"false"}}
      )
    @donation.storage_location.intake! @donation
    render status: 200, json: @donation.to_json
  end

  def create
    @donation = Donation.new(donation_params.merge(organization: current_organization))
    if (@donation.save)
      @donation.storage_location.intake! @donation
      redirect_to donations_path
    else
      load_form_collections
      @donation.line_items.build if @donation.line_items.count == 0
      flash[:alert] = "There was an error starting this donation, try again?"
      Rails.logger.info "ERROR: #{@donation.errors}"
{'fraction_non_alphanumeric': 0.09705637322965843, 'fraction_numerical': 0.0013885031935573452, 'mean_word_length': 3.1230681167716083, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3298102', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2098, 'n_words': 436}