Skip to content

Commit

Permalink
Merge pull request #438 from kir-dev/enable-sub-group-admins-to-see-a…
Browse files Browse the repository at this point in the history
…ll-evaluations

enable sub group admins to see each other evaluations
  • Loading branch information
SepsiLaszlo authored Jun 19, 2024
2 parents 149c894 + 8fb94bc commit affb031
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
6 changes: 3 additions & 3 deletions app/controllers/sub_group_principles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class SubGroupPrinciplesController < ApplicationController
before_action :require_sssl

def index
authorize Principle.new(sub_group: @sub_group), policy_class: SubGroupPrinciplePolicy
authorize @sub_group, policy_class: SubGroupPrinciplePolicy
@evaluation = current_evaluation
@principles = @evaluation.principles.where(sub_group: @sub_group).order(:type, :id)
@can_edit = true
@can_edit = SubGroupPrinciplePolicy.new(current_user, @sub_group).edit?
end

def update
Expand All @@ -26,7 +26,7 @@ def create
@principle.evaluation = @evaluation
@principle.sub_group = @sub_group

authorize @principle, policy_class: SubGroupPrinciplePolicy
authorize @sub_group, policy_class: SubGroupPrinciplePolicy
# Todo add error handling
@principle.save
@can_edit = true
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/sub_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def show
authorize @sub_group
@policy = policy(@sub_group)
@sub_group_memberships = @sub_group.sub_group_memberships.includes(membership: :user)
@sub_group_principle_policy = SubGroupPrinciplePolicy.new(current_user, Principle.new(sub_group: @sub_group))
@sub_group_principle_policy = SubGroupPrinciplePolicy.new(current_user, @sub_group)
@sub_group_evaluation_policy = SubGroupEvaluationPolicy.new(current_user, @sub_group)
end

# GET /sub_groups/new
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/sub_group_policy_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module SubGroupPolicyHelper

private

def admin_for_any_sub_group?
SubGroupMembership.joins(:sub_group, :membership).where('sub_groups.group_id': sub_group.group.id,
'memberships.user_id': user.id,
admin: true).present?
end
end
10 changes: 8 additions & 2 deletions app/policies/sub_group_evaluation_policy.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# frozen_string_literal: true

class SubGroupEvaluationPolicy < ApplicationPolicy
include SubGroupPolicyHelper

alias sub_group record

def table?
return false if off_season?

EvaluationPolicy.new(user, evaluation).table? || admin_of_the_sub_group?
EvaluationPolicy.new(user, evaluation).table? || admin_of_the_sub_group? || admin_for_any_sub_group?
end

alias update_point_request? table?
def update_point_request?
return false if off_season?

EvaluationPolicy.new(user, evaluation).edit? || admin_of_the_sub_group?
end

def update_entry_request?
EvaluationPolicy.new(user, evaluation).update_entry_request?
Expand Down
18 changes: 10 additions & 8 deletions app/policies/sub_group_principle_policy.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
class SubGroupPrinciplePolicy < ApplicationPolicy
alias principle record
include SubGroupPolicyHelper

alias sub_group record

def index?
leader_of_the_group? || leader_assistant_of_the_group? || admin_of_the_sub_group? || admin_for_any_sub_group?
end

def edit?
leader_of_the_group? || leader_assistant_of_the_group? || admin_of_the_sub_group?
end

alias create? index?
alias update? create?
alias destroy? create?
alias create? edit?
alias update? edit?
alias destroy? edit?

def leader_of_the_group?
membership.present? && membership.has_post?(PostType::LEADER_POST_ID)
Expand All @@ -21,10 +27,6 @@ def admin_of_the_sub_group?
sub_group_membership.present? && sub_group_membership.admin?
end

def sub_group
@sub_group ||= principle.sub_group
end

def membership
@membership ||= user.membership_for(sub_group.group)
end
Expand Down
22 changes: 11 additions & 11 deletions app/views/sub_groups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

<div class="uk-panel uk-panel-box">
<h3>Tagok</h3>
<% if @policy.edit? %>
<%= link_to 'Szerkesztés', edit_group_sub_group_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %>
<% end %>
<% if @sub_group_principle_policy.index? %>
<%= link_to 'Pontozási elvek', group_sub_group_principles_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %>
<% end %>
<% if @sub_group_evaluation_policy.table? %>
<%= link_to 'Pontozás', group_sub_group_evaluation_table_path(current_group, @sub_group, current_group.current_evaluation), class: 'uk-button uk-button-primary' %>
<% end %>
<br/>
<%= link_to 'Vissza', group_sub_groups_path(current_group), class: 'uk-button uk-margin-top' %>
<table class="uk-table uk-table-striped uk-table-condensed">
<thead>
<th></th>
Expand Down Expand Up @@ -41,16 +52,5 @@
<% end %>
</tbody>
</table>

<% if @policy.edit? %>
<%= link_to 'Szerkesztés', edit_group_sub_group_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %>
<% end %>
<% if @sub_group_principle_policy.index? &&
SystemAttribute.season.value != SystemAttribute::OFFSEASON &&
current_group.current_evaluation&.semester == SystemAttribute.semester.to_s %>
<%= link_to 'Pontozási elvek', group_sub_group_principles_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %>
<%= link_to 'Pontozás', group_sub_group_evaluation_table_path(current_group, @sub_group, current_group.current_evaluation), class: 'uk-button uk-button-primary' %>
<% end %>
<br/>
<%= link_to 'Vissza', group_sub_groups_path(current_group), class: 'uk-button uk-margin-top' %>
</div>
7 changes: 4 additions & 3 deletions spec/policies/sub_group_principle_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'rails_helper'

RSpec.describe SubGroupPrinciplePolicy, type: :policy do
subject { described_class.new(user, sub_group_principle) }
subject { described_class.new(user, sub_group) }

let(:all_action) { [:index, :create, :update, :destroy] }

let(:sub_group_principle) { create(:sub_group_principle) }
let(:group) { sub_group_principle.sub_group.group }
let(:sub_group_principle) { create(:sub_group_principle)}
let(:sub_group) { sub_group_principle.sub_group}
let(:group) { sub_group.group }
context 'when the user is the group leader' do
let(:user) { group.leader.user }

Expand Down

0 comments on commit affb031

Please sign in to comment.