hpi-swt2/sport-portal

View on GitHub
app/controllers/events_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class EventsController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy, :join, :leave, :schedule, :ranking, :team_join, :overview]
  authorize_resource :event


  helper EventsHelper

  # GET /events
  def index
    @events = Event.all
  end

  # GET /events/1
  def show
  end

  # GET /events/new
  def new
    if event_type
      @event = event_type.new
    else
      render :create_from_type
    end
  end

  # GET /events/1/edit
  def edit
  end

  # POST /events
  def create
    @event = event_type.new(event_params)
    set_associations
    if @event.save
      @event.editors << current_user
      redirect_to @event, notice: 'Event was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /events/1
  def update
    @event.invalidate_schedule if event_params[:has_place_3_match].to_i.zero? == @event.has_place_3_match
    if @event.update(event_params)
      redirect_to @event, notice: 'Event was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /events/1
  def destroy
    @event.destroy
    redirect_to events_url, notice: 'Event was successfully destroyed.'
  end

  # PUT /events/1/join
  def join
    if @event.single?
      @event.add_participant(current_user)
    else
      @event.add_team(Team.find(event_params[:teams]))
    end

    flash[:success] = t('success.join_event', event: @event.name)
    redirect_back fallback_location: events_url
  end

  # GET /events/1/team_join
  def team_join
    respond_to do |format|
      format.js
    end
  end

  # PUT /events/1/leave
  def leave
    team = Team.find((current_user.owned_teams & @event.teams)[0].id)
    @event.remove_team(team)
    flash[:success] = t('success.leave_event', event: @event.name)
    redirect_back fallback_location: events_url
  end

  # GET /events/1/schedule
  def schedule
    if @event.matches.empty?
      @event.generate_schedule
      @event.save
    elsif not @event.is_up_to_date
      @event.update_schedule
      @event.save
    end

    @matches = @event.matches
    @schedule_type = @event.type.downcase!
  end

  def overview
    if @event.type == 'League'
      redirect_to @event, error: t('events.overview.not_available_for_leagues')
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_event
      @event = Event.find(params[:id])
    end

    def set_associations
      @event.matchtype ||= :bestof
      @event.game_winrule ||= :most_sets
      @event.min_players_per_team ||= 1
      @event.max_players_per_team ||= 1
      @event.owner = current_user
    end

    # Get the type of event that should be created
    def event_type
      return League if params[:type] == 'League'
      return Tournament if params[:type] == 'Tournament'
      return Rankinglist if params[:type] == 'Rankinglist'
      params[:type]
    end

    def map_event_on_event_types
      [:league, :tournament, :rankinglist].each do |value|
        delete_mapping_parameter value
      end
    end

    def delete_mapping_parameter(event_class)
      if params.has_key? event_class
        params[:event] = params.delete event_class
      end
    end

    # Only allow a trusted parameter "white list" through.
    def event_params
      map_event_on_event_types
      params.require(:event).permit(:name,
                                    :description,
                                    :discipline,
                                    :type,
                                    :game_mode,
                                    :max_teams,
                                    :player_type,
                                    :deadline,
                                    :startdate,
                                    :teams,
                                    :enddate,
                                    :matchtype,
                                    :bestof_length,
                                    :game_winrule,
                                    :points_for_win,
                                    :points_for_draw,
                                    :points_for_lose,
                                    :initial_value,
                                    :min_players_per_team,
                                    :max_players_per_team,
                                    :gameday_duration,
                                    :image,
                                    :remove_image,
                                    :has_place_3_match,
                                    :maximum_elo_change,
                                    user_ids: [])
    end
end