<commit_before># frozen_string_literal: true

module Meals
  class MealsController < ApplicationController
    include Lensable
    include MealShowable
    include Destructible

    decorates_assigned :meals, :meal_summary, :report, :user

    # decorates_assigned :report collides with action method
    helper_method :meals_report

    before_action :create_worker_change_notifier, only: :update
    before_action -> { nav_context(:meals, :meals) }, except: %i[jobs report]

    def index
      prepare_lenses(:search, :"meals/time", :community)

      authorize(sample_meal)
      load_meals(:index)
      load_communities_in_cluster
    end

    def jobs
      authorize(sample_meal)
      nav_context(:meals, :jobs)
      prepare_lenses(:"meals/job_user", :"meals/time")
      @user = lenses[:user].user
      load_meals(:jobs)
      load_communities_in_cluster
    end

    def show
      @meal = Meal.find(params[:id])
      authorize(@meal)

      # Don't want the signup form to get cached
      set_no_cache unless @meal.in_past?

      flash.now[:error] = I18n.t("meals.cancelled_notice") if @meal.cancelled?

      @signup = Signup.for(current_user, @meal)
      @signup_parts = @signup.parts
      @expand_signup_form = params[:signup].present? || @signup.persisted?
      prep_show_meal_vars
    end

    def report
      authorize(sample_meal, :report?)
      @community = current_community
      nav_context(:meals, :report)
      prepare_lenses(*report_lenses)
      @meals_report = Report.new(@community, range: lenses[:dates].range)
      @communities = Community.by_name_with_first(@community).to_a
    end

    def new
      @meal = init_meal
      authorize(@meal)
      prep_form_vars
    end

    def edit
      @meal = Meal.find(params[:id])
      authorize(@meal)
      ensure_head_cook_assignment_present
      prep_form_vars
    end

    def create
      @meal = Meal.new(
        community_id: current_user.community_id,
        community_ids: [current_user.community_id],
        creator: current_user
      )
      @meal.assign_attributes(meal_params)
      @meal.build_reservations
      authorize(@meal)
      if @meal.save
        flash[:success] = "Meal created successfully."
        redirect_to(meals_path)
      else
        prep_form_vars
        render(:new)
      end
    end

    def update
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.assign_attributes(meal_params)
      @meal.build_reservations
      if @meal.save
        flash[:success] = "Meal updated successfully."
        @worker_change_notifier&.check_and_send!
        redirect_to(meals_path)
      else
        prep_form_vars
        render(:edit)
      end
    end

    def close
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.close!
      flash[:success] = "Meal closed successfully."
      redirect_to(meals_path)
    end

    def reopen
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.reopen!
      flash[:success] = "Meal reopened successfully."
      redirect_to(meals_path)
    end

    def summary
      @meal = Meal.find(params[:id]).decorate
      @meal_summary = Summary.new(@meal)
      @portion_count_builder = PortionCountBuilder.new(@meal)
      authorize(@meal)
      load_signups
      @cost_calculator = CostCalculator.build(@meal)
      return unless @meal.open? && current_user == @meal.head_cook
      flash.now[:alert] = "Note: This meal is not yet closed and people can still sign up for it. "\
        "You should close the meal using the link below before printing this summary."
    end<commit_msg>765: Remove reference to close button from meal summary page notice<commit_after># frozen_string_literal: true

module Meals
  class MealsController < ApplicationController
    include Lensable
    include MealShowable
    include Destructible

    decorates_assigned :meals, :meal_summary, :report, :user

    # decorates_assigned :report collides with action method
    helper_method :meals_report

    before_action :create_worker_change_notifier, only: :update
    before_action -> { nav_context(:meals, :meals) }, except: %i[jobs report]

    def index
      prepare_lenses(:search, :"meals/time", :community)

      authorize(sample_meal)
      load_meals(:index)
      load_communities_in_cluster
    end

    def jobs
      authorize(sample_meal)
      nav_context(:meals, :jobs)
      prepare_lenses(:"meals/job_user", :"meals/time")
      @user = lenses[:user].user
      load_meals(:jobs)
      load_communities_in_cluster
    end

    def show
      @meal = Meal.find(params[:id])
      authorize(@meal)

      # Don't want the signup form to get cached
      set_no_cache unless @meal.in_past?

      flash.now[:error] = I18n.t("meals.cancelled_notice") if @meal.cancelled?

      @signup = Signup.for(current_user, @meal)
      @signup_parts = @signup.parts
      @expand_signup_form = params[:signup].present? || @signup.persisted?
      prep_show_meal_vars
    end

    def report
      authorize(sample_meal, :report?)
      @community = current_community
      nav_context(:meals, :report)
      prepare_lenses(*report_lenses)
      @meals_report = Report.new(@community, range: lenses[:dates].range)
      @communities = Community.by_name_with_first(@community).to_a
    end

    def new
      @meal = init_meal
      authorize(@meal)
      prep_form_vars
    end

    def edit
      @meal = Meal.find(params[:id])
      authorize(@meal)
      ensure_head_cook_assignment_present
      prep_form_vars
    end

    def create
      @meal = Meal.new(
        community_id: current_user.community_id,
        community_ids: [current_user.community_id],
        creator: current_user
      )
      @meal.assign_attributes(meal_params)
      @meal.build_reservations
      authorize(@meal)
      if @meal.save
        flash[:success] = "Meal created successfully."
        redirect_to(meals_path)
      else
        prep_form_vars
        render(:new)
      end
    end

    def update
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.assign_attributes(meal_params)
      @meal.build_reservations
      if @meal.save
        flash[:success] = "Meal updated successfully."
        @worker_change_notifier&.check_and_send!
        redirect_to(meals_path)
      else
        prep_form_vars
        render(:edit)
      end
    end

    def close
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.close!
      flash[:success] = "Meal closed successfully."
      redirect_to(meals_path)
    end

    def reopen
      @meal = Meal.find(params[:id])
      authorize(@meal)
      @meal.reopen!
      flash[:success] = "Meal reopened successfully."
      redirect_to(meals_path)
    end

    def summary
      @meal = Meal.find(params[:id]).decorate
      @meal_summary = Summary.new(@meal)
      @portion_count_builder = PortionCountBuilder.new(@meal)
      authorize(@meal)
      load_signups
      @cost_calculator = CostCalculator.build(@meal)
      return unless @meal.open? && current_user == @meal.head_cook
      flash.now[:alert] = "Note: This meal is not yet closed and people can still sign up for it. "\
        "You should close the meal before printing this summary."
    end
{'fraction_non_alphanumeric': 0.09650697224906807, 'fraction_numerical': 0.0009664503658704957, 'mean_word_length': 2.7827676240208876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3004349', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2245, 'n_words': 475}