-
Notifications
You must be signed in to change notification settings - Fork 17
/
helpers.rb
44 lines (40 loc) · 1.13 KB
/
helpers.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
helpers do
# a helper method to turn a string ID
# representation into a BSON::ObjectId
def object_id(val)
BSON::ObjectId.from_string(val)
rescue BSON::ObjectId::Invalid
nil
end
def document_by_id(id)
id = object_id(id) if String === id
if id.nil?
{}.to_json
else
document = settings.post_db.find(_id: id).to_a.first
(document || {}).to_json
end
end
def log_event(type, name, message, params = '{}')
case type
when 'error'
logger.error("event=#{name} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
when 'info'
logger.info("event=#{name} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
when 'warning'
logger.warn("event=#{name} | " \
"message=\'#{message}\' | " \
"params: #{params.to_json}")
end
end
def flash_danger(message)
session[:flashes] << { type: 'alert-danger', message: message }
end
def flash_success(message)
session[:flashes] << { type: 'alert-success', message: message }
end
end