Class: Editor::HappeningsController

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

Overview

This controller manafe Happening model for editors

Instance Method Summary collapse

Instance Method Details

#createObject

POST /editor/happenings


30
31
32
33
34
35
36
37
38
# File 'app/controllers/editor/happenings_controller.rb', line 30

def create
  @happening = @fact.happenings.new(happening_params)

  if @happening.save
    render 'editor/facts/show'
  else
    render :new
  end
end

#destroyObject

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


50
51
52
53
# File 'app/controllers/editor/happenings_controller.rb', line 50

def destroy
  @happening.destroy
  redirect_to editor_root_path
end

#editObject

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


27
# File 'app/controllers/editor/happenings_controller.rb', line 27

def edit; end

#exportObject

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


56
57
58
59
# File 'app/controllers/editor/happenings_controller.rb', line 56

def export
  @tickets = [%w[Username Posti]] + @happening.tickets.includes(:user).all.map { |t| [t.user.username, t.seats] } + [['Totale', @happening.seats_count]]
  send_data @tickets.map(&:to_csv).join, filename: 'tickets.csv'
end

#filter_paramsObject (private)

Filter params for search an Happening


79
80
81
# File 'app/controllers/editor/happenings_controller.rb', line 79

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

#happening_paramsObject (private)

Filter params for set an Happening


74
75
76
# File 'app/controllers/editor/happenings_controller.rb', line 74

def happening_params
  params.require(:happening).permit(:detail, :start_at, :start_sale_at, :stop_sale_at, :max_seats, :max_seats_for_ticket, :repeat_for, repeat_in: [])
end

#indexObject

GET /editor/facts/:fact_id/happenings


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

def index
  type = filter_params[:type] == 'history' ? 'history' : 'future'
  @text = ['detail ilike :text', { text: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
  @pagy, @happenings = pagy(
    @fact.happenings.send(type).where(@text),
    items: 6
  )
end

#newObject

GET /editor/facts/fact_id/happenings/new


22
23
24
# File 'app/controllers/editor/happenings_controller.rb', line 22

def new
  @happening = @fact.happenings.new(repeat_for: 0, repeat_in: [1, 2, 3, 4, 5])
end

#set_factObject (private)

Set @fact beore each action


64
65
66
# File 'app/controllers/editor/happenings_controller.rb', line 64

def set_fact
  @fact = current_user.facts.find(params[:fact_id])
end

#set_happeningObject (private)

Set @happening when needed


69
70
71
# File 'app/controllers/editor/happenings_controller.rb', line 69

def set_happening
  @happening = @fact.happenings.find(params[:id])
end

#showObject

GET /editor/facts/:fact_id/happenings/:id


19
# File 'app/controllers/editor/happenings_controller.rb', line 19

def show; end

#updateObject

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


41
42
43
44
45
46
47
# File 'app/controllers/editor/happenings_controller.rb', line 41

def update
  if @happening.update(happening_params)
    render action: :show
  else
    render :edit
  end
end