-
Notifications
You must be signed in to change notification settings - Fork 344
/
services_controller.rb
138 lines (111 loc) · 3.78 KB
/
services_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
class Admin
class ServicesController < ApplicationController
include Taggable
before_action :authenticate_admin!
layout 'admin'
def index
@services = Kaminari.paginate_array(policy_scope(Service)).
page(params[:page]).per(params[:per_page])
end
def edit
assign_location_service_and_taxonomy_ids
authorize @location
end
def update
assign_location_service_and_taxonomy_ids
preprocess_service_params
authorize @location
preprocess_service
if @service.update(service_params.except(:locations))
redirect_to [:admin, @location, @service],
notice: 'Service was successfully updated.'
else
render :edit
end
end
def new
@location = Location.find(params[:location_id])
@taxonomy_ids = []
authorize @location
@service = Service.new
end
def create
prepare_and_authorize_service_creation
if @service.save
redirect_to admin_location_path(@location),
notice: "Service '#{@service.name}' was successfully created."
else
render :new
end
end
def destroy
service = Service.find(params[:id])
authorize service.location
service.destroy
redirect_to admin_locations_path
end
private
def prepare_and_authorize_service_creation
preprocess_service_params
@location = Location.find(params[:location_id])
@service = @location.services.new(service_params.except(:locations))
@taxonomy_ids = []
authorize @location
preprocess_service
end
def preprocess_service_params
shift_and_split_params(params[:service], :keywords)
end
def preprocess_service
add_program_to_service_if_authorized
add_service_to_location_if_authorized
end
def add_program_to_service_if_authorized
prog_id = service_params[:program_id]
@service.program = nil and return if prog_id.blank?
if program_ids_for(@service).select { |id| id == prog_id.to_i }.present?
@service.program_id = prog_id
end
end
def program_ids_for(service)
service.location.organization.programs.pluck(:id)
end
def add_service_to_location_if_authorized
return if location_ids.blank?
location_ids.each do |id|
Location.find(id.to_i).services.create!(service_params.except(:locations))
end
end
def location_ids
locations = service_params[:locations]
return if locations.blank?
locations.select { |id| location_ids_for(@service).include?(id.to_i) }
end
def location_ids_for(service)
@ids ||= service.location.organization.locations.pluck(:id)
end
def assign_location_service_and_taxonomy_ids
@service = Service.find(params[:id])
@location = Location.find(params[:location_id])
@taxonomy_ids = @service.categories.pluck(:taxonomy_id)
end
# rubocop:disable MethodLength
def service_params
params.require(:service).permit(
{ accepted_payments: [] }, :alternate_name, :audience, :description, :eligibility, :email,
:fees, { funding_sources: [] }, :application_process, :interpretation_services,
{ keywords: [] }, { languages: [] }, :name, { required_documents: [] },
{ service_areas: [] }, :status, :website, :wait_time, { category_ids: [] },
:program_id, { locations: [] },
regular_schedules_attributes: %i[weekday opens_at closes_at id _destroy],
holiday_schedules_attributes: %i[
closed start_date end_date opens_at closes_at id _destroy
],
phones_attributes: %i[
country_prefix department extension number number_type vanity_number id _destroy
]
)
end
# rubocop:enable MethodLength
end
end