<commit_before>class CrowdCampaignsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]

  def index
    head :ok and return if browser.bot? && !request.format.js?
    @crowd_campaigns = collection_scope.in(current_region).includes(:user)
    @crowd_campaigns = filter_collection(@crowd_campaigns)
    @crowd_campaigns = @crowd_campaigns.by_currentness.page(params[:page]).per(params[:per_page] || 15)
  end

  def show
    @crowd_campaign = CrowdCampaign.in(current_region).find(params[:id])
    @comments = @crowd_campaign.comments.includes(:user, :images).order(created_at: :desc)
    @preview = params[:preview] == 'true' ?  true : false
    show_status_message?
  end

  def new
    @crowd_campaign = CrowdCampaign.new(current_user_address_params)
  end

  def create
    @crowd_campaign = CrowdCampaign.new(crowd_campaign_params)
    @crowd_campaign.user_id = current_user.admin? ? params[:user_id] : current_user.id
    @crowd_campaign.region_id = current_region.id
    @crowd_campaign.clear_address if params[:no_address] == 'true'

    if @crowd_campaign.save
      #CrowdCampaignMailer.crowd_campaign_published(@crowd_campaign).deliver_later
      #ActionProcessor.track(@crowd_campaign, :create)
      redirect_to edit_description_crowd_campaign_path(@crowd_campaign)
    else
      render :new
    end
  end

  def edit
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_description
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_finance
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_rewards
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_media
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_finish
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_next_steps
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def update
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    @crowd_campaign.clear_address if params[:no_address] == 'true'

    if @crowd_campaign.update(crowd_campaign_params)
      if params[:page]
        redirect_to params[:page]
      elsif params[:submit_for_approve] && !@crowd_campaign.ready_for_approve?
        redirect_back fallback_location: edit_crowd_campaign_path(@crowd_campaign), notice: 'Deine Crowdfundingkampagne konnte noch nicht zur Freigabe eingereicht werden. Bitte fülle alle Felder aus, bis alle Schritte mit einem Haken gekennzeichnet sind.'
      elsif params[:submit_for_approve] && @crowd_campaign.ready_for_approve?
        @crowd_campaign.status = :pending
        @crowd_campaign.save
        redirect_to edit_next_steps_crowd_campaign_path(@crowd_campaign)
      else
        redirect_back fallback_location: edit_crowd_campaign_path(@crowd_campaign), notice: "Deine Änderungen wurden gespeichert."
      end
    else
      render :edit
    end

  end

  def destroy
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    @crowd_campaign.destroy
    redirect_to crowd_campaigns_user_path
  end

  private

  def form_status_message?
    flash.now[:alert] = "Deine Kampagne wird gerade überprüft. Du erhältst eine Nachricht sobald sie genehmnigt wurde." if @crowd_campaign.pending?
    flash.now[:alert] = "Deine Kampagne wurde genehmigt und läuft ab #{@crowd_campaign.runtime} | #{ActionController::Base.helpers.link_to('Kampagne ansehen', crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.approved?
  end

  def show_status_message?
    if @crowd_campaign.user == current_user
      flash.now[:alert] = "Deine Kampagne ist noch im Bearbeitungsmodus. | #{ActionController::Base.helpers.link_to('Kampagne bearbeiten', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.draft?
      flash.now[:alert] = "Deine Kampagne wird überprüft. Du erhältst eine Nachricht sobald sie genehmnigt wurde. | #{ActionController::Base.helpers.link_to('Zum Kampagnen-Setup', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.pending?
      flash.now[:alert] = "Deine Kampagne wurde genehmigt und läuft ab #{@crowd_campaign.runtime}. | #{ActionController::Base.helpers.link_to('Zum Kampagnen-Setup', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.approved?
    else
      flash.now[:alert] = "Kampagnen Voransicht - Diese Kampagne ist noch in Bearbeitung." if @crowd_campaign.draft? || @crowd_campaign.pending?
      flash.now[:alert] = "Kampagnen Voransicht - Diese Kampagne läuft ab #{@crowd_campaign.runtime}." if @crowd_campaign.approved?
    end
  end

  def collection_scope
    if params[:user_id].present?
      CrowdCampaign.approved.where(user_id: params[:user_id])
    else
      CrowdCampaign.approved
    end
  end

  def filter_collection(collection)
    graetzl_ids = params.dig(:filter, :graetzl_ids)

    if params[:category_id].present?
      collection = collection.where(crowd_category: params[:category_id])
    end

    if params[:favorites].present? && current_user<commit_msg>wording<commit_after>class CrowdCampaignsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]

  def index
    head :ok and return if browser.bot? && !request.format.js?
    @crowd_campaigns = collection_scope.in(current_region).includes(:user)
    @crowd_campaigns = filter_collection(@crowd_campaigns)
    @crowd_campaigns = @crowd_campaigns.by_currentness.page(params[:page]).per(params[:per_page] || 15)
  end

  def show
    @crowd_campaign = CrowdCampaign.in(current_region).find(params[:id])
    @comments = @crowd_campaign.comments.includes(:user, :images).order(created_at: :desc)
    @preview = params[:preview] == 'true' ?  true : false
    show_status_message?
  end

  def new
    @crowd_campaign = CrowdCampaign.new(current_user_address_params)
  end

  def create
    @crowd_campaign = CrowdCampaign.new(crowd_campaign_params)
    @crowd_campaign.user_id = current_user.admin? ? params[:user_id] : current_user.id
    @crowd_campaign.region_id = current_region.id
    @crowd_campaign.clear_address if params[:no_address] == 'true'

    if @crowd_campaign.save
      #CrowdCampaignMailer.crowd_campaign_published(@crowd_campaign).deliver_later
      #ActionProcessor.track(@crowd_campaign, :create)
      redirect_to edit_description_crowd_campaign_path(@crowd_campaign)
    else
      render :new
    end
  end

  def edit
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_description
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_finance
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_rewards
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_media
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_finish
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def edit_next_steps
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    form_status_message?
  end

  def update
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    @crowd_campaign.clear_address if params[:no_address] == 'true'

    if @crowd_campaign.update(crowd_campaign_params)
      if params[:page]
        redirect_to params[:page]
      elsif params[:submit_for_approve] && !@crowd_campaign.ready_for_approve?
        redirect_back fallback_location: edit_crowd_campaign_path(@crowd_campaign), notice: 'Deine Crowdfundingkampagne konnte noch nicht zur Freigabe eingereicht werden. Bitte fülle alle Felder aus, bis alle Schritte mit einem Haken gekennzeichnet sind.'
      elsif params[:submit_for_approve] && @crowd_campaign.ready_for_approve?
        @crowd_campaign.status = :pending
        @crowd_campaign.save
        redirect_to edit_next_steps_crowd_campaign_path(@crowd_campaign)
      else
        redirect_back fallback_location: edit_crowd_campaign_path(@crowd_campaign), notice: "Deine Änderungen wurden gespeichert."
      end
    else
      render :edit
    end

  end

  def destroy
    @crowd_campaign = current_user.crowd_campaigns.find(params[:id])
    @crowd_campaign.destroy
    redirect_to crowd_campaigns_user_path
  end

  private

  def form_status_message?
    flash.now[:alert] = "Deine Kampagne wird gerade überprüft. Du erhältst eine Nachricht sobald sie genehmnigt wurde. | #{ActionController::Base.helpers.link_to('Kampagne ansehen', crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.pending?
    flash.now[:alert] = "Deine Kampagne wurde genehmigt und läuft ab #{@crowd_campaign.runtime} | #{ActionController::Base.helpers.link_to('Kampagne ansehen', crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.approved?
  end

  def show_status_message?
    if @crowd_campaign.user == current_user
      flash.now[:alert] = "Deine Kampagne ist noch im Bearbeitungsmodus. | #{ActionController::Base.helpers.link_to('Kampagne bearbeiten', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.draft?
      flash.now[:alert] = "Deine Kampagne wird überprüft. Du erhältst eine Nachricht sobald sie genehmnigt wurde. | #{ActionController::Base.helpers.link_to('Zum Kampagnen-Setup', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.pending?
      flash.now[:alert] = "Deine Kampagne wurde genehmigt und läuft ab #{@crowd_campaign.runtime}. | #{ActionController::Base.helpers.link_to('Zum Kampagnen-Setup', edit_crowd_campaign_path(@crowd_campaign))}" if @crowd_campaign.approved?
    else
      flash.now[:alert] = "Kampagnen Voransicht - Diese Kampagne ist noch in Bearbeitung." if @crowd_campaign.draft? || @crowd_campaign.pending?
      flash.now[:alert] = "Kampagnen Voransicht - Diese Kampagne läuft ab #{@crowd_campaign.runtime}." if @crowd_campaign.approved?
    end
  end

  def collection_scope
    if params[:user_id].present?
      CrowdCampaign.approved.where(user_id: params[:user_id])
    else
      CrowdCampaign.approved
    end
  end

  def filter_collection(collection)
    graetzl_ids = params.dig(:filter, :graetzl_ids)

    if params[:category_id].present?
      collection = collection.where(crowd_category: params[:category_id])
    end

    if params[:favorites].present? && current_user
{'fraction_non_alphanumeric': 0.09779768570362075, 'fraction_numerical': 0.0003732736095558044, 'mean_word_length': 4.814975583288117, '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': '2226002', 'n_tokens_mistral': 3819, 'n_tokens_neox': 3506, 'n_words': 640}