Skip to content

Commit

Permalink
add basic SubGroup system
Browse files Browse the repository at this point in the history
  • Loading branch information
SepsiLaszlo committed Dec 13, 2022
1 parent ede3f19 commit 244f928
Show file tree
Hide file tree
Showing 34 changed files with 820 additions and 42 deletions.
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,5 @@ group :development do
# Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-commands-rspec'

gem 'better_errors'
gem 'binding_of_caller'
gem 'rack-mini-profiler'
end
13 changes: 3 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,11 @@ GEM
annotate (3.0.3)
activerecord (>= 3.2, < 7.0)
rake (>= 10.4, < 14.0)
better_errors (2.8.0)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootsnap (1.7.1)
msgpack (~> 1.0)
brakeman (4.7.0)
builder (3.2.4)
byebug (11.0.1)
coderay (1.1.3)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
cookiejar (0.3.3)
Expand All @@ -90,7 +83,6 @@ GEM
database_cleaner-active_record (1.8.0)
activerecord
database_cleaner (~> 1.8.0)
debug_inspector (0.0.3)
diff-lcs (1.4.4)
docile (1.3.2)
dotenv (2.7.6)
Expand Down Expand Up @@ -202,6 +194,8 @@ GEM
rspec-rails (>= 3.0.0)
racc (1.6.0)
rack (2.2.3)
rack-mini-profiler (3.0.0)
rack (>= 1.2.0)
rack-protection (2.1.0)
rack
rack-test (1.1.0)
Expand Down Expand Up @@ -328,8 +322,6 @@ PLATFORMS
DEPENDENCIES
activity_notification (~> 2.2, >= 2.2.1)
annotate
better_errors
binding_of_caller
bootsnap (>= 1.1.0)
brakeman
byebug
Expand All @@ -350,6 +342,7 @@ DEPENDENCIES
puma
pundit (~> 2.1)
pundit-matchers (~> 1.6)
rack-mini-profiler
rails (~> 6.0.3.6)
rails-controller-testing
render_anywhere
Expand Down
12 changes: 1 addition & 11 deletions app/controllers/evaluations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class EvaluationsController < ApplicationController
include EvaluationsControllerHelper
before_action :validate_correct_group
before_action :set_search, only: :table

Expand Down Expand Up @@ -124,15 +125,4 @@ def new_evaluation

evaluation
end

def set_search
@search = OpenStruct.new(term: params[:term]&.downcase, start_with?: !!params[:start_with])
end

def search_users
return if @search.term.blank?

query = @search.start_with? ? "#{@search.term}%" : "%#{@search.term}%"
@users = @users.where("full_name like ?", query)
end
end
1 change: 1 addition & 0 deletions app/controllers/principles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PrinciplesController < EvaluationsController
def index
@evaluation = current_evaluation
authorize @evaluation, :show?
@principles = @evaluation.principles.order(:sub_group_id, :type, :id)

@can_edit = policy(@evaluation).edit?
end
Expand Down
32 changes: 32 additions & 0 deletions app/controllers/sub_group_evaluations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class SubGroupEvaluationsController < ApplicationController
include EvaluationsControllerHelper
before_action :set_search, only: :table

def table
@point_details = PointDetail.joins(point_request: :evaluation)
.where(evaluations: { id: current_evaluation.id })
.includes(:principle)
@evaluation = current_evaluation
@group = @evaluation.group

@ordered_principles = @evaluation.principles
.where(sub_group_id: params[:sub_group_id])
.order(:type, :id)

point_eligible_user_ids = @evaluation.group.point_eligible_memberships.map(&:user_id)
@users = User.with_full_name.where(id: point_eligible_user_ids)
.includes(:entry_requests,
point_requests: [point_details: [:point_detail_comments, :principle]])
search_users
@users = @users.order(:full_name).page(params[:page])
@users_for_pagination = @users
@users = EvaluationUserDecorator.decorate_collection(@users, context: { evaluation: @evaluation })

@evaluation_point_calculator = EvaluationPointCalculator.new(@users)
evaluation_policy = policy(@evaluation)
@update_point_request = evaluation_policy.update_point_request?
@update_entry_request = evaluation_policy.update_entry_request?

render 'evaluations/table'
end
end
58 changes: 58 additions & 0 deletions app/controllers/sub_group_principles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class SubGroupPrinciplesController < ApplicationController
before_action :set_principle, only: %i[update destroy]
before_action :validate_correct_evaluation, only: %i[update destroy]
# before_action :authorize_evaluation, except: [:index]
before_action :set_sub_group, only: [:index, :create, :update]

def index
@evaluation = current_evaluation
authorize @evaluation, :show?

@principles = @evaluation.principles.where(sub_group: @sub_group).order(:type, :id)
@can_edit = true
end

def update
@principle.update(principle_params)

render 'principles/update'
end

def create
@principle = Principle.new(principle_params)
@evaluation = current_evaluation
@principle.evaluation = @evaluation
@principle.sub_group = @sub_group
@principle.save

render 'principles/create'
end

def destroy
@principle.destroy

render 'principles/destroy'
end

private

def set_principle
@principle = Principle.find(params[:id])
end

def validate_correct_evaluation
render forbidden_page unless current_evaluation == @principle.evaluation
end

def principle_params
params.require(:principle).permit(:type, :name, :max_per_member, :description)
end

def current_evaluation
current_group.current_evaluation
end

def set_sub_group
@sub_group = SubGroup.find(params[:sub_group_id])
end
end
93 changes: 93 additions & 0 deletions app/controllers/sub_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
class SubGroupsController < ApplicationController
before_action :set_sub_group, only: [:show, :edit, :update, :destroy, :join, :leave]

# GET /sub_groups
def index
@sub_groups = SubGroup.where(group: current_group)
sub_group_memberships = SubGroupMembership.where(membership: current_membership)
@current_user_subgroup_ids = sub_group_memberships.map(&:sub_group_id)
end

# GET /sub_groups/1
def show
@sub_group_memberships = @sub_group.sub_group_memberships.includes(membership: :user)
end

# GET /sub_groups/new
def new
@sub_group = SubGroup.new
end

# GET /sub_groups/1/edit
def edit
end

# POST /sub_groups
def create
@sub_group = SubGroup.new(sub_group_params)
@sub_group.group_id = params[:group_id]

if @sub_group.save
redirect_to group_sub_group_path(current_group, @sub_group),
notice: 'Sub group was successfully created.'
else
render :new
end
end

# PATCH/PUT /sub_groups/1
def update
if @sub_group.update(sub_group_params)
redirect_to group_sub_group_path(current_group, @sub_group),
notice: 'Sub group was successfully updated.'
else
render :edit
end
end

# DELETE /sub_groups/1
def destroy
ActiveRecord::Base.transaction do
@sub_group.sub_group_memberships.destroy_all
@sub_group.principles.update_all(sub_group_id: nil)
@sub_group.destroy
end
redirect_to group_sub_groups_path(current_group), notice: 'Sub group was successfully destroyed.'
end

def join
SubGroupMembership.create!(membership: current_membership, sub_group: @sub_group)

redirect_to group_sub_groups_path(current_group)
end

def leave
sub_group_membership = SubGroupMembership.find_by(
membership: current_membership,
sub_group: @sub_group)
sub_group_membership.destroy!

redirect_to group_sub_groups_path(current_group)
end

def set_admin
@sub_group_membership = SubGroupMembership.find(params[:sub_group_membership_id])
@sub_group_membership.update!(admin: params[:admin])
end

private

# Use callbacks to share common setup or constraints between actions.
def set_sub_group
@sub_group = SubGroup.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def sub_group_params
params.require(:sub_group).permit(:name)
end

def current_membership
@current_membership ||= current_user.membership_for(current_group)
end
end
16 changes: 16 additions & 0 deletions app/helpers/evaluations_controller_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module EvaluationsControllerHelper
private

def set_search
@search = OpenStruct.new(term: params[:term]&.downcase, start_with?: !!params[:start_with])
end

def search_users
return if @search.term.blank?

query = @search.start_with? ? "#{@search.term}%" : "%#{@search.term}%"
@users = @users.where("full_name like ?", query)
end
end
6 changes: 6 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def inactive?

!active_in_last_two_semesters?
end

include ApplicationHelper

def point_eligible_memberships
memberships.active.includes(:user)
.sort { |m1, m2| hu_compare(m1.user.full_name, m2.user.full_name) }
Expand Down Expand Up @@ -155,6 +157,10 @@ def primary_memberships
Membership.where(id: primary_membership_ids).active
end

def current_evaluation
evaluations.find_by(semester: SystemAttribute.semester.to_s)
end

private

def founded_less_than_a_year_ago?
Expand Down
2 changes: 2 additions & 0 deletions app/models/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Membership < ApplicationRecord
belongs_to :user
has_many :posts
has_many :post_types, through: :posts
has_many :sub_group_memberships
has_many :sub_groups, through: :sub_group_memberships

before_create :set_defaults

Expand Down
7 changes: 7 additions & 0 deletions app/models/principle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
# name :string
# type :string
# evaluation_id :integer
# sub_group_id :bigint
#
# Indexes
#
# index_principles_on_sub_group_id (sub_group_id)
#
# Foreign Keys
#
# fk_rails_... (evaluation_id => evaluations.id)
# fk_rails_... (sub_group_id => sub_groups.id)
#

class Principle < ApplicationRecord
self.inheritance_column = nil # So it won't try to interpret the type column as STI

belongs_to :evaluation
belongs_to :sub_group, optional: true
has_many :point_details, dependent: :destroy, inverse_of: :principle

delegate :group, to: :evaluation
Expand Down
27 changes: 27 additions & 0 deletions app/models/sub_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: sub_groups
#
# id :bigint not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# group_id :bigint not null
#
# Indexes
#
# index_sub_groups_on_group_id (group_id)
#
# Foreign Keys
#
# fk_rails_... (group_id => groups.id)
#

class SubGroup < ApplicationRecord
belongs_to :group
has_many :sub_group_memberships
has_many :memberships, through: :sub_group_memberships
has_many :principles
validates :name, presence: true
validates :group_id, presence: true
end
Loading

0 comments on commit 244f928

Please sign in to comment.