Skip to content

Commit

Permalink
Merge pull request #46 from cepe/master
Browse files Browse the repository at this point in the history
Add accurancy, links to user profile; user odds
  • Loading branch information
qarol authored Jun 23, 2024
2 parents 4d5e17c + 9bab6e6 commit b0144cb
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/matches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<% @users.each do |user| %>
<% if @answers[user.id].blank? %>
<tr>
<td class="text-nowrap"><%= user.username %></td>
<td class="text-nowrap"><%= link_to user.username, user_path(user) %></td>
<td class="text-nowrap" colspan="6">brak typu</td>
</tr>
<% else %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/rankings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<% position = @points.index(user.points) %>
<li class="list-group-item <%= 'active' if Current.user == user %>">
<%= "#{position + 1}. " %>
<%= user.username %>
<strong class="float-right"><%= number_with_precision(user.points) %></strong>
<%= link_to user.username, user_path(user) %>
<strong class="float-right"><%= number_with_precision(user.points) %> (<%= "% 2d" % user.accuracy %> trafień)</strong>
<div class="clearfix"></div>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<% User.includes(:invited_by).order(:username).each_with_index do |user, index| %>
<tr>
<td class="align-middle"><%= index + 1 %></td>
<td class="align-middle"><%= user.username %></td>
<td class="align-middle"><%= link_to user.username, user_path(user) %></td>
<td class="align-middle"><%= user.invited_by_username %></td>
<td class="align-middle">
<%= user_status(user) %>
Expand Down
64 changes: 64 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<%= render partial: 'shared/menu', locals: { s: 'users' } %>
<h2>
<i class="fa fa-futbol-o"></i>
Głosy użytkownika <%= @user.username %>
</h2>
<h3>
Liczba trafień: <%= @user.accuracy %>
</h3>
<h3>Szczegóły:</h3>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<% @matches_started.group_by(&:start_date).each do |start_date, games| %>
<thead>
<tr>
<th class="text-nowrap" colspan="3"><%= l(start_date, format: :only_date) %></th>
<th class="col-xs-1 text-nowrap text-center">1</th>
<th class="col-xs-1 text-nowrap text-center">X</th>
<th class="col-xs-1 text-nowrap text-center">2</th>
<th class="col-xs-1 text-nowrap text-center">1X</th>
<th class="col-xs-1 text-nowrap text-center">X2</th>
<th class="col-xs-1 text-nowrap text-center">12</th>
<th class="col-xs-1 text-nowrap text-center">Szczegóły</th>
</tr>
</thead>
<tbody>
<% games.each do |match| %>
<tr >
<td class="col-xs-1 text-nowrap text-center">
<%= l(match.start, format: :only_time) %>
</td>
<td class="col-xs-1 text-nowrap text-center"><%= "#{match.result_a}:#{match.result_b}" %></td>
<td class="text-nowrap">
<%= match.team_a %>
-
<%= match.team_b %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.win_a? %>">
<%= number_with_precision(match.win_a) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.tie? %>">
<%= number_with_precision(match.tie) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.win_b? %>">
<%= number_with_precision(match.win_b) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.win_tie_a? %>">
<%= number_with_precision(match.win_tie_a) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.win_tie_b? %>">
<%= number_with_precision(match.win_tie_b) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center <%= 'info' if (match.answers & @answers).first&.not_tie? %>">
<%= number_with_precision(match.not_tie) || '---' %>
</td>
<td class="col-xs-1 text-nowrap text-center">
<%= link_to "zobacz", match_path(match) %>
</td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
</div>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
post :fin
Expand Down

0 comments on commit b0144cb

Please sign in to comment.