-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgames_controller.rb
More file actions
118 lines (101 loc) · 2.75 KB
/
games_controller.rb
File metadata and controls
118 lines (101 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class GamesController < ApplicationController
before_action :find_game, only: [:show, :update, :edit]
before_action :authenticate_user!, except: [:index, :show]
GAMES_PER_PAGE = 6
def index
@states = Game.states
@tags = Tag.all
if params[:user_id]
user = User.find params[:user_id]
@games = user.games
else
@games = search(game_subset)
end
@games = @games.page(params[:page]).per(GAMES_PER_PAGE)
end
def show
end
def update
game = Game.find params[:id]
if cannot? :manage, game
redirect_to root_path
elsif game.update(game_params)
redirect_to @game, notice: 'Game status was successfully updated.'
elsif params[:commit]
toggle_state(game)
else
render :edit
end
end
def new
@game = Game.new
end
def create
@game = Game.new(game_params)
@game.user = current_user
if @game.save
redirect_to games_path, notice: 'Game was successfully uploaded.'
else
render :new
end
end
def edit
game = Game.find params[:id]
redirect_to root_path if cannot? :manage, game
end
def destroy
game = Game.find params[:id]
if cannot? :manage, game
redirect_to root_path
else
game.destroy
redirect_to games_path
end
end
def toggle_state(game)
params[:commit] == Game::STATE_APPROVE ? game.approve : game.reject
game.save
redirect_to game
end
private
def game_subset
Game.user_data_subset(user_is_admin?, user_is_dev?, current_user&.id)
end
def search(games_to_search)
results = games_to_search
if params[:search_user].present?
results = results.search_by('user', params[:search_user])
end
if params[:tag].present?
results = results.search_by('tags', params.require(:tag)[:tag_ids])
end
if params[:search_state] && !params[:search_state].first.empty?
results = results.search_by('state', params[:search_state])
end
results
end
def find_game
@game = Game.find(params[:id]).decorate
end
def game_params
params.require(:game).permit(:title, :cpu, :gpu, :ram,
:size, :approval_date,
:status_id, :stats,
:description, { pictures: [] },
:attachment, :link,
{ tag_ids: [] }, :aasm_state)
end
# Used to fulfill client requests for 5 new games
def fill_machine_order(games)
games.limit(5).order('RANDOM()')
end
def alter_aasm(game)
if params[:commit] == Game::STATE_APPROVE
game.approve
elsif params[:commit] == Game::STATE_REJECT
game.reject
end
game.save
redirect_to @game, notice: 'Game state was successfully updated.'
end
end