1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Added views to delegate tournament permissions

This commit is contained in:
2016-01-07 19:55:15 +00:00
parent 216e60d2b8
commit dd9e19b43f
10 changed files with 157 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ class StaticPagesController < ApplicationController
tournaments_delegated = current_user.delegated_tournaments tournaments_delegated = current_user.delegated_tournaments
all_tournaments = tournaments_created + tournaments_delegated all_tournaments = tournaments_created + tournaments_delegated
@tournaments = all_tournaments.sort_by{|t| t.daysUntil} @tournaments = all_tournaments.sort_by{|t| t.daysUntil}
@schools = current_user.delegated_schools
end end
def not_allowed def not_allowed

View File

@@ -1,10 +1,30 @@
class TournamentsController < ApplicationController class TournamentsController < ApplicationController
before_action :set_tournament, only: [:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets] before_action :set_tournament, only: [:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets]
before_filter :check_access_manage, only: [:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] before_filter :check_access_manage, only: [:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches]
before_filter :check_access_destroy, only: [:destroy] before_filter :check_access_destroy, only: [:destroy,:delegate]
before_filter :check_for_matches, only: [:up_matches,:bracket,:all_brackets] before_filter :check_for_matches, only: [:up_matches,:bracket,:all_brackets]
def delegate
if params[:search]
@users = User.search(params[:search])
elsif params[:user]
@user = User.find(params[:user]["id"])
@delegate = TournamentDelegate.new
@delegate.user_id = @user.id
@delegate.tournament_id = @tournament.id
respond_to do |format|
if @delegate.save
format.html { redirect_to "/tournaments/#{@tournament.id}/delegate", notice: 'Delegated permissions added successfully' }
else
format.html { redirect_to "/tournaments/#{@tournament.id}/delegate", notice: 'There was an issue delegating permissions please try again' }
end
end
else
@users_delegates = @tournament.delegates
end
end
def matches def matches
@matches = @tournament.matches.sort_by{|m| m.bout_number} @matches = @tournament.matches.sort_by{|m| m.bout_number}
if @match if @match

View File

@@ -40,7 +40,7 @@ class Ability
end end
#Can manage school if tournament owner #Can manage school if tournament owner
can :manage, School do |school| can :manage, School do |school|
school.tournament.user.id == user.id school.tournament.user_id == user.id
end end
#Can manage school if tournament delegate #Can manage school if tournament delegate
can :manage, School do |school| can :manage, School do |school|

View File

@@ -1,2 +1,4 @@
class SchoolDelegate < ActiveRecord::Base class SchoolDelegate < ActiveRecord::Base
belongs_to :school
belongs_to :user
end end

View File

@@ -1,4 +1,4 @@
class TournamentDelegate < ActiveRecord::Base class TournamentDelegate < ActiveRecord::Base
# belongs_to :tournament belongs_to :tournament
# has_one :user belongs_to :user
end end

View File

@@ -2,10 +2,29 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
has_many :tournaments has_many :tournaments
has_many :delegated_tournaments, class_name: "TournamentDelegate" has_many :delegated_tournament_permissions, class_name: "TournamentDelegate"
has_many :delegated_schools, class_name: "SchoolDelegate" has_many :delegated_school_permissions, class_name: "SchoolDelegate"
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
def delegated_tournaments
tournaments_delegated = []
delegated_tournament_permissions.each do |t|
tournaments_delegated << t.tournament
end
tournaments_delegated
end
def delegated_schools
schools_delegated = []
delegated_school_permissions.each do |t|
schools_delegated << t.school
end
schools_delegated
end
def self.search(search)
where("email LIKE ?", "%#{search}%")
end
end end

View File

@@ -27,6 +27,8 @@
<td><%= link_to 'Show', tournament, :class=>"btn btn-default btn-sm" %> <td><%= link_to 'Show', tournament, :class=>"btn btn-default btn-sm" %>
<% if can? :manage, tournament %> <% if can? :manage, tournament %>
<%= link_to 'Edit', edit_tournament_path(tournament), :class=>"btn btn-primary btn-sm" %> <%= link_to 'Edit', edit_tournament_path(tournament), :class=>"btn btn-primary btn-sm" %>
<% end %>
<% if can? :destroy, tournament %>
<%= link_to 'Destroy', tournament, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"btn btn-danger btn-sm" %> <%= link_to 'Destroy', tournament, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"btn btn-danger btn-sm" %>
<% end %> <% end %>
</td> </td>
@@ -34,7 +36,38 @@
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<br>
<br>
<% if @schools.size > 0 %>
<h1>My Schools</h1>
<br>
<br>
<table class="table table-striped table-bordered table-condensed" id="tournamentList">
<thead>
<tr>
<th>Name</th>
<th>Tournament Name</th>
<th>Tournament Date</th>
<th></th>
</tr>
</thead>
<tbody>
<% @schools.each do |school| %>
<tr>
<td><%= school.name %></td>
<td><%= tournament.school.name %></td>
<td><%= tournament.school.date %></td>
<td><%= link_to 'Show', school, :class=>"btn btn-default btn-sm" %>
<% if can? :manage, school %>
<%= link_to 'Edit', edit_school_path(school), :class=>"btn btn-primary btn-sm" %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<br> <br>

View File

@@ -0,0 +1,65 @@
<%= link_to "Back to #{@tournament_name}", "/tournaments/#{@tournament_id}", :class=>"btn btn-default" %>
<br>
<br>
<% if @users %>
<h1>Search results</h1> <%= form_tag(tournament_delegate_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search users" %>
<%= submit_tag "Search" %>
<% end %>
<p>Search by email address</p>
</br>
</br>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>User Email</th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td>
<%= form_for user, :url => url_for(:controller => 'tournaments', :action => 'delegate', :method => "patch") do |f| %>
<%= f.hidden_field :id, :value => user.id %>
<% if can? :manage, @tournament %>
<%= submit_tag "Give permissions", :class=>"btn btn-success"%>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% if @users_delegates %>
<h1>Delegated users</h1> <%= form_tag(tournament_delegate_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search users" %>
<%= submit_tag "Search" %>
<% end %>
<p>Search by email address</p>
</br>
</br>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>User Email</th>
</tr>
</thead>
<tbody>
<% @users_delegates.each do |delegate| %>
<tr>
<td><%= delegate.user.email %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View File

@@ -31,6 +31,8 @@
<td><%= link_to 'Show', tournament, :class=>"btn btn-default btn-sm" %> <td><%= link_to 'Show', tournament, :class=>"btn btn-default btn-sm" %>
<% if can? :manage, tournament %> <% if can? :manage, tournament %>
<%= link_to 'Edit', edit_tournament_path(tournament), :class=>"btn btn-primary btn-sm" %> <%= link_to 'Edit', edit_tournament_path(tournament), :class=>"btn btn-primary btn-sm" %>
<% end %>
<% if can? :destroy, tournament %>
<%= link_to 'Destroy', tournament, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"btn btn-danger btn-sm" %> <%= link_to 'Destroy', tournament, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"btn btn-danger btn-sm" %>
<% end %> <% end %>
</td> </td>

View File

@@ -40,6 +40,11 @@ Wrestling::Application.routes.draw do
get 'tournaments/:id/up_matches' => 'tournaments#up_matches' get 'tournaments/:id/up_matches' => 'tournaments#up_matches'
get 'tournaments/:id/no_matches' => 'tournaments#no_matches' get 'tournaments/:id/no_matches' => 'tournaments#no_matches'
get 'tournaments/:id/matches' => 'tournaments#matches' get 'tournaments/:id/matches' => 'tournaments#matches'
get 'tournaments/:id/delegate' => 'tournaments#delegate', :as => :tournament_delegate
patch 'tournaments/:id/delegate' => 'tournaments#delegate', :as => :set_tournament_delegate
get 'schools/:id/delegate' => 'schools#delegate'
post 'schools/:id/delegate' => 'schools#delegate'
# Example of regular route: # Example of regular route:
# get 'products/:id' => 'catalog#view' # get 'products/:id' => 'catalog#view'