From 5565abf7b648afd2e1d1d0e4368dce1aa4aefd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chwa=C5=82a?= Date: Sun, 23 Jun 2024 01:23:34 +0200 Subject: [PATCH 1/3] Show bets of user --- app/controllers/users_controller.rb | 10 ++++++ app/models/match.rb | 5 +++ app/views/users/index.html.erb | 2 +- app/views/users/show.html.erb | 56 +++++++++++++++++++++++++++++ config/routes.rb | 2 +- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f2922a4..d62622c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,11 +2,14 @@ class UsersController < ApplicationController def index + authorize! :index, User @user = User.new end def create + authorize! :create, User user = User.new(invitation_params.merge(invited_by: current_user)) + if user.save @token = user.generate_token_for(:invitation) else @@ -39,9 +42,16 @@ def destroy redirect_to users_path end + def show + @user = User.find(params[:id]) + @matches_started = Match.started.includes(:answers) + @answers = @user.answers + end + private def invitation_params params.require(:user).permit(:username) end + end diff --git a/app/models/match.rb b/app/models/match.rb index fc5a378..a8e2926 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -12,6 +12,11 @@ class Match < ApplicationRecord numericality: { greater_than_or_equal_to: 0, only_integer: true }, allow_blank: true + scope :started, lambda { + where(arel_table[:start].lt(DateTime.now)) + .order(:start) + } + scope :finished, lambda { where(arel_table[:start].lt(DateTime.now)) .where.not(result_a: nil) diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 033858d..0895a54 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -37,7 +37,7 @@ <% User.includes(:invited_by).order(:username).each_with_index do |user, index| %> <%= index + 1 %> - <%= user.username %> + <%= link_to user.username, user_path(user) %> <%= user.invited_by_username %> <%= user_status(user) %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..496294c --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,56 @@ +<%= render partial: 'shared/menu', locals: { s: 'users' } %> +

+ + Głosy użytkownika <%= @user.username %> +

+
+ + <% @matches_started.group_by(&:start_date).each do |start_date, games| %> + + + + + + + + + + + + + <% games.each do |match| %> + + + + + + + + + + + + <% end %> + + <% end %> +
<%= l(start_date, format: :only_date) %>1X21XX212
+ <%= l(match.start, format: :only_time) %> + <%= "#{match.result_a}:#{match.result_b}" %> + <%= match.team_a %> + - + <%= match.team_b %> + + <%= number_with_precision(match.win_a) || '---' %> + + <%= number_with_precision(match.tie) || '---' %> + + <%= number_with_precision(match.win_b) || '---' %> + + <%= number_with_precision(match.win_tie_a) || '---' %> + + <%= number_with_precision(match.win_tie_b) || '---' %> + + <%= number_with_precision(match.not_tie) || '---' %> +
+
+ diff --git a/config/routes.rb b/config/routes.rb index 36311cc..1b45b96 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ post 'sign_in', to: 'sessions#create' delete 'sign_out', to: 'sessions#destroy' - resources :users, only: %i[index create destroy] do + resources :users, only: %i[index create destroy show] do member do get :resend_invitation get :fin From 84b68110dc1053bfbebab8cb943e850d8d757fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chwa=C5=82a?= Date: Sun, 23 Jun 2024 01:30:37 +0200 Subject: [PATCH 2/3] Add link to user bets from ranking view --- app/views/rankings/show.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/rankings/show.html.erb b/app/views/rankings/show.html.erb index b1a7db4..adbd4e7 100644 --- a/app/views/rankings/show.html.erb +++ b/app/views/rankings/show.html.erb @@ -12,7 +12,7 @@ <% position = @points.index(user.points) %>
  • <%= "#{position + 1}. " %> - <%= user.username %> + <%= link_to user.username, user_path(user) %> <%= number_with_precision(user.points) %>
  • From d4a36c186c4818fb9ab36571b4027c30277e6e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chwa=C5=82a?= Date: Sun, 23 Jun 2024 21:42:34 +0200 Subject: [PATCH 3/3] Add more links; show how many odds were accurate --- app/models/user.rb | 4 ++++ app/views/matches/show.html.erb | 4 ++-- app/views/rankings/show.html.erb | 2 +- app/views/users/show.html.erb | 8 ++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index c89c5a3..bdc9528 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,10 @@ def answer_by_match(match) answers.find_by(match: match) end + def accuracy + answers.map(&:point).count { |score| score > 0.0 } + end + def accept_invitation(params = {}) update(params.merge(invitation_accepted_at: DateTime.now)) end diff --git a/app/views/matches/show.html.erb b/app/views/matches/show.html.erb index fbe660e..9f97e2b 100644 --- a/app/views/matches/show.html.erb +++ b/app/views/matches/show.html.erb @@ -79,12 +79,12 @@ <% @users.each do |user| %> <% if @answers[user.id].blank? %> - <%= user.username %> + <%= link_to user.username, user_path(user) %> brak typu <% else %> - <%= user.username %> + <%= link_to user.username, user_path(user) %> <%= '1' if @answers[user.id].win_a? %> diff --git a/app/views/rankings/show.html.erb b/app/views/rankings/show.html.erb index adbd4e7..6853567 100644 --- a/app/views/rankings/show.html.erb +++ b/app/views/rankings/show.html.erb @@ -13,7 +13,7 @@
  • <%= "#{position + 1}. " %> <%= link_to user.username, user_path(user) %> - <%= number_with_precision(user.points) %> + <%= number_with_precision(user.points) %> (<%= "% 2d" % user.accuracy %> trafień)
  • <% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 496294c..aa8ca36 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -3,6 +3,10 @@ Głosy użytkownika <%= @user.username %> +

    + Liczba trafień: <%= @user.accuracy %> +

    +

    Szczegóły:

    <% @matches_started.group_by(&:start_date).each do |start_date, games| %> @@ -15,6 +19,7 @@ + @@ -47,6 +52,9 @@ + <% end %>
    1X X2 12Szczegóły
    <%= number_with_precision(match.not_tie) || '---' %> + <%= link_to "zobacz", match_path(match) %> +