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!, #record_not_found!

Instance Method Details

#createObject

POST /fact/:fact_id/happenings/:happening_id/tickets


19
20
21
22
# File 'app/controllers/tickets_controller.rb', line 19

def create
  TicketMailer.with(ticket: @ticket).confirm.deliver_later if @ticket.update filter_ticket
  render 'happenings/show'
end

#destroyObject

POST /fact/:fact_id/happenings/:happening_id/tickets/:id


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

def destroy
  TicketMailer.with(ticket: @ticket).deleted.deliver_later if @ticket.destroy
  render 'happenings/show'
end

#filter_ticketObject (private)

filter params for Happening's Ticket


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

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

#filter_ticketsObject (private)

filter params for search Happening


40
41
42
# File 'app/controllers/tickets_controller.rb', line 40

def filter_tickets
  params.fetch(:filter, {}).permit(:text, :type)
end

#indexObject

GET /tickets


9
# File 'app/controllers/tickets_controller.rb', line 9

def index; end

#listObject

GET /tickets/list


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

def list
  type = filter_tickets[:type] == 'history' ? 'start_at < :from' : 'start_at > :from'
  @text = ['facts.title ilike :text', { text: "%#{filter_tickets[:text]}%" }] if filter_tickets[:text].present?
  @pagy, @tickets = pagy current_user.tickets.joins(happening: [:fact]).where(type, from: Time.zone.now).where(@text), items: 10
end

#set_ticketObject (private)

seet @ticket when needed


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

def set_ticket
  @fact = Fact.find(params[:fact_id])
  @happening = @fact.happenings.find(params[:happening_id])
  @ticket = @happening.tickets.find_or_initialize_by(user: current_user)
end