clients_controller.rb 3.61 KB
Newer Older
1
class ClientsController < ApplicationController
2
  include AvailabilityAttributes
3
  include NestedAttributes
Tugce Nur Tas's avatar
Tugce Nur Tas committed
4
  include ContactAttributes
5

6
  before_action :set_client, only: [:show, :edit, :update, :set_terminated]
7
  before_action :set_social_worker_collection, only: [:new, :edit]
8
9

  def index
10
    authorize Client
11
    @q = policy_scope(Client).ransack(default_filter)
12
    @q.sorts = ['created_at desc'] if @q.sorts.empty?
13
    @clients = @q.result
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
14
    respond_to do |format|
15
16
17
18
      format.xlsx
      format.html do
        @clients = @clients.paginate(page: params[:page], per_page: params[:print] && @clients.size)
      end
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
19
    end
20
21
  end

22
23
  def search
    authorize Client
24
    @q = policy_scope(Client).ransack contact_full_name_cont: params[:term]
25
26
27
28
29
30
    @clients = @q.result distinct: true
    respond_to do |format|
      format.json
    end
  end

Tugce Nur Tas's avatar
Tugce Nur Tas committed
31
  def show; end
32
33

  def new
34
    @client = Client.new(user: current_user)
35
    @client.language_skills << LanguageSkill.new(language: 'DE')
36
    authorize @client
37
38
  end

39
  def edit
40
    @client.language_skills << LanguageSkill.new(language: 'DE') if @client.german_missing?
41
  end
42
43
44

  def create
    @client = Client.new(client_params)
45
    @client.user = current_user
46
    @client.involved_authority ||= current_user if current_user.social_worker?
47
    authorize @client
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
48
    if @client.save
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
49
      redirect_to @client, create_success_notice
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
50
51
    else
      render :new
52
53
54
55
    end
  end

  def update
56
    @client.assign_attributes(client_params)
57
    @client.resigned_by = current_user if @client.will_save_change_to_acceptance?(to: :resigned)
58
59
    if @client.save
      redirect_to @client, create_update_redirect_notice
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
60
    else
61
      @custom_notice = resigned_fail_notice if @client.errors.messages[:acceptance].present?
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
62
      render :edit
63
64
65
    end
  end

66
  def set_terminated
67
68
69
    if @client.update(acceptance: :resigned, resigned_by: current_user)
      redirect_back(fallback_location: client_path(@client),
        notice: 'Klient/in wurde erfolgreich beendet.')
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
70
    else
71
      redirect_back(fallback_location: client_path(@client), notice: resigned_fail_notice)
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
72
73
74
    end
  end

75
76
  private

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
77
78
79
80
81
82
  def create_success_notice
    if @client.user.social_worker? && ClientNotification.active.any?
      { notice: ClientNotification.active.pluck(:body).to_sentence }
    else
      make_notice
    end
83
84
85
86
87
88
89
90
91
92
  end

  def create_update_redirect_notice
    if @client.saved_change_to_acceptance?(to: :resigned)
      { notice: 'Klient/in wurde erfolgreich beendet.' }
    else
      make_notice
    end
  end

93
94
95
96
97
98
99
  def resigned_fail_notice
    {
      message: 'Beenden fehlgeschlagen.', model_message: @client.errors.messages[:acceptance].first,
      action_link: { text: 'Begleitung bearbeiten', path: edit_assignment_path(@client.assignment) }
    }
  end

100
101
102
103
104
105
106
107
108
  def default_filter
    return { acceptance_not_eq: 2 } if params[:q].blank?
    filters = params.to_unsafe_hash[:q]
    if filters[:acceptance_eq].present? || filters[:contact_full_name_cont].present?
      filters.except(:acceptance_not_eq)
    else
      filters.merge(acceptance_not_eq: 2)
    end
  end
109

Tugce Nur Tas's avatar
Tugce Nur Tas committed
110
111
  def set_client
    @client = Client.find(params[:id])
Tugce Nur Tas's avatar
Tugce Nur Tas committed
112
    authorize @client
Tugce Nur Tas's avatar
Tugce Nur Tas committed
113
114
  end

115
116
117
118
  def set_social_worker_collection
    @social_workers = User.social_workers
  end

Tugce Nur Tas's avatar
Tugce Nur Tas committed
119
  def client_params
Tugce Nur Tas's avatar
Tugce Nur Tas committed
120
    params.require(:client).permit(
121
      :gender_request, :age_request, :other_request, :birth_year,
122
      :salutation, :nationality, :entry_date, :permit, :goals, :education, :interests,
123
      :acceptance, :comments, :involved_authority_id, :competent_authority, :actual_activities,
124
      :cost_unit, language_skills_attributes, relatives_attributes, contact_attributes,
125
      availability_attributes
126
    )
Tugce Nur Tas's avatar
Tugce Nur Tas committed
127
  end
128
end