Class: Editor::EventsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/editor/events_controller.rb

Overview

This controller manage Event model for editors

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_editor!

Methods inherited from ApplicationController

#access_denied!, #default_url_options, #record_not_found!, #set_turbo, #switch_locale

Instance Method Details

#createObject

POST /editor/events



31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/editor/events_controller.rb', line 31

def create
  @event = Event.new(event_params)
  if @event.save
    @status = { success: "Evento creato" }
    redirect_to editor_event_path(@event)
  else
    @status = { error: "ERRORE - Evento non creato. Verifica gli errori." }
    render action: :new
  end
end

#destroyObject

DELETE /editor/events/:id



54
55
56
57
58
59
60
61
62
# File 'app/controllers/editor/events_controller.rb', line 54

def destroy
  if @event.destroy
    @status = { success: "Evento eliminato" }
    redirect_to editor_events_path
  else
    @status = { error: "ERRORE - Evento non eliminato. Verifica gli errori." }
    render action: :edit
  end
end

#editObject

GET /editor/events/:id/edit



28
# File 'app/controllers/editor/events_controller.rb', line 28

def edit; end

#event_paramsObject (private)

Filter params for set a Event



82
83
84
85
# File 'app/controllers/editor/events_controller.rb', line 82

def event_params
  params.require(:event).permit(:tickets_frequency, :title, :group_id, :body, :start_on, :stop_on, :pinned, :image,
                               :where)
end

#filter_paramsObject (private)

Filter params for search a Event



77
78
79
# File 'app/controllers/editor/events_controller.rb', line 77

def filter_params
  params.fetch(:filter, {}).permit(:category, :from, :text, :to)
end

#indexObject

GET /editor/events



10
11
12
13
14
15
16
17
# File 'app/controllers/editor/events_controller.rb', line 10

def index
  @categories = @groups.pluck :title, :id
  from     = filter_params[:from]
  to       = filter_params[:to]
  group_id = @groups.exists?(filter_params[:category]) ? filter_params[:category] : @groups.pluck(:id)
  text     = filter_params[:text]
  @pagy, @events = pagy(Event.searchable(from:, to:, group_id:, text:), items: 6)
end

#newObject

GET /editor/events/new



23
24
25
# File 'app/controllers/editor/events_controller.rb', line 23

def new
  @event = Event.new
end

#set_eventObject (private)

set @event for current user when needed



67
68
69
70
# File 'app/controllers/editor/events_controller.rb', line 67

def set_event
  @event = Event.find_by(id: params[:id], group: @groups)
  @scope = @event.id
end

#set_groupsObject (private)



72
73
74
# File 'app/controllers/editor/events_controller.rb', line 72

def set_groups
  @groups = current_user.groups
end

#showObject

GET /editor/events/:id



20
# File 'app/controllers/editor/events_controller.rb', line 20

def show; end

#updateObject

PATCH/PUT /editor/events/:id



43
44
45
46
47
48
49
50
51
# File 'app/controllers/editor/events_controller.rb', line 43

def update
  if @event.update(event_params)
    @status = { success: "Evento Aggiornato" }
    render action: :show
  else
    @status = { error: "ERRORE - Evento non aggiornato. Verifica gli errori." }
    render :edit
  end
end