Skip to content

Commit

Permalink
add leader assistant post type
Browse files Browse the repository at this point in the history
  • Loading branch information
SepsiLaszlo committed Dec 22, 2022
1 parent 45d8fc8 commit b1504f4
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 34 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ group :development do
gem 'spring'
gem 'spring-commands-rspec'
gem 'rack-mini-profiler'
gem 'faker'
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ GEM
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faker (2.19.0)
i18n (>= 1.6, < 2)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
ffi (1.15.0)
Expand Down Expand Up @@ -331,6 +333,7 @@ DEPENDENCIES
em-http-request
exception_handler (~> 0.8.0.0)
factory_bot_rails
faker
jquery-rails
kaminari
listen
Expand Down
4 changes: 4 additions & 0 deletions app/models/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def leader?
has_post?(PostType::LEADER_POST_ID)
end

def leader_assistant?
has_post?(PostType::LEADER_ASSISTANT_ID)
end

def newbie?
has_post?(PostType::DEFAULT_POST_ID) && end_date.nil? && !archived?
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/post_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PostType < ApplicationRecord
PEK_ADMIN_ID = 66
NEW_MEMBER_ID = 104
EVALUATION_HELPER_ID = 1188
LEADER_ASSISTANT_ID = 1460

COMMON_TYPES = [
FINANCIAL_OFFICER_POST_ID,
Expand All @@ -34,7 +35,8 @@ class PostType < ApplicationRecord
DEFAULT_POST_ID,
PEK_ADMIN_ID,
NEW_MEMBER_ID,
EVALUATION_HELPER_ID
EVALUATION_HELPER_ID,
LEADER_ASSISTANT_ID
].freeze
scope :without_common, -> { where.not(id: COMMON_TYPES) }
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def leader_of?(group)
membership&.leader?
end

def leader_assistant_of?(group)
membership = membership_for(group)
membership&.leader_assistant?
end

def member_of?(group)
membership = membership_for(group)
membership&.active?
Expand Down
28 changes: 16 additions & 12 deletions app/policies/evaluation_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def edit?
alias destroy? edit?
alias copy_previous_principles? edit?


alias update_comments? show?

def update_point_request?
Expand All @@ -41,6 +40,7 @@ def cancel_point_request?
def update_entry_request?
update_request?(entry_request_status)
end

alias edit_justification? update_entry_request?

def submit_entry_request?
Expand Down Expand Up @@ -71,7 +71,7 @@ def update_request?(request_status)
def submit_request?(request_status)
return false unless submittable_request?(request_status)

leader_of_the_group? || pek_admin?
leader_of_the_group? || leader_assistant_of_the_group? || pek_admin?
end

def submittable_request?(request_status)
Expand All @@ -86,21 +86,25 @@ def cancel_request?(request_status)
return false unless application_season?
return false unless request_status == Evaluation::NOT_YET_ASSESSED

leader_of_the_group? || pek_admin?
leader_of_the_group? || leader_assistant_of_the_group? || pek_admin?
end

def leader_of_the_group?
cache { user.leader_of?(evaluation.group) }
user.leader_of?(evaluation.group)
end

def leader_assistant_of_the_group?
user.leader_assistant_of?(evaluation.group)
end

def evaluation_helper_of_the_group?
cache { user.evaluation_helper_of?(evaluation.group) }
user.evaluation_helper_of?(evaluation.group)
end

def leader_of_the_resort?
return false unless group_is_a_resort_member?

cache { user.leader_of?(evaluation.group.parent) }
user.leader_of?(evaluation.group.parent)
end

def leader_in_the_resort?
Expand All @@ -125,7 +129,7 @@ def evaluation_helper_in_the_resort?
end

def group_is_a_resort_member?
cache { Group.resorts.include?(evaluation.group.parent) }
Group.resorts.include?(evaluation.group.parent)
end

def group_is_a_resort?
Expand All @@ -142,26 +146,26 @@ def evaluation_helper_at_resort?
end

def rvt_member?
cache { user.roles.rvt_member? }
user.roles.rvt_member?
end

def rvt_leader?
cache { user.roles.rvt_leader? }
user.roles.rvt_leader?
end

def point_request_status
cache { evaluation.point_request_status }
evaluation.point_request_status
end

def entry_request_status
cache { evaluation.entry_request_status }
evaluation.entry_request_status
end

def evaluation
record
end

def can_update_active_request?
leader_of_the_group? || evaluation_helper_of_the_group? || pek_admin?
leader_of_the_group? || leader_assistant_of_the_group? || evaluation_helper_of_the_group? || pek_admin?
end
end
6 changes: 5 additions & 1 deletion app/policies/group_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create?
alias new? create?

def manage_memberships?
leader? || evaluation_helper?
leader? || leader_assistant? || evaluation_helper?
end

alias manage_posts? manage_memberships?
Expand All @@ -32,6 +32,10 @@ def leader?
user.leader_of?(group)
end

def leader_assistant?
user.leader_assistant_of?(group)
end

def evaluation_helper?
user.evaluation_helper_of?(group)
end
Expand Down
12 changes: 8 additions & 4 deletions app/policies/post_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class PostPolicy < ApplicationPolicy
def create?
return false unless group&.has_post_type?(post_type.id)
return true if leader?
return true if evaluation_helper? &&
(group.own_post_types.include?(post_type) ||
post_type.id == PostType::NEW_MEMBER_ID)
return true if (evaluation_helper? || leader_assistant?) &&
(group.own_post_types.include?(post_type) ||
post_type.id == PostType::NEW_MEMBER_ID)

false
end
Expand All @@ -25,7 +25,11 @@ def post_type

def leader?
user.leader_of?(group)
end
end

def leader_assistant?
user.leader_assistant_of?(group)
end

def evaluation_helper?
user.evaluation_helper_of?(group)
Expand Down
6 changes: 5 additions & 1 deletion app/policies/sub_group_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def index?
alias show? index?

def create?
return true if leader_of_the_group?
return true if leader_of_the_group? || leader_assistant_of_the_group?
end

alias edit? create?
Expand All @@ -33,6 +33,10 @@ def leader_of_the_group?
membership.present? && membership.has_post?(PostType::LEADER_POST_ID)
end

def leader_assistant_of_the_group?
membership.present? && membership.has_post?(PostType::LEADER_ASSISTANT_ID)
end

def membership
user.membership_for(sub_group.group)
end
Expand Down
6 changes: 5 additions & 1 deletion app/policies/sub_group_principle_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SubGroupPrinciplePolicy < ApplicationPolicy
alias principle record

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

alias create? index?
Expand All @@ -13,6 +13,10 @@ def leader_of_the_group?
membership.present? && membership.has_post?(PostType::LEADER_POST_ID)
end

def leader_assistant_of_the_group?
membership.present? && membership.has_post?(PostType::LEADER_ASSISTANT_ID)
end

def admin_of_the_sub_group?
sub_group_membership.present? && sub_group_membership.admin?
end
Expand Down
79 changes: 65 additions & 14 deletions spec/policies/evaluation_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
RSpec.describe EvaluationPolicy, type: :policy do
subject { described_class.new(user, evaluation) }

let(:evaluation) { create(:evaluation) }
let(:evaluation_actions) { %i[show current table edit update edit_justification] }
let(:evaluation) { create(:evaluation) }
let(:evaluation_actions) { %i[show current table edit update edit_justification] }
let(:evaluation_view_actions) { evaluation_actions - %i[edit update edit_justification] }
let(:point_request_actions) { %i[submit_point_request cancel_point_request update_point_request] }
let(:entry_request_actions) { %i[submit_entry_request cancel_entry_request update_entry_request] }
let(:submit_actions) { %i[submit_point_request submit_entry_request] }
let(:cancel_actions) { %i[cancel_point_request cancel_entry_request] }
let(:update_request_actions) { %i[update_point_request update_entry_request] }
let(:update_comment_actions) { %i[update_comments] + evaluation_view_actions }
let(:all_action) { entry_request_actions + point_request_actions + evaluation_actions }
let(:point_request_actions) { %i[submit_point_request cancel_point_request update_point_request] }
let(:entry_request_actions) { %i[submit_entry_request cancel_entry_request update_entry_request] }
let(:submit_actions) { %i[submit_point_request submit_entry_request] }
let(:cancel_actions) { %i[cancel_point_request cancel_entry_request] }
let(:update_request_actions) { %i[update_point_request update_entry_request] }
let(:update_comment_actions) { %i[update_comments] + evaluation_view_actions }
let(:all_action) { entry_request_actions + point_request_actions + evaluation_actions }

context 'when application season' do
include_context 'application season'
Expand All @@ -36,13 +36,36 @@
end
end

context 'and the user is the group leader assistant' do
let(:user) do
evaluation.group.memberships.select(&:leader_assistant?).first.user
end

it { is_expected.to permit_actions(all_action - cancel_actions) }
it { is_expected.to permit_actions(update_comment_actions) }
it { is_expected.to forbid_actions(cancel_actions) }

context "and the request is submitted" do
before(:each) do
evaluation.point_request_status = Evaluation::NOT_YET_ASSESSED
evaluation.entry_request_status = Evaluation::NOT_YET_ASSESSED
end

it { is_expected.to permit_actions(cancel_actions) }
it { is_expected.to forbid_actions(submit_actions) }

it { is_expected.to permit_actions(evaluation_view_actions) }
it { is_expected.to forbid_actions(update_request_actions) }
end
end

context "and the user is the evaluation helper of the group" do
let(:user) do
evaluation.group.memberships.select(&:evaluation_helper?).first.user
end

it { is_expected.to permit_actions(evaluation_view_actions + update_request_actions + [:edit_justification] ) }
it { is_expected.to forbid_actions(all_action - evaluation_view_actions - update_request_actions - [:edit_justification]) }
it { is_expected.to permit_actions(evaluation_view_actions + update_request_actions + [:edit_justification]) }
it { is_expected.to forbid_actions(all_action - evaluation_view_actions - update_request_actions - [:edit_justification]) }
end

context 'and the user is a leader of another the group' do
Expand All @@ -52,10 +75,10 @@
end

context 'and the user is a leader of another group in the resort' do
let(:user) { evaluation.group.parent.children.select{ |g| g != evaluation.group }.first.leader.user }
let(:user) { evaluation.group.parent.children.select { |g| g != evaluation.group }.first.leader.user }

it { is_expected.to permit_actions(evaluation_view_actions) }
it { is_expected.to forbid_actions(all_action - evaluation_view_actions) }
it { is_expected.to permit_actions(evaluation_view_actions) }
it { is_expected.to forbid_actions(all_action - evaluation_view_actions) }
end

context 'and the group has no parents' do
Expand Down Expand Up @@ -156,6 +179,34 @@
end
end

context "and the user is the group leader assistant" do
let(:user) do
evaluation.group.memberships.select(&:leader_assistant?).first.user
end

context "and the evaluation is not yet assessed" do
before(:each) do
evaluation.point_request_status = Evaluation::NOT_YET_ASSESSED
evaluation.entry_request_status = Evaluation::NOT_YET_ASSESSED
end

it { is_expected.to permit_actions(evaluation_view_actions) }
it { is_expected.to forbid_actions(all_action - evaluation_view_actions) }
end

context "and the point request is rejected" do
before { evaluation.point_request_status = Evaluation::REJECTED }

it { is_expected.to permit_actions(point_request_actions - cancel_actions) }
end

context "when the entry_request is rejected" do
before { evaluation.entry_request_status = Evaluation::REJECTED }

it { is_expected.to permit_actions(entry_request_actions - cancel_actions) }
end
end

context "when the user is the resort leader" do
let(:user) { evaluation.group.parent.leader.user }

Expand Down
11 changes: 11 additions & 0 deletions spec/policies/group_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
expect(subject).to permit(user, group)
end
end

context 'when the user is the group leader assistant' do
let(:membership) { create(:membership, :leader) }
let(:user) { membership.user }
let(:group) { membership.group }

it 'grants access' do
expect(subject).to permit(user, group)
end
end

context 'when the user is not the group leader' do
let(:membership) { create(:membership) }
let(:user) { membership.user }
Expand Down
8 changes: 8 additions & 0 deletions spec/policies/post_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
end
end

context "when the current user is the group leader assistant" do
let(:user) { membership.group.memberships.first(&:leader_assistant?).user }

it "is permitted" do
expect(subject).to permit(user, post.reload)
end
end

context "when the user is not a member" do
let(:user) { create(:user) }
it "is forbidden" do
Expand Down
Loading

0 comments on commit b1504f4

Please sign in to comment.