<commit_before>class DiscussionsController < ApplicationController
  include Mumuki::Laboratory::Controllers::Content

  before_action :validate_forum_enabled!
  before_action :validate_not_in_exam!
  before_action :set_debatable, except: [:subscription]
  before_action :authenticate!, only: [:update, :create]
  before_action :discussion_filter_params, only: :index
  before_action :read_discussion, only: :show

  helper_method :discussion_filter_params

  def index
    @discussions = current_content_discussions.for_user(current_user)
    @filtered_discussions = @discussions.scoped_query_by(discussion_filter_params)
  end

  def show
  end

  def update
    subject.update_status! params[:status], current_user
    redirect_to [@debatable, subject], notice: I18n.t(:discussion_updated)
  end

  def subscription
    current_user&.toggle_subscription!(subject)
    head :ok
  end

  def upvote
    current_user&.toggle_upvote!(subject)
    head :ok
  end

  def create
    validate_discussions_permission!
    discussion = @debatable.discuss! current_user, discussion_params
    redirect_to [@debatable, discussion]
  end

  private

  def current_content_discussions
    @debatable.discussions
  end

  def set_debatable
    @debatable_class = params[:debatable_class]
    @debatable = Discussion.debatable_for(@debatable_class, params)
  end

  def subject
    @discussion ||= Discussion.find_by(id: params[:id])
  end

  def read_discussion
    @discussion.subscription_for(current_user)&.read!
  end

  def discussion_params
    params.require(:discussion).permit(:title, :description)
  end

  def discussion_filter_params
    @filter_params ||= params.permit(Discussion.permitted_query_params)
  end

  def validate_forum_enabled!
    raise Mumuki::Domain::NotFoundError unless Organization.current.forum_enabled?
  end

  def validate_discussions_permission!<commit_msg>Using before hooks for validations<commit_after>class DiscussionsController < ApplicationController
  include Mumuki::Laboratory::Controllers::Content

  # discussions are not enabled for all organitions nor all users
  # there is no need to validate forum existance when creating; next validation is stronger
  before_action :validate_forum_enabled!, except: :create
  before_action :validate_can_create_discusssion!, only: :create

  # users are not allowed to access discussions during exams
  before_action :validate_not_in_exam!

  before_action :set_debatable, except: [:subscription]
  before_action :authenticate!, only: [:update, :create]
  before_action :discussion_filter_params, only: :index
  before_action :read_discussion, only: :show

  helper_method :discussion_filter_params

  def index
    @discussions = current_content_discussions.for_user(current_user)
    @filtered_discussions = @discussions.scoped_query_by(discussion_filter_params)
  end

  def show
  end

  def update
    subject.update_status! params[:status], current_user
    redirect_to [@debatable, subject], notice: I18n.t(:discussion_updated)
  end

  def subscription
    current_user&.toggle_subscription!(subject)
    head :ok
  end

  def upvote
    current_user&.toggle_upvote!(subject)
    head :ok
  end

  def create
    discussion = @debatable.discuss! current_user, discussion_params
    redirect_to [@debatable, discussion]
  end

  private

  def current_content_discussions
    @debatable.discussions
  end

  def set_debatable
    @debatable_class = params[:debatable_class]
    @debatable = Discussion.debatable_for(@debatable_class, params)
  end

  def subject
    @discussion ||= Discussion.find_by(id: params[:id])
  end

  def read_discussion
    @discussion.subscription_for(current_user)&.read!
  end

  def discussion_params
    params.require(:discussion).permit(:title, :description)
  end

  def discussion_filter_params
    @filter_params ||= params.permit(Discussion.permitted_query_params)
  end

  def validate_forum_enabled!
    raise Mumuki::Domain::NotFoundError unless Organization.current.forum_enabled?
  end

  def validate_can_create_discusssion!
{'fraction_non_alphanumeric': 0.07475943745373798, 'fraction_numerical': 0.0009869232667160128, 'mean_word_length': 5.142424242424243, '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': '660784', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1200, 'n_words': 269}