<commit_before>class ItemsController < ApplicationController
  require_relative '../../app/domain/policies/user/account_complete'

  before_action :set_item, only: [:show, :edit, :update, :destroy, :schedule]

  # access for only logged users
  before_action :is_logged, only: [:new, :edit, :create, :update]

  # access only for admin users
  before_action :is_admin, only: [:index, :aprove, :destroy]

  layout 'main_and_right_sidebar', except: :index


  include Gmaps


  # GET /events
  # GET /events.json
  def index
    @items = get_item_class.all
    render layout: 'full-width', template: "#{get_item_route}/index"
  end



  # GET /events/1
  # GET /events/1.json
  def show
    require_relative '../../app/domain/dependecies/javascript_dependencies'

    if user_signed_in?
      @distance = current_user.distance_until(@item, :minutes)
    end

    @tip = Tip.new

    # geolocalization of event
    gon.latitude = @item.relative_latitude
    gon.longitude = @item.relative_longitude

    # array with places for map
    gon.events_local_formatted = format_for_map_this(Item.all)

    render template: 'items/show'
  end



  # GET /events/new
  def new

    unless Villeme::Policies::AccountComplete.is_complete?(current_user)
      redirect_to root_path, alert: 'Você precisa estar com o perfil completo para criar um evento!'
    end

    @item = current_user.items.build(type: params[:type])
    @place = @item.build_place

    set_current_user_lat_long_in_gon
    set_array_of_places_in_gon

    render template: "#{get_item_route}/new"
  end




  # GET /events/1/edit
  def edit

    if @item.user_id == current_user.id or current_user.admin?
      @item.build_place if @item.place.nil?

      set_array_of_places_in_gon

      gon.latitude = @item.relative_latitude
      gon.longitude = @item.relative_longitude

      render template: 'items/edit'
    else
      redirect_to root_path, notice: 'Ops! Você so pode editar os eventos que criou.'
    end
  end




  # POST /events
  # POST /events.json
  def create
    @item = current_user.items.create(item_params)
    set_type_for_item
    auto_moderate_if_admin
    set_place_for_item

    if @item.save
      redirect_item_according_type
    else
      render action: 'new', alert: 'O evento não pode ser criado, arrume as informações abaixo.'
    end
  end




  # PATCH/PUT /events/1
  # PATCH/PUT /events/1.json
  def update
    @item.update_attributes(item_params)
    auto_moderate_if_admin
    set_place_for_item

    if @item.save
      redirect_item_according_type
    else
      render action: 'new', alert: 'O evento não pode ser atualizado, arrume as informações abaixo.'
    end
  end






  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @item.destroy
    redirect_to events_url
  end



  # PUT /events/aprove/1.json
  def aprove
    @item = Item.find params[:id]
    @item.moderate = 1
    @item.save!
    render js: "alert('Evento aprovado!');"
  end


  def schedule
    if current_or_guest_user.guest?
      render js: 'Villeme.Ux.loginModal("Você precisa estar logado para agendar eventos!")'
    elsif agended(@item)
      current_user.unschedule(@item)
      agended_by_count = @item.agended_by_count
      render json: {agended: false, event: "event-#{@item.id}", count: current_user.agenda_items.upcoming.count, agended_by_count: agended_by_count[:count], new_title: agended_by_count[:text]}<commit_msg>Removed comments<commit_after>class ItemsController < ApplicationController
  require_relative '../../app/domain/policies/user/account_complete'

  before_action :set_item, only: [:show, :edit, :update, :destroy, :schedule]

  # access for only logged users
  before_action :is_logged, only: [:new, :edit, :create, :update]

  # access only for admin users
  before_action :is_admin, only: [:index, :aprove, :destroy]

  layout 'main_and_right_sidebar', except: :index


  include Gmaps


  def index
    @items = get_item_class.all
    render layout: 'full-width', template: "#{get_item_route}/index"
  end


  def show
    require_relative '../../app/domain/dependecies/javascript_dependencies'

    if user_signed_in?
      @distance = current_user.distance_until(@item, :minutes)
    end

    @tip = Tip.new

    # geolocalization of event
    gon.latitude = @item.relative_latitude
    gon.longitude = @item.relative_longitude

    # array with places for map
    gon.events_local_formatted = format_for_map_this(Item.all)

    render template: 'items/show'
  end



  def new

    unless Villeme::Policies::AccountComplete.is_complete?(current_user)
      redirect_to root_path, alert: 'Você precisa estar com o perfil completo para criar um evento!'
    end

    @item = current_user.items.build(type: params[:type])
    @place = @item.build_place

    set_current_user_lat_long_in_gon
    set_array_of_places_in_gon

    render template: "#{get_item_route}/new"
  end



  def edit

    if @item.user_id == current_user.id or current_user.admin?
      @item.build_place if @item.place.nil?

      set_array_of_places_in_gon

      gon.latitude = @item.relative_latitude
      gon.longitude = @item.relative_longitude

      render template: 'items/edit'
    else
      redirect_to root_path, notice: 'Ops! Você so pode editar os eventos que criou.'
    end
  end



  def create
    @item = current_user.items.create(item_params)
    set_type_for_item
    auto_moderate_if_admin
    set_place_for_item

    if @item.save
      redirect_item_according_type
    else
      render action: 'new', alert: 'O evento não pode ser criado, arrume as informações abaixo.'
    end
  end




  def update
    @item.update_attributes(item_params)
    auto_moderate_if_admin
    set_place_for_item

    if @item.save
      redirect_item_according_type
    else
      render action: 'new', alert: 'O evento não pode ser atualizado, arrume as informações abaixo.'
    end
  end




  def destroy
    @item.destroy
    redirect_to events_url
  end



  def aprove
    @item = Item.find params[:id]
    @item.moderate = 1
    @item.save!
    render js: "alert('Evento aprovado!');"
  end


  def schedule
    if current_or_guest_user.guest?
      render js: 'Villeme.Ux.loginModal("Você precisa estar logado para agendar eventos!")'
    elsif agended(@item)
      current_user.unschedule(@item)
      agended_by_count = @item.agended_by_count
      render json: {agended: false, event: "event-#{@item.id}", count: current_user.agenda_items.upcoming.count, agended_by_count: agended_by_count[:count], new_title: agended_by_count[:text]}
{'fraction_non_alphanumeric': 0.09461938245184959, 'fraction_numerical': 0.001528584530724549, 'mean_word_length': 3.3561917443408786, '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': '2996640', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2170, 'n_words': 518}