Skip to content

Commit

Permalink
Team join/leave improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abartov committed Dec 2, 2024
1 parent 0d5666b commit d30609a
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 24 deletions.
6 changes: 3 additions & 3 deletions app/controllers/team_memberships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class TeamMembershipsController < InheritedResources::Base

before_action :set_tm, only: %i[show edit update destroy]

def create
Expand Down Expand Up @@ -34,7 +33,7 @@ def destroy

def leave
@team_membership = TeamMembership.find(params[:id])
if current_user == @team_membership.user || current_user.admin_or_editor?
if current_user.id == @team_membership.user_id || current_user.admin_or_editor?
@the_id = @team_membership.id
@team_membership.destroy
else
Expand All @@ -53,6 +52,7 @@ def set_tm
end

def allowed?
current_user.admin_or_editor? || current_user == User.find(team_membership_params[:user_id])
relevant_user_id = params[:team_membership].present? ? team_membership_params[:user_id].to_i : @team_membership.user_id
current_user.admin_or_editor? || current_user.id == relevant_user_id
end
end
2 changes: 1 addition & 1 deletion app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TeamsController < ApplicationController
before_action :set_team, only: %i[show edit update destroy mass_message]
before_action :require_admin
before_action :require_admin, only: %i[new create edit update destroy mass_message]

# GET /teams
def index
Expand Down
6 changes: 3 additions & 3 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Team < ApplicationRecord
scope :open, -> { where(open: true) }

def has_member?(user)
users.joins(:team_memberships).where(team_memberships: {left: nil}).include?(user)
team_memberships.where(left: nil, user_id: user.id).exists?
end

def has_task?(task)
Expand All @@ -18,7 +18,7 @@ def has_task?(task)

# return users whose role in the team is 'lead'
def team_leads
users.where(team_memberships: {team_role: TeamMembership.team_roles[:lead], left: nil})
users.where(team_memberships: { team_role: TeamMembership.team_roles[:lead], left: nil })
end

def team_lead_memberships
Expand All @@ -38,7 +38,7 @@ def all_team_memberships_ever
end

def user_ids
users.where(team_memberships: {left: nil}).map(&:id)
users.where(team_memberships: { left: nil }).map(&:id)
end

def team_lead_ids
Expand Down
5 changes: 4 additions & 1 deletion app/views/team_memberships/create.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
$('.membership').html("<%= escape_javascript(render partial: 'teams/memberships', locals: { edit: true}) %>");
$('.membership').html("<%= escape_javascript(render partial: 'teams/memberships', locals: { edit: true}) %>");
if($('#team_show').length > 0 || $('#public_profile').length > 0) {
location.reload();
}
5 changes: 4 additions & 1 deletion app/views/team_memberships/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
$('#<%= @the_id %>').remove();
$('#<%= @the_id %>').remove();
if($('#team_show').length > 0 || $('#public_profile').length > 0) {
location.reload();
}
5 changes: 4 additions & 1 deletion app/views/team_memberships/leave.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
$('#<%= @the_id %>').remove();
$('#<%= @the_id %>').remove();
if($('#public_profile').length > 0) {
location.reload();
}
2 changes: 1 addition & 1 deletion app/views/teams/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
= f.label t('.open')
%br
= f.label t('.targetdate')
= f.date_select :targetdate, order: [:day, :month, :year], start_year: Date.today.year, end_year: Date.today.year + 10, locale: :he, use_month_numbers: true
= f.date_select :targetdate, order: [:day, :month, :year], start_year: Date.today.year, end_year: Date.today.year + 10, locale: :he, use_month_numbers: true, include_blank: true
- unless @team.new_record?
.membership
= render partial: 'memberships', locals: {f: f, edit: true}
Expand Down
16 changes: 9 additions & 7 deletions app/views/teams/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
%th= t('.number_of_tasks_approved')
%th= t('.progress')
%th= t('activerecord.attributes.team.targetdate')
%th= t('.actions')
- if current_user.is_admin?
%th= t('.actions')

%tbody
- @teams.each do |team|
Expand All @@ -33,11 +34,12 @@
%td= done.to_s
%td= done > 0 ? "#{(done.to_f / total.to_f * 100).round(2)}%" : ""
%td= team.targetdate
%td
= link_to t(:edit), edit_team_path(team)
= ' | '
= link_to t(:delete), team, method: :delete, data: { confirm: t(:are_you_sure) }
- if current_user.is_admin?
%td
= link_to t(:edit), edit_team_path(team)
= ' | '
= link_to t(:delete), team, method: :delete, data: { confirm: t(:are_you_sure) }

%br

= link_to t('.new'), new_team_path
- if current_user.is_admin?
= link_to t('.new'), new_team_path
4 changes: 2 additions & 2 deletions app/views/teams/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
%p#notice= notice

%h1= "#{t(:team)}: #{@team.name}"
%h1#team_show= "#{t(:team)}: #{@team.name}"
%p
= @team.description
%h2= t(:stats)
%ul
%li
= t('.members_count', count: @team.team_memberships.count)
- if @team.has_member?(current_user)
%span{style: 'background-color: lightgreen;'}= t('.including_you')
%span{id: @team.team_memberships.where(user_id: current_user.id).first.id, style: 'background-color: lightgreen;'}= t('.including_you')
%li= t('.tasks_count', count: @team.tasks.count)
%li= t('.completed_count', count: @team.tasks.approved.count + @team.tasks.other_task_creat.count)
= render partial: 'memberships', locals: {edit: false}
Expand Down
3 changes: 2 additions & 1 deletion app/views/users/_public_profile.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.box
.box#public_profile
.boxhead
= _("User details")
.boxcontainer
Expand Down Expand Up @@ -35,6 +35,7 @@
.midtitle.teams
%h4= t('tabs.teams')
.boxbody
%b= link_to t(:to_all_teams), '/teams'
%ul
- @user.team_memberships.each do |tm|
%li{id: tm.id}
Expand Down
3 changes: 2 additions & 1 deletion config/locales/he.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ he:
loading: הנתונים נטענים...
'yes': כן
'no': לא
volunteer_prefs: העדפות התנדבות
volunteer_prefs: העדפות התנדבות
to_all_teams: לרשימת כל הצוותים
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

ActiveRecord::Schema.define(version: 2024_11_08_052206) do

create_table "api_users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "api_users", charset: "latin1", force: :cascade do |t|
t.string "api_key"
t.string "email"
t.datetime "created_at", precision: 6, null: false
Expand All @@ -31,7 +31,7 @@
t.index ["user_id"], name: "index_assignment_histories_on_user_id"
end

create_table "audit_logs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "audit_logs", charset: "latin1", force: :cascade do |t|
t.string "backtrace"
t.string "data"
t.bigint "api_user_id"
Expand Down

0 comments on commit d30609a

Please sign in to comment.