forked from sketch-city/harvey-api
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathshelters_controller.rb
More file actions
116 lines (99 loc) · 3.14 KB
/
shelters_controller.rb
File metadata and controls
116 lines (99 loc) · 3.14 KB
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
class SheltersController < ApplicationController
before_action :set_headers, except: [:index]
before_action :set_index_headers, only: [:index]
before_action :set_shelter, only: [:show, :edit, :update, :destroy, :archive]
def index
@shelters = Shelter.all
@page = Page.shelters.first_or_initialize
end
def new
@shelter = Shelter.new
end
def create
if(admin?)
@shelter = Shelter.new(shelter_update_params)
if @shelter.save
redirect_to shelters_path, notice: 'Shelter was successfully created.'
else
render :new
end
else
draft = Draft.new(info: shelter_update_params.merge({record_type: Shelter.name}), created_by: current_user)
if draft.save
redirect_to draft, notice: 'Your new shelter is pending approval.'
send_slack_notification "New shelter added: https://irma-api.herokuapp.com/drafts/#{draft.id}"
else
@shelter = Shelter.new(shelter_update_params)
render :new
end
end
end
def show
end
def edit
end
def update
if(admin?)
if @shelter.update(shelter_update_params)
redirect_to shelters_path, notice: 'Shelter was successfully updated.'
else
render :edit
end
else
draft = Draft.new(record: @shelter, info: shelter_update_params, created_by: current_user)
if draft.save
redirect_to draft, notice: 'Your shelter update is pending approval.'
send_slack_notification "Shelter updated: https://irma-api.herokuapp.com/drafts/#{draft.id}"
else
render :edit
end
end
end
def destroy
end
def archive
if(admin?)
@shelter.update_attributes(active: false)
redirect_to shelters_path, notice: "Archived!"
else
redirect_to shelters_path, notice: "You must be an admin to archive."
end
end
def drafts
@drafts = Draft.includes(:record).where("record_type = ? OR info->>'record_type' = 'Shelter'", Shelter.name).where(accepted_by_id: nil).where(denied_by_id: nil)
end
private
# This is the definition of a beautiful hack. 1 part gross, 2 parts simplicity. Does something neat not clever.
def set_headers
if(admin?)
columns = Shelter::ColumnNames + Shelter::PrivateFields
@columns = columns
@headers = columns.map(&:titleize)
else
@columns = Shelter::ColumnNames
@headers = Shelter::ColumnNames.map(&:titleize)
end
end
def set_index_headers
if(admin?)
columns = (Shelter::ColumnNames + Shelter::PrivateFields) - Shelter::IndexHiddenColumnNames
@columns = columns
@headers = columns.map(&:titleize)
else
columns = Shelter::ColumnNames - Shelter::IndexHiddenColumnNames
@columns = columns
@headers = columns.map(&:titleize)
end
end
def set_shelter
@shelter = Shelter.find(params[:id])
end
# TODO: Test private fields are only updatable by admin
def shelter_update_params
if(admin?)
params.require(:shelter).permit(Shelter::UpdateFields + Shelter::PrivateFields).keep_if { |_,v| v.present? }
else
params.require(:shelter).permit(Shelter::UpdateFields).keep_if { |_,v| v.present? }
end
end
end