<commit_before># frozen_string_literal: true

# rubocop:disable ClassLength
class MomentsController < ApplicationController
  include CollectionPageSetupConcern
  include MomentsHelper
  include Shared

  before_action :set_moment, only: %i[show edit update destroy]

  # GET /moments
  # GET /moments.json
  def index
    if current_user
      @user_logged_in = true
      @react_moments = define_moments(current_user)
    else
      @user_logged_in = false
    end
    page_collection('@moments', 'moment')
  end

  # GET /moments/1
  # GET /moments/1.json
  def show
    show_with_comments(@moment)
  end

  # GET /moments/new
  def new
    @moment = Moment.new
    set_association_variables!
  end

  # GET /moments/1/edit
  def edit
    unless @moment.user_id == current_user.id
      redirect_to_path(moment_path(@moment))
    end

    set_association_variables!
  end

  # POST /moments
  # POST /moments.json
  def create
    @moment = Moment.new(moment_params.merge(user_id: current_user.id))
    @viewers = current_user.allies_by_status(:accepted)
    @category = Category.new
    @mood = Mood.new
    @strategy = Strategy.new
    @moment.published_at = Time.zone.now if publishing?
    shared_create(@moment)
  end

  # PATCH/PUT /moments/1
  # PATCH/PUT /moments/1.json
  # rubocop:disable MethodLength
  def update
    @viewers = current_user.allies_by_status(:accepted)
    @category = Category.new
    @mood = Mood.new
    @strategy = Strategy.new
    if publishing? && !@moment.published?
      @moment.published_at = Time.zone.now
    elsif saving_as_draft?
      @moment.published_at = nil
    end
    empty_array_for :viewers, :mood, :strategy, :category
    shared_update(@moment, moment_params)
  end
  # rubocop:enable MethodLength

  # DELETE /moments/1
  # DELETE /moments/1.json
  def destroy
    @moment.destroy
    redirect_to_path(moments_path)
  end

  private

  def set_moment
    @moment = Moment.friendly.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    redirect_to_path(moments_path)
  end

  def moment_params
    params.require(:moment).permit(
      :name, :why, :fix, :comment, :published_at, :draft,
      category: [], mood: [], viewers: [], strategy: []
    )
  end

  def set_association_variables!
    @viewers = current_user.allies_by_status(:accepted)
    @categories = Category.where(user: current_user).order(created_at: :desc)
    @category = Category.new
    @moods = Mood.where(user: current_user).order(created_at: :desc)
    @mood = Mood.new
    @strategies = associated_strategies
    @strategy = Strategy.new
  end

  def associated_strategies
    # current_user's strategies and all viewable strategies from allies
    strategy_ids = current_user.strategies.pluck(:id)
    @viewers.each do |ally|
      ally.strategies.each do |strategy|
        strategy_ids << strategy.id if strategy.viewer?(current_user)
      end
    end

    Strategy.where(id: strategy_ids).order(created_at: :desc)
  end

  def define_moments(user)
    period = 'day'
    # +1 day buffer to ensure we include today as well
    end_date = Date.current + 1.day
    start_date = get_start_by_period(period, end_date)
    @react_moments = Moment.where(user: user)
                           .group_by_period(period,
                                            :created_at,
                                            range: start_date..end_date).count
  end

  def get_start_by_period(period, end_date)
    case period
    when 'day'
      end_date - 1.week
    when 'week'
      end_date - 1.month
    when 'month'
      end_date - 1.year
    else
      end_date - 1.week
    end
  end

  def publishing?
    params[:publishing] == '1'
  end

  def saving_as_draft?
    params[:publishing] != '1'
  end

  def empty_array_for(*symbols)
    symbols.each do |symbol|
      @moment[symbol] = [] if moment_params[symbol].nil?
    end
  end
end
# rubocop:enable ClassLength
<commit_msg>chore: cleanup MomentsController (#1233)<commit_after># frozen_string_literal: true
class MomentsController < ApplicationController
  include CollectionPageSetupConcern
  include MomentsHelper
  include Shared

  before_action :set_moment, only: %i[show edit update destroy]
  before_action :load_viewers, only: %i[new edit create update]

  # GET /moments
  # GET /moments.json
  def index
    if current_user
      # +1 day buffer to ensure we include today as well
      end_date = Date.current + 1.day
      start_date = end_date - 1.week
      @react_moments = current_user.moments
                                   .group_by_period('day',
                                                    :created_at,
                                                    range: start_date..end_date)
                                   .count
    end
    page_collection('@moments', 'moment')
  end

  # GET /moments/1
  # GET /moments/1.json
  def show
    show_with_comments(@moment)
  end

  # GET /moments/new
  def new
    @moment = Moment.new
    set_association_variables!
  end

  # GET /moments/1/edit
  def edit
    unless @moment.user_id == current_user.id
      redirect_to_path(moment_path(@moment))
    end
    set_association_variables!
  end

  # POST /moments
  # POST /moments.json
  def create
    @moment = current_user.moments.build(moment_params)
    @moment.published_at = Time.zone.now if publishing?
    shared_create(@moment)
  end

  # PATCH/PUT /moments/1
  # PATCH/PUT /moments/1.json
  def update
    if publishing? && !@moment.published?
      @moment.published_at = Time.zone.now
    elsif saving_as_draft?
      @moment.published_at = nil
    end
    empty_array_for :viewers, :mood, :strategy, :category
    shared_update(@moment, moment_params)
  end

  # DELETE /moments/1
  # DELETE /moments/1.json
  def destroy
    @moment.destroy
    redirect_to_path(moments_path)
  end

  private

  def set_moment
    @moment = Moment.friendly.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    redirect_to_path(moments_path)
  end

  def moment_params
    params.require(:moment).permit(
      :name, :why, :fix, :comment, :published_at, :draft,
      category: [], mood: [], viewers: [], strategy: []
    )
  end

  def set_association_variables!
    @categories = current_user.categories.order(created_at: :desc)
    @category = Category.new
    @moods = current_user.moods.order(created_at: :desc)
    @mood = Mood.new
    @strategies = associated_strategies
    @strategy = Strategy.new
  end

  def load_viewers
    @viewers = current_user.allies_by_status(:accepted)
  end

  def associated_strategies
    # current_user's strategies and all viewable strategies from allies
    strategy_ids = current_user.strategy_ids
    Strategy.where(user: @viewers).each do |strategy|
      strategy_ids << strategy.id if strategy.viewer?(current_user)
    end
    Strategy.where(id: strategy_ids).order(created_at: :desc)
  end

  def publishing?
    params[:publishing] == '1'
  end

  def saving_as_draft?
    !publishing?
  end

  def empty_array_for(*symbols)
    symbols.each do |symbol|
      @moment[symbol] = [] if moment_params[symbol].nil?
    end
  end
end

{'fraction_non_alphanumeric': 0.09383792909397862, 'fraction_numerical': 0.004220596510973551, 'mean_word_length': 2.8117962466487936, 'pattern_counts': {'":': 0, '<': 9, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2748320', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2287, 'n_words': 508}