Class: TicketsController

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

Overview

this controller manage Ticket model

Instance Method Summary collapse

Methods inherited from ApplicationController

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

Instance Method Details

#createObject

POST /event/:event_id/happenings/:happening_id/tickets



25
26
27
28
29
# File 'app/controllers/tickets_controller.rb', line 25

def create
  @ticket = current_user.tickets.create(filter_ticket)
  @happening = @ticket.happening
  @event = @happening.event
end

#destroyObject

POST /event/:event_id/happenings/:happening_id/tickets/:id



32
33
34
35
36
37
38
# File 'app/controllers/tickets_controller.rb', line 32

def destroy
  @ticket.destroy
  respond_to do |format|
    format.html { redirect_to tickets_path, notice: "Ticket was successfully destroyed." }
    format.turbo_stream
  end
end

#filter_paramsObject (private)

filter params for search Happening



53
54
55
# File 'app/controllers/tickets_controller.rb', line 53

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

#filter_ticketObject (private)

filter params for Happening‘s Ticket



58
59
60
# File 'app/controllers/tickets_controller.rb', line 58

def filter_ticket
  params.fetch(:ticket, {}).permit(:happening_id)
end

#indexObject

GET /tickets



9
10
11
12
13
14
15
16
# File 'app/controllers/tickets_controller.rb', line 9

def index
  @scope = filter_params[:scope]
  search = { user: current_user }
  search[:happening] = { start_at: ((filter_params[:from].try(:to_date) || Date.today)..filter_params[:to].try(:to_date).try(:end_of_day)) }
  search[:happening_id] = @scope if @scope.present?
  @text = [ "events.title ilike :text", { text: "%#{filter_params[:text]}%" } ] if filter_params[:text].present?
  @pagy, @tickets = pagy Ticket.joins(happening: [ :event ]).where(search).where(@text), items: 10
end

#set_prerequisiteObject (private)

seet @ticket when needed



43
44
45
46
# File 'app/controllers/tickets_controller.rb', line 43

def set_prerequisite
  @event = Event.find(params[:event_id])
  @happening = @event.happenings.find(params[:happening_id])
end

#set_ticketObject (private)



48
49
50
# File 'app/controllers/tickets_controller.rb', line 48

def set_ticket
  @ticket = Ticket.includes(happening: :event).find_by(user: current_user, id: params[:id])
end