Verified Commit e01c4281 authored by Markus Koller's avatar Markus Koller 🦊
Browse files

Show current and archived assignments on clients#show

parent d7076905
Pipeline #17278 failed with stage
in 60 minutes and 2 seconds
......@@ -37,9 +37,11 @@ Metrics/LineLength:
Exclude:
- 'db/seeds.rb'
Metrics/ClassLength:
Max: 150
Exclude:
- 'test/**/*'
Metrics/MethodLength:
Max: 15
Exclude:
- 'test/**/*'
- 'db/seeds.rb'
......
......@@ -53,7 +53,7 @@ class AssignmentsController < ApplicationController
end
def new
@assignment = Assignment.new
@assignment = Assignment.new(client_id: params[:client_id], volunteer_id: params[:volunteer_id])
@assignment.default_values
authorize @assignment
end
......@@ -82,7 +82,7 @@ class AssignmentsController < ApplicationController
def find_client
set_volunteer
@q = policy_scope(Client).need_accompanying.ransack(params[:q])
@q = policy_scope(Client).inactive.ransack(params[:q])
@q.sorts = ['created_at desc'] if @q.sorts.empty?
@need_accompanying = @q.result.paginate(page: params[:page])
end
......
......@@ -28,7 +28,10 @@ class ClientsController < ApplicationController
end
end
def show; end
def show
@current_assignments = @client.assignments.unterminated
@archived_assignments = @client.assignment_logs
end
def new
@client = Client.new(user: current_user)
......@@ -91,9 +94,11 @@ class ClientsController < ApplicationController
end
def resigned_fail_notice
assignment = @client.assignments.unterminated.first
{
message: 'Beenden fehlgeschlagen.', model_message: @client.errors.messages[:acceptance].first,
action_link: { text: 'Begleitung bearbeiten', path: edit_assignment_path(@client.assignment) }
action_link: { text: 'Begleitung bearbeiten', path: edit_assignment_path(assignment) }
}
end
......
......@@ -29,8 +29,15 @@ class VolunteersController < ApplicationController
def show
@volunteer_events = @volunteer.events.past
@group_offer_categories = @volunteer.group_offer_categories.active.without_house_moving
@group_offer_categories_house_moving = @volunteer.group_offer_categories.active.house_moving
@current_assignments = @volunteer.assignments.unterminated
@archived_assignments = @volunteer.assignment_logs
@current_group_assignments = @volunteer.group_assignments.unterminated
@archived_group_assignments = @volunteer.group_assignment_logs
end
def new
......
......@@ -23,13 +23,9 @@ class Client < ApplicationRecord
belongs_to :involved_authority, -> { with_deleted }, class_name: 'User',
inverse_of: 'involved_authorities', optional: true
has_many :manager_departments, through: :user, source: :departments
has_one :assignment_creator_departments, through: :assignment, source: :creator
has_one :assignment, dependent: :destroy
has_many :assignment_logs
has_one :volunteer, through: :assignment
has_many :volunteer_logs, through: :assignment_logs
has_many :assignments, dependent: :destroy
has_many :assignment_logs, dependent: :destroy
has_one :contact, as: :contactable, dependent: :destroy
accepts_nested_attributes_for :contact
......@@ -49,33 +45,26 @@ class Client < ApplicationRecord
Bitte sicherstellen, dass alle Einsätze komplett abgeschlossen sind.'
}, unless: :terminatable?
scope :with_assignment, (-> { joins(:assignment) })
scope :with_active_assignment, (-> { with_assignment.merge(Assignment.active) })
scope :need_accompanying, lambda {
inactive.order(created_at: :asc)
}
scope :with_active_assignment_between, lambda { |start_date, end_date|
with_assignment.merge(Assignment.active_between(start_date, end_date))
}
scope :with_assignments, (-> { distinct.joins(:assignments) })
scope :with_active_assignments, (-> { with_assignments.merge(Assignment.active) })
scope :with_inactive_assignment, lambda {
left_outer_joins(:assignment)
scope :with_inactive_assignments, lambda {
left_outer_joins(:assignments)
.accepted
.where('assignments.period_end < ?', Time.zone.today)
}
scope :without_assignment, lambda {
left_outer_joins(:assignment).where('assignments.id is NULL')
scope :without_assignments, lambda {
left_outer_joins(:assignments).where('assignments.id is NULL')
}
scope :active, lambda {
accepted.with_active_assignment
accepted.with_active_assignments
}
scope :inactive, lambda {
accepted.without_assignment.or(with_inactive_assignment)
active_assignments = Assignment.active.where('client_id = clients.id')
accepted.where("NOT EXISTS (#{active_assignments.to_sql})")
}
scope :resigned_between, lambda { |start_date, end_date|
......@@ -87,7 +76,7 @@ class Client < ApplicationRecord
}
def terminatable?
assignment.blank? || assignment.no_period? || assignment.ended?
assignments.unterminated.none?
end
def self.acceptences_restricted
......
......@@ -22,14 +22,13 @@ class GroupOffer < ApplicationRecord
accepts_nested_attributes_for :group_assignments, allow_destroy: true
has_many :group_assignment_logs
has_many :volunteers, through: :group_assignments
has_many :volunteer_logs, through: :group_assignment_logs
has_many :hours, as: :hourable, dependent: :destroy, inverse_of: :hourable
has_many :feedbacks, as: :feedbackable, dependent: :destroy, inverse_of: :feedbackable
has_many :trial_feedbacks, as: :trial_feedbackable, inverse_of: :trial_feedbackable,
dependent: :destroy
has_many :volunteers, through: :group_assignments
has_many :volunteer_contacts, through: :volunteers, source: :contact
validates :title, :offer_type, presence: true
......
......@@ -201,16 +201,8 @@ class Volunteer < ApplicationRecord
accepted? && assignments.active.blank? && group_assignments.active.blank?
end
def unterminated_assignments?
assignments.unterminated.any?
end
def unterminated_group_assignments?
group_assignments.unterminated.any?
end
def terminatable?
!(unterminated_assignments? || unterminated_group_assignments?)
assignments.unterminated.none? && group_assignments.unterminated.none?
end
def state
......
......@@ -4,10 +4,8 @@
.row
.col-xs-12.col-md-6
- if action_name == 'new'
= f.association :client, include_blank: true,
selected: params[:client_id] || @assignment&.client&.id
= f.association :volunteer, include_blank: true,
selected: params[:volunteer_id] || @assignment&.volunteer&.id
= f.association :client, include_blank: true
= f.association :volunteer, include_blank: true
- else
h4.m-b-10= t_model(Client)
= render 'user_table', user: @assignment.client, client: true
......
......@@ -80,8 +80,12 @@ h1= @client.contact.full_name
h3 Wann sind Sie verfügbar?
= render 'availability/show', available: @client
- if @client.assignment
h3#assignments Begleitungen
= render 'assignments/client_volunteer_index', assignments: @client.assignment
- if @current_assignments.present?
h3#assignments Aktuelle Begleitungen
= render 'assignments/client_volunteer_index', assignments: @current_assignments
- if @archived_assignment.present?
h3 Archivierte Begleitungen
= render 'assignments/client_volunteer_index', assignments: @archived_assignment
= render 'show_navigation'
- editable ||= false
table.table.table-striped.group-assignments-table
thead
tr
......
......@@ -141,21 +141,24 @@ h4.label-list
- else
em keine
- if @volunteer.assignments.unterminated.any?
- if @current_assignments.present?
h2.small#assignments Aktuelle Begleitungen
= render 'assignments/client_volunteer_index', assignments: @volunteer.assignments.unterminated
= render 'assignments/client_volunteer_index', assignments: @current_assignments
- if @volunteer.assignment_logs.any?
- if @archived_assignments.present?
h2.small Archivierte Begleitungen
= render 'assignment_logs/assignment_logs_volunteer_index', assignment_logs: @volunteer.assignment_logs
= render 'assignment_logs/assignment_logs_volunteer_index',
assignment_logs: @archived_assignments
- if @volunteer.group_assignments.unterminated.any?
- if @current_group_assignments.present?
h2.small#assignments= t('.group_assignments')
= render 'group_assignments/volunteer_group_assignments', group_assignments: @volunteer.group_assignments.unterminated, editable: true
= render 'group_assignments/volunteer_group_assignments',
group_assignments: @current_group_assignments, editable: true
- if @volunteer.group_assignment_logs.any?
- if @archived_group_assignments.present?
h2.small= t('.old_group_assignments')
= render 'group_assignments/volunteer_group_assignments_logs', group_assignment_logs: @volunteer.group_assignment_logs
= render 'group_assignments/volunteer_group_assignments_logs',
group_assignment_logs: @archived_group_assignments
- if policy(Event).show? && @volunteer_events.present?
= render 'events/volunteer_events', events: @volunteer_events
......
......@@ -8,7 +8,8 @@ class ClientActivityFilterTest < ActionDispatch::IntegrationTest
@client_rejected = create :client, acceptance: 'rejected'
create :assignment_active, client: @client_rejected
@client_resigned = create :client, acceptance: 'accepted'
create :assignment_active, client: @client_resigned, period_end: 2.days.ago
create :assignment_active, client: @client_resigned, period_end: 2.days.ago,
termination_verified_by: @superadmin, termination_verified_at: Time.zone.now
@client_resigned.resigned!
login_as @superadmin
end
......
......@@ -2,62 +2,62 @@ require 'test_helper'
class ClientScopesTest < ActiveSupport::TestCase
def setup
@now = Time.zone.now
@with_assignment = create :client
@with_active_assignment = create :client
@with_assignment_between_30_15_days_ago = create :client
@with_assignment_between_60_30_days_ago = create :client
@with_active_and_inactive_assignment = create :client
@no_assignment = create :client
[
{ title: 'start_60_days_ago', client: @with_assignment, start_date: @now.days_ago(60) },
{ title: 'start_60_days_ago', client: @with_active_assignment, start_date: 60.days.ago },
{ title: 'end_15_days_ago', client: @with_assignment_between_30_15_days_ago,
start_date: @now.days_ago(30), end_date: @now.days_ago(15) },
start_date: 30.days.ago, end_date: 15.days.ago },
{ title: 'end_30_days_ago', client: @with_assignment_between_60_30_days_ago,
start_date: @now.days_ago(60), end_date: @now.days_ago(30) }
].map { |parameters| make_assignment(parameters) }
start_date: 60.days.ago, end_date: 30.days.ago },
{ title: 'start_60_days_ago', client: @with_active_and_inactive_assignment,
start_date: 60.days.ago },
{ title: 'start_60_days_ago', client: @with_active_and_inactive_assignment,
start_date: 60.days.ago, end_date: 30.days.ago }
].each do |parameters|
make_assignment(parameters)
end
end
test 'with_assignments returns only clients that have assignments' do
query = Client.with_assignment
assert query.include? @with_assignment
assert query.include? @with_assignment_between_30_15_days_ago
assert query.include? @with_assignment_between_60_30_days_ago
refute query.include? @no_assignment
assert_equal 3, query.count
assert_equal [
@with_active_assignment,
@with_assignment_between_30_15_days_ago,
@with_assignment_between_60_30_days_ago,
@with_active_and_inactive_assignment
], Client.with_assignments.order(:id)
end
test 'with_active_assignments returns only clients that have active assignments' do
query = Client.with_active_assignment
assert query.include? @with_assignment
refute query.include? @with_assignment_between_30_15_days_ago
refute query.include? @with_assignment_between_60_30_days_ago
refute query.include? @no_assignment
assert_equal 1, query.count
end
test 'with_active_assignments_between clients that have active assignments in date range' do
query = Client.with_active_assignment_between(@now.days_ago(80), @now.days_ago(50))
assert query.include? @with_assignment
assert query.include? @with_assignment_between_60_30_days_ago
refute query.include? @with_assignment_between_30_15_days_ago
refute query.include? @no_assignment
assert_equal 2, query.count
assert_equal [
@with_active_assignment,
@with_active_and_inactive_assignment
], Client.with_active_assignments.order(:id)
end
test 'without_assignment returns only clients that have no assignments' do
query = Client.without_assignment
refute query.include? @with_assignment
refute query.include? @with_assignment_between_60_30_days_ago
refute query.include? @with_assignment_between_30_15_days_ago
assert query.include? @no_assignment
assert_equal 1, query.count
assert_equal [
@no_assignment
], Client.without_assignments.order(:id)
end
test 'with_inactive_assignment returns only clients that have an inactive assignment' do
query = Client.with_inactive_assignment
refute query.include? @with_assignment
assert query.include? @with_assignment_between_60_30_days_ago
assert query.include? @with_assignment_between_30_15_days_ago
refute query.include? @no_assignment
assert_equal 2, query.count
assert_equal [
@with_assignment_between_30_15_days_ago,
@with_assignment_between_60_30_days_ago,
@with_active_and_inactive_assignment
], Client.with_inactive_assignments.order(:id)
end
test 'inactive returns clients without assignments or with only inactive assignments' do
assert_equal [
@with_assignment_between_30_15_days_ago,
@with_assignment_between_60_30_days_ago,
@no_assignment
], Client.inactive.order(:id)
end
end
......@@ -15,21 +15,21 @@ class ClientTest < ActiveSupport::TestCase
refute client.valid?
end
test 'a client without assignment should show up in need accompanying' do
result = Client.need_accompanying
test 'a client without assignment should show up in inactive' do
result = Client.inactive
assert_equal [@client], result.to_a
end
test 'a client with an active assignment should not show up in need accompanying' do
test 'a client with an active assignment should not show up in inactive' do
create :assignment_active, client: @client
result = Client.need_accompanying
result = Client.inactive
assert_equal [], result.to_a
end
test 'a client with an inactive assignment should show up in need accompanying' do
test 'a client with an inactive assignment should show up in inactive' do
client = create :client
create :assignment_inactive, client: client
result = Client.need_accompanying
result = Client.inactive
assert result.include? @client
assert result.include? client
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment