<commit_before>class EventsController < ApplicationController
  before_action :authenticate_person!
  before_action :set_events, only: :index
  before_action :set_event, only: :show
  before_action :authorize_group_access

  protect_from_forgery except: [:create] #TODO: Add the csrf token in react.

  def index
    respond_to do |format|
      format.html
      format.json do
        render json: {
          events: JsonApi::EventsRepresenter.for_collection.new(Event.add_attendance_counts(@events)),
          tags: JsonApi::TagsRepresenter.for_collection.new(Tag.type_event),
          total_pages: @events.total_pages,
          page: @events.current_page
        }.to_json
      end
    end
  end

  def show
    event_with_attendance = Event.add_attendance_counts([@event]).first

    respond_to do |format|
      format.html
      format.json do
        render json: JsonApi::EventRepresenter.new(event_with_attendance).to_json
      end
    end
  end

  def create
    @event = Event.new(events_params)
    @event.groups << current_group
    respond_to do |format|
      if @event.save
        format.json do
          render json: JsonApi::EventRepresenter.new(@event).to_json
        end
      else
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_events
    return [] unless current_group

    #this is a bit clunky and could be cleaned up. --rabble
    return @events = Person.find(params[:member_id]).events.page(params[:page]) if params[:member_id]

    @events = Group.find(params[:group_id]).all_events.includes(:location, :groups)

    @events = @events.tagged_with(params[:tag]) if params[:tag]

    filter = params.fetch(:filter) { nil }

    if filter_by_key(filter, :name)
      @events = Event.joins(:location).where(<commit_msg>[#643] Fixed group event search bug.<commit_after>class EventsController < ApplicationController
  before_action :authenticate_person!
  before_action :set_events, only: :index
  before_action :set_event, only: :show
  before_action :authorize_group_access

  protect_from_forgery except: [:create] #TODO: Add the csrf token in react.

  def index
    respond_to do |format|
      format.html
      format.json do
        render json: {
          events: JsonApi::EventsRepresenter.for_collection.new(Event.add_attendance_counts(@events)),
          tags: JsonApi::TagsRepresenter.for_collection.new(Tag.type_event),
          total_pages: @events.total_pages,
          page: @events.current_page
        }.to_json
      end
    end
  end

  def show
    event_with_attendance = Event.add_attendance_counts([@event]).first

    respond_to do |format|
      format.html
      format.json do
        render json: JsonApi::EventRepresenter.new(event_with_attendance).to_json
      end
    end
  end

  def create
    @event = Event.new(events_params)
    @event.groups << current_group
    respond_to do |format|
      if @event.save
        format.json do
          render json: JsonApi::EventRepresenter.new(@event).to_json
        end
      else
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_events
    return [] unless current_group

    #this is a bit clunky and could be cleaned up. --rabble
    return @events = Person.find(params[:member_id]).events.page(params[:page]) if params[:member_id]

    @events = Group.find(params[:group_id]).all_events.includes(:location, :groups)

    @events = @events.tagged_with(params[:tag]) if params[:tag]

    filter = params.fetch(:filter) { nil }

    if filter_by_key(filter, :name)
      @events = @events.joins(:location).where(
{'fraction_non_alphanumeric': 0.09780547277160662, 'fraction_numerical': 0.000812787862367922, 'mean_word_length': 3.1623449830890644, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1195176', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1121, 'n_words': 258}