-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests_controller.rb
executable file
·224 lines (211 loc) · 8.37 KB
/
requests_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
class Object
def metaclass; class << self; self; end end
end
class Hash
def to_obj
hash = self
metaclass.class_eval do
hash.each_pair do |key, value|
define_method key do
value
end
end
end
end
end
class Backend::RequestsController < BackendController
# Filters
before_filter :init_org, :only => :show
# GET /backend/requests.html -------------------------------------- HTML
def index
@requests = Request.find(:all, :order => "created_at DESC").paginate(:page => params[:page], :per_page => 10)
end
# GET /backend/orgs/1/requests.html ------------------------------- HTML
def show
@requests = @org.requests.find(:all, :order => "created_at DESC").paginate(:page => params[:page], :per_page => 10)
end
# GET /backend/requests/1/edit.html ------------------------------- HTML
def edit
@request = Request.find(params[:id])
case @request.object
when "org" then
case @request.action
when "add" then
@org = @request.content[:new_record][:org].to_obj
@phones = @request.content[:new_record][:phones]
@activities = @request.content[:new_record][:activities]
render :template => "backend/requests/templates/org_add"
when "edit" then
@org_editable = @request.content[:edit_record].to_hash.to_obj
render :template => "backend/requests/templates/org_edit"
when "delete" then
@org = Org.find(@request.content[:delete_record_id])
render :template => "backend/requests/templates/org_delete"
end
when "phone" then
case @request.action
when "add" then
@phones = @request.content[:phones]
render :template => "backend/requests/templates/phone_add"
when "edit" then
@phone = Phone.find(@request.content[:original_record_id])
@phone_editable = @request.content[:edit_record].to_obj
render :template => "backend/requests/templates/phone_edit"
when "delete" then
@phone = Phone.find(@request.content[:delete_record_id])
render :template => "backend/requests/templates/phone_delete"
end
when "activity" then
case @request.action
when "add" then
@activities = @request.content[:new_record]
render :template => "backend/requests/templates/activity_add"
when "delete" then
@activity = Category.find(@request.content[:delete_record_id])
render :template => "backend/requests/templates/activity_delete"
end
end
session[:back] = request.env["HTTP_REFERER"]
rescue ActiveRecord::RecordNotFound => e
respond_error "Наверное объект запроса уже удален"
rescue
respond_error
end
# PUT /backend/requests/1.html ------------------------------------ HTML
def update
@request = Request.find(params[:id])
unless params[:request][:comment].blank?
unless @request.approved
if params[:request][:approved] == "1"
case @request.object
when "org" then
case @request.action
when "add" then
Org.transaction do
@org = Org.create!(
:name => params[:org][:name],
:description => params[:org][:description],
:user_id => @request.user_id,
:town_id => params[:org][:town_id],
:street_id => params[:org][:street_id],
:house => params[:org][:house],
:email => params[:org][:email],
:www => params[:org][:www]
)
if params[:activities]
params[:activities].each do |activity|
Activity.create(
:org_id => @org.id,
:category_id => activity
)
end
end
unless params[:phones].empty?
params[:phones].each do |phone|
Phone.create(
:org_id => @org.id,
:department => phone[:department],
:number => phone[:number]
)
end
end
expire_cache_org(@org.id)
end
when "edit" then
@org = Org.find(@request.org_id)
@org.update_attributes!(params[:org_editable])
expire_cache_org(@request.org_id)
when "delete" then
expire_cache_org(@request.org_id)
Org.destroy(@request.org_id)
end
when "phone" then
case @request.action
when "add" then
unless params[:phones].empty?
params[:phones].each do |phone|
Phone.create(
:org_id => @request.org_id,
:department => phone[:department],
:number => phone[:number]
)
end
end
expire_cache_phones(@request.org_id)
when "edit" then
phone = Phone.find(@request.content[:original_record_id])
phone.update_attributes!(params[:phone_editable])
expire_cache_phones(@request.org_id)
when "delete" then
expire_cache_phones(@request.org_id)
Phone.destroy(@request.content[:delete_record_id])
end
when "activity" then
case @request.action
when "add" then
params[:activities].each do |activity|
Activity.create(
:org_id => @request.org_id,
:category_id => activity
)
expire_cache_activities(@request.org_id)
end
when "delete" then
expire_cache_activities(@request.org_id)
Activity.delete_all(['org_id = ? AND category_id = ?', @request.org_id, @request.content[:delete_record_id]])
end
end
@request.approval(params[:request][:comment], params[:request][:approved])
respond_success "Запрос успешно обработан", session[:back]
else
@request.approval(params[:request][:comment], params[:request][:approved])
respond_success "Запрос не обработан"
end
else
@request.approval(params[:request][:comment], params[:request][:approved])
respond_success "Данные запроса успешно изменены"
end
else
respond_error "Введите комментарий к запросу"
end
rescue
respond_error
end
# DELETE /backend/requests/1.html --------------------------------- HTML
def destroy
Request.delete(params[:id])
respond_success "Запрос успешно удален"
rescue
respond_error
end
private
def init_org
@org = Org.find(params[:id])
end
def expire_cache_org(id)
org = Org.find(id)
expire_file("orgs/#{id}.html")
expire_dirs("orgs/#{id}/")
org.categories.collect{ |c| [c.id] }.each do |category|
expire_files("categories/#{category}.html")
expire_dirs("categories/#{category}/")
end
org.tags.collect{ |t| [t.id] }.each do |tag|
expire_files("tag/#{tag}.html")
expire_dirs("tag/#{tag}/")
end
end
def expire_cache_phones(id)
expire_files("orgs/#{id}/phones.html")
expire_dirs("orgs/#{id}/phones/")
end
def expire_cache_activities(id)
org = Org.find(id)
expire_file("orgs/#{id}.html")
expire_files("orgs/#{id}/activities.html")
org.categories.collect{ |c| [c.id] }.each do |category|
expire_files("categories/#{category}.html")
expire_dirs("categories/#{category}/")
end
end
end