<commit_before>require 'icalendar'

class EventsController < ApplicationController
  respond_to :html

  helper_method :unapproved_count, :educational?, :current_filter?, :filter_params

  before_action :authenticate_user!, except: %i[index show]
  before_action :set_model, only: %i[index]
  before_action :set_event, only: [:show, :edit, :destroy, :update, :publish, :cancel_publication, :participants]
  before_action :check_actual_slug, only: :show
  before_action :define_meta_tags, only: [:show, :edit]
  before_action :set_organizer, only: :create
  before_action :set_filter_kind, only: :index

  load_and_authorize_resource param_method: :event_params, except: %i[index]

  has_scope :ordered_desc, type: :boolean, allow_blank: true, default: true

  def index
    if filter_params[:status] == 'unapproved'
      @events = @model.unapproved.visible_by_user(current_user)
    else
      @events = @model.filter_by(**filter_params).published
    end

    @events = @events.page(params[:page]).decorate
    @rss_events = @model.published.order(published_at: :desc).limit(100).decorate
    respond_to do |format|
      format.html
      format.json { render json: @model.published.order(started_at: :asc).page(params[:page]).to_json }
      format.atom
      format.ics { render body: Calendar.new(@model.published.order(started_at: :asc).page(params[:page])).to_ical }
      format.rss
    end
  end

  def show
    @event = @event.decorate
    flash[:warning] = t('.waiting_for_approval') unless @event.published?
    respond_to do |format|
      format.html { respond_with @event }
      format.ics { render body: Calendar.new(@event).to_ical, mime_type: Mime::Type.lookup("text/calendar") }
    end
  end

  def participants
    participants = @event.participants
    filename = "#{ @event.id }_#{ @event.slug }_participants"
    columns_to_export = %w(email profile_link full_name employment)

    respond_to do |format|
      format.csv {
        send_data RenderARCollectionToCsv.perform(participants, columns_to_export),
                  type: Mime::Type.lookup('text/csv'),
                  disposition: "attachment; filename=#{filename}.csv"
      }
    end
  end

  def new
    respond_with @event
  end

  def create
    @event.save
    @event.migrate_to_address
    respond_with @event
  end

  def edit
    respond_with @event
  end

  def destroy<commit_msg>Disable address resolving in test env<commit_after>require 'icalendar'

class EventsController < ApplicationController
  respond_to :html

  helper_method :unapproved_count, :educational?, :current_filter?, :filter_params

  before_action :authenticate_user!, except: %i[index show]
  before_action :set_model, only: %i[index]
  before_action :set_event, only: [:show, :edit, :destroy, :update, :publish, :cancel_publication, :participants]
  before_action :check_actual_slug, only: :show
  before_action :define_meta_tags, only: [:show, :edit]
  before_action :set_organizer, only: :create
  before_action :set_filter_kind, only: :index

  load_and_authorize_resource param_method: :event_params, except: %i[index]

  has_scope :ordered_desc, type: :boolean, allow_blank: true, default: true

  def index
    if filter_params[:status] == 'unapproved'
      @events = @model.unapproved.visible_by_user(current_user)
    else
      @events = @model.filter_by(**filter_params).published
    end

    @events = @events.page(params[:page]).decorate
    @rss_events = @model.published.order(published_at: :desc).limit(100).decorate
    respond_to do |format|
      format.html
      format.json { render json: @model.published.order(started_at: :asc).page(params[:page]).to_json }
      format.atom
      format.ics { render body: Calendar.new(@model.published.order(started_at: :asc).page(params[:page])).to_ical }
      format.rss
    end
  end

  def show
    @event = @event.decorate
    flash[:warning] = t('.waiting_for_approval') unless @event.published?
    respond_to do |format|
      format.html { respond_with @event }
      format.ics { render body: Calendar.new(@event).to_ical, mime_type: Mime::Type.lookup("text/calendar") }
    end
  end

  def participants
    participants = @event.participants
    filename = "#{ @event.id }_#{ @event.slug }_participants"
    columns_to_export = %w(email profile_link full_name employment)

    respond_to do |format|
      format.csv {
        send_data RenderARCollectionToCsv.perform(participants, columns_to_export),
                  type: Mime::Type.lookup('text/csv'),
                  disposition: "attachment; filename=#{filename}.csv"
      }
    end
  end

  def new
    respond_with @event
  end

  def create
    @event.save
    @event.migrate_to_address unless Rails.env.test?
    respond_with @event
  end

  def edit
    respond_with @event
  end

  def destroy
{'fraction_non_alphanumeric': 0.11254420636571666, 'fraction_numerical': 0.001248179737882255, 'mean_word_length': 3.9161554192229038, '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': '912713', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1508, 'n_words': 332}