Class: Editor::TicketsController

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

Overview

This controller manage Ticket model for editors

Instance Method Summary collapse

Instance Method Details

#check_happeningObject (private)


81
82
83
# File 'app/controllers/editor/tickets_controller.rb', line 81

def check_happening
  access_denied! unless fact_ids.include?(Happening.find(ticket_params[:happening_id]).fact_id)
end

#createObject

POST /editor/facts/:fact_id/happenings/:happening_id/tickets/


47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/editor/tickets_controller.rb', line 47

def create
  @ticket = Ticket.new(ticket_params)
  @ticket.by_editor = true
  if @ticket.save
    flash[:success] = 'Prenotazione salvata'
    redirect_to tickets_editor_fact_happening_path(@ticket.happening.fact, @ticket.happening)
  else
    @users = User.pluck :username, :id
    @status = { error: 'Creatione prenotazione fallita' }
    render action: 'new'
  end
end

#destroyObject

DELETE /editor/facts/:fact_id/happenings/:happening_id/tickets/:id


73
74
75
76
77
# File 'app/controllers/editor/tickets_controller.rb', line 73

def destroy
  @ticket.destroy
  flash[:success] = 'Prenotazione eliminata'
  redirect_to editor_fact_happening_path(@fact, @happening)
end

#editObject

GET /editor/facts/:fact_id/happenings/:happening_id/tickets/:id/edit


42
43
44
# File 'app/controllers/editor/tickets_controller.rb', line 42

def edit
  @users = User.pluck :username, :id
end

#fact_idsObject (private)

set @fact before any action


86
87
88
# File 'app/controllers/editor/tickets_controller.rb', line 86

def fact_ids
  current_user.facts.pluck(:id)
end

#filter_paramsObject (private)

Filter params for search an Happening


107
108
109
# File 'app/controllers/editor/tickets_controller.rb', line 107

def filter_params
  params.fetch(:filter, {}).permit(:text, :user_id, :happening_id)
end

#happening_id(value) ⇒ Object (private)

Set @happening before any action


91
92
93
# File 'app/controllers/editor/tickets_controller.rb', line 91

def happening_id(value)
  Happening.find_by(id: value, fact_id: fact_ids).id
end

#indexObject

GET /editor/tickets


12
# File 'app/controllers/editor/tickets_controller.rb', line 12

def index; end

#list_by_happeningObject

GET /editor/tickets/list


15
16
17
18
19
20
21
22
23
# File 'app/controllers/editor/tickets_controller.rb', line 15

def list_by_happening
  @happening = Happening.find_by(id: params[:id], fact_id: fact_ids)
  @text = ["users.username ilike :string", { string: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
  @pagy, @tickets = pagy(
    @happening.tickets.includes(:user).where(@text),
    items: 6
  )
  render action: :list
end

#list_by_userObject


25
26
27
28
29
30
31
32
33
# File 'app/controllers/editor/tickets_controller.rb', line 25

def list_by_user
  @user = User.find(params[:id])
  @text = ["facts.title ilike :string", { string: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
  @pagy, @tickets = pagy(
    @user.tickets.includes(happening: [:fact]).where(@text).where(happening: { fact_id: fact_ids }),
    items: 6
  )
  render action: :list
end

#newObject

GET /editor/facts/:fact_id/happenings/:happening_id/tickets/new


36
37
38
39
# File 'app/controllers/editor/tickets_controller.rb', line 36

def new
  @ticket = Ticket.new(happening_id: happening_id(filter_params[:happening_id]))
  @users = User.pluck :username, :id
end

#set_ticketObject (private)

Set ticket when needed


96
97
98
99
# File 'app/controllers/editor/tickets_controller.rb', line 96

def set_ticket
  @ticket = Ticket.includes(:happening).find(params[:id])
  access_denied! unless fact_ids.include?(@ticket.happening.fact_id)
end

#ticket_paramsObject (private)

Filter params for set a ticket


102
103
104
# File 'app/controllers/editor/tickets_controller.rb', line 102

def ticket_params
  params.require(:ticket).permit(:happening_id, :user_id, :seats)
end

#updateObject

PATCH/PUT /editor/facts/:fact_id/happenings/:happening_id/tickets/:id


61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/editor/tickets_controller.rb', line 61

def update
  if @ticket.update(ticket_params.merge(by_editor: true))
    flash[:success] = 'Prenotazione salvata'
    render partial: 'ticket', locals: { ticket: @ticket, happening: @happening, fact: @fact }
  else
    @users = User.pluck :username, :id
    @status = { error: 'Aggionramento prenotazione fallito' }
    render partial: 'form', locals: { ticket: @ticket, happening: @happening, fact: @fact }
  end
end