Class: Editor::HappeningsController

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

Overview

This controller manafe Happening 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/happenings



34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/editor/happenings_controller.rb', line 34

def create
  @event = Event.find_by(id: happening_params[:event_id], group: @groups)
  @happenings = @event.happenings.massive_create(**massive_create_params.to_h.to_options)
  @happening = @happenings.first

  if @happening.save
    render "editor/events/show"
  else
    render :new
  end
end

#destroyObject

DELETE /editor/events/:event_id/happenings/:id



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

def destroy
  @happening.destroy
  redirect_to editor_root_path
end

#editObject

GET /editor/events/:event_id/happenings/:id/edit



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

def edit; end

#exportObject

GET /editor/events/:event_id/happenings/:happening_id/tickets/export



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

def export
  @tickets = [ %w[Email] ] + @happening.tickets.includes(:user).all.map { |t|
                                      [ t.user.email ]
                                    } + [ [ @happening.tickets_count ] ]
  send_data @tickets.map(&:to_csv).join, filename: "tickets.csv"
end

#filter_paramsObject (private)

Filter params for search an Happening



92
93
94
# File 'app/controllers/editor/happenings_controller.rb', line 92

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

#happening_paramsObject (private)

Filter params for set an Happening



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

def happening_params
  params.require(:happening).permit(:title, :event_id, :max_tickets, :max_tickets_for_user, :start_at, :start_sale_at, :stop_sale_at)
end

#indexObject

GET /editor/events/:event_id/happenings



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

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

#massive_create_paramsObject (private)



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

def massive_create_params
  params.require(:happening).permit(:title, :event_id, :max_tickets, :max_tickets_for_user, :from, :to, :start_sale_before, :stop_sale_before, repeat_in: [], minutes: [])
end

#newObject

GET /editor/events/event_id/happenings/new



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

def new
  @event = Event.find_by(id: params[:event_id], group: @groups)
  @happening = @event.happenings.new
end

#set_groupsObject (private)



71
72
73
# File 'app/controllers/editor/happenings_controller.rb', line 71

def set_groups
  @groups = current_user.groups
end

#set_happeningObject (private)

Set @happening when needed



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

def set_happening
  @happening = Happening.includes(:event).where(event: { group: current_user.groups }).find(params[:id])
  @event = @happening.event
end

#showObject

GET /editor/events/:event_id/happenings/:id



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

def show; end

#updateObject

PATCH/PUT /editor/events/:event_id/happenings/:id



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

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