-
Notifications
You must be signed in to change notification settings - Fork 0
refactoring #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
refactoring #75
Changes from 88 commits
8bac5b0
9685bc3
c63eb8b
eab445d
f4578e3
b8703d4
f443726
30a66ca
0a4d12c
c1be315
a906740
503a783
b15f867
ce66ae2
17b17d3
1e770b0
d419bcb
0f93138
ee7e900
0bb6c22
03e7102
34544af
26f80da
6e93ee0
fb05e39
6f011a7
0c55f34
5f7824b
ae05494
6bf4edf
2c5342f
058b238
37efbda
9f9d99c
a8106b7
3b8b882
b7c4da4
432274d
6eed13c
ff31f54
83f6818
6206f9a
dd56a25
0293f8a
483fc73
0c33b29
01f5de8
014230d
6c6f70e
dc3db55
8984936
2496747
d174db3
f241a91
ba5eec0
b719e16
0371c6b
1290b14
e2d3948
585f57c
15612e2
4f3dfe8
91d0fd9
c6a32e8
88f3d63
ecf5719
08bc5e4
3e8540e
52f176e
a6a1c91
78a4d85
b67c214
1b0599e
4f743c0
7e7f7f8
b429d5a
d4b5ea9
ad41d0e
9b2f84c
a670aa2
9db7725
f1deb15
8485ce0
372a718
98d4da9
be4de17
994534e
b920d12
68bd19a
310e49b
f2bfc57
ab8dc1e
6a66a0c
4d1895a
bdd4890
ea00633
bef894f
2b94b97
6e79e22
c3a9139
2b1225a
7565948
0f8f132
62f5f9e
45b855e
30e9daa
23a5259
13c8925
e6a7369
f9f9869
1fee544
ab8f4c7
9a0677d
ac69b3f
d0fe754
405a2f6
fe6bb12
23102af
0baa2e3
e74bf13
f4b6b42
d369859
6e124f0
74551ce
6db77ce
f19dcd3
a4fffae
58d64af
db3f218
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # channel for timer and selection | ||
| class MatchChannel < ApplicationCable::Channel | ||
| def subscribed | ||
| stream_from "match_channel_#{params[:match_id]}" | ||
| end | ||
| end |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Matches_Controller | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RESTFul Routes comments index -> should have been a nested route tournaments/id/matches/ |
||
| class MatchesController < ApplicationController | ||
| load_and_authorize_resource :tournament | ||
| load_and_authorize_resource through: :tournament | ||
|
|
@@ -16,31 +15,19 @@ def all | |
|
|
||
| def show; end | ||
|
|
||
| def new | ||
| @match = Match.new | ||
| end | ||
|
|
||
| def playmatch | ||
| @match = Match.includes([:users]).find(params[:match_id]) | ||
| redirect_to result_tournament_match_path(match_id: @match) unless @match.match_winner_id.nil? | ||
| @players = @match.users | ||
| @remaining_tries = @match.remaining_tries(@players.first.id) | ||
| @done_tries = @match.selections.by_user(@players.first.id).size | ||
| @is_player = @match.users.include?(current_user) | ||
| @messages = @match.messages.includes([:user]).all.reverse | ||
| end | ||
| return if @match.done? | ||
|
|
||
| def result | ||
| @is_player = @match.users.include?(current_user) | ||
| @players = @match.users | ||
| @players_selections = @match.selections.order(:try_num).group_by(&:user_id) | ||
| @players_scores = @match.selections.group(:user_id).winner.count | ||
| @result_message = @match.result_message(current_user.id) | ||
| @players_data = @match.users.pluck(:id, :name).to_h | ||
| end | ||
|
|
||
| private | ||
| def result | ||
| return if @match.undone? | ||
|
|
||
| def match_params | ||
| params.require(:match).permit(:match_winner_id, :winner_score, :match_time, :tournament_id) | ||
| @players_data = @match.users.pluck(:id, :name).to_h | ||
| @players_selections = @match.selections.includes(:user).order(:try_num).group_by(&:user_id) | ||
| @players_scores = @match.selections.winner.group(:user_id).count | ||
| @result_message = @match.result_message(current_user.id) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SelectionController < ApplicationController | ||
| load_and_authorize_resource | ||
| before_action :authenticate_user! | ||
|
|
||
| def create | ||
| @selection = Selection.new(selection_params) | ||
| @selection.add_try_num | ||
| respond_to do |format| | ||
| if @selection.save | ||
| format.json { render json: { data: 'Saved' }, status: :ok } | ||
| else | ||
| format.json { render json: { errors: @selection.errors.full_messages }, status: :unprocessable_entity } | ||
| end | ||
| if @selection.save | ||
| flash[:notice] = 'Choice Saved' | ||
| else | ||
| flash[:error] = @selection.errors.full_messages.join(', ') | ||
| end | ||
| end | ||
|
|
||
| def new | ||
| @selection = Selection.new | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def selection_params | ||
| params.require(:selection).permit(:match_id, :user_id, :selection) | ||
| params.require(:selection).permit(:match_id, :user_id, :choice) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,72 +1,67 @@ | ||
| # Tournamnets Controller | ||
| # frozen_string_literal: true | ||
|
|
||
| class TournamentsController < ApplicationController | ||
| load_and_authorize_resource | ||
| before_action :authenticate_user!, except: [:index] | ||
|
|
||
| def index | ||
| @tournaments = Tournament.includes(:users).all | ||
| @tournaments = Tournament.includes(%i[users winner]).all | ||
| @tournaments = @tournaments.order(:registration_deadline).page(params[:page]) | ||
| end | ||
|
|
||
| def show; end | ||
|
|
||
| def new | ||
| @tournament = Tournament.new | ||
| end | ||
| def new; end | ||
|
|
||
| def edit; end | ||
|
|
||
| def register | ||
| @tournaments_user = TournamentsUser.new(user: current_user, tournament: @tournament) | ||
| if @tournaments_user.save | ||
| redirect_to tournament_url(@tournament), notice: 'You have registered for the tournament!' | ||
| flash[:notice] = 'You have registered for the tournament!.' | ||
| else | ||
| redirect_to tournament_url(@tournament), notice: 'Already Registered' | ||
| flash[:errors] = @tournament.errors.full_messages.join(', ') | ||
| end | ||
| redirect_to tournament_url(@tournament) | ||
| end | ||
|
|
||
| def create_matches | ||
| registered_users = @tournament.users | ||
| length = registered_users.length | ||
| return unless (length - 8) != 0 | ||
|
|
||
| begin | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| MatchCreator.new(@tournament, registered_users).create_match | ||
| redirect_to tournament_path(@tournament), notice: 'Matches Generated' | ||
| TournamentMatchesCreator.new(@tournament, @tournament.users).call | ||
| flash[:notice] = 'Matches Generated.' | ||
| rescue StandardError => e | ||
| redirect_to tournament_path(@tournament), alert: "Error generating matches: #{e.message}" | ||
| flash[:alert] = "Error generating matches: #{e.message}" | ||
| end | ||
| redirect_to tournament_path(@tournament) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line is repeating. You can DRY it ? |
||
| end | ||
|
|
||
| def create | ||
| @tournament = Tournament.new(tournament_params) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Load everything using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to show why an operation failed so the user can take appropriate actions, -> https://github.com/techiealiraza/rock-papper-sissors-app/pull/75/files#diff-c9c5ff5ae897df4b0504a76d7549d657c8b3c20875611584db7552f5a83ac229L48 |
||
| respond_to do |format| | ||
| if @tournament.save | ||
| format.html { redirect_to tournament_url(@tournament), notice: 'Tournament was successfully created.' } | ||
| else | ||
| format.html { render :new, status: :unprocessable_entity } | ||
| end | ||
| if @tournament.save | ||
| flash[:notice] = 'Tournament was successfully created.' | ||
| redirect_to tournament_url(@tournament) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _path vs _url ? |
||
| else | ||
| flash[:errors] = @tournament.errors.full_messages.join(', ') | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| respond_to do |format| | ||
| if @tournament.update(tournament_params) | ||
| format.html { redirect_to tournament_url(@tournament), notice: 'Tournament was successfully updated.' } | ||
| else | ||
| format.html { render :edit, status: :unprocessable_entity } | ||
| end | ||
| if @tournament.update(tournament_params) | ||
| flash[:notice] = 'Tournament was successfully updated.' | ||
| redirect_to tournament_url(@tournament) | ||
| else | ||
| flash[:errors] = @tournament.errors.full_messages.join(', ') | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| respond_to do |format| | ||
| if @tournament.destroy | ||
| format.html { redirect_to tournaments_url, notice: 'Tournament was successfully deleted.' } | ||
| else | ||
| format.html { render :edit, status: :unprocessable_entity } | ||
| end | ||
| if @tournament.destroy | ||
| flash[:notice] = 'Tournament was successfully deleted.' | ||
| redirect_to tournaments_url | ||
| else | ||
| flash[:errors] = @tournament.errors.full_messages.join(', ') | ||
| render :index | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. render -> index is wrong |
||
| end | ||
| end | ||
|
|
||
|
|
@@ -77,7 +72,7 @@ def tournament_params | |
| :description, | ||
| :start_date, | ||
| :end_date, | ||
| :tournament_winner_id, | ||
| :winner_id, | ||
| :image, | ||
| :registration_deadline) | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ali -> Code Understanding