-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.rb
76 lines (61 loc) · 2.01 KB
/
notify.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
module Controll
module Helper
module Notify
extend ActiveSupport::Concern
included do
attr_writer :notification_types
notification_types.each do |type|
define_method "create_#{type}" do |*args|
create_notification args.first, type.to_sym, args.extract_options!
end
end
end
# msg stack
def notifications
@notifications ||= []
end
def main_event
notifications.last || create_notice(:success)
end
def notify name, type = nil, options = {}
notifications << create_notification(name, type, options)
self # enable method chaining on controller
end
protected
include Controll::Event::Helper
def error name = :error, options = {}
notify name, :error, options
end
def success name = :success, options = {}
notify name, :success, options
end
def create_notification name, type = nil, options = {}
type ||= :notice
raise ArgumentError, "Not a valid notification type: #{type}, must be one of: #{valid_notification_types}" unless valid_notification_type?(type)
create_event name, type, options
end
alias_method :create_event, :create_notification
def valid_notification_type? type
notification_types.include? type.to_sym
end
def notification_types
return self.class.notification_types if self.class.respond_to? :notification_types
Controll::Notify::Flash.types
end
# allows customization of notification types
module ClassMethods
def notification_types
@notification_types ||= default_notification_types
end
def default_notification_types
Controll::Notify::Flash.types
end
end
def process_notifications
notifications.each do |message|
message_handler.send(message.type).notify message.name, message.options
end
end
end
end
end