Class: Biovision::Notifiers::BaseNotifier

Inherits:
Object
  • Object
show all
Defined in:
app/lib/biovision/notifiers/base_notifier.rb

Overview

Base notifier class for dispatching component notifications

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ BaseNotifier

Returns a new instance of BaseNotifier.

Parameters:



10
11
12
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 10

def initialize(user)
  @user = user
end

Instance Attribute Details

#notificationObject

Returns the value of attribute notification.



7
8
9
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 7

def notification
  @notification
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 7

def user
  @user
end

Class Method Details

.[](notification) ⇒ Object

Parameters:



15
16
17
18
19
20
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 15

def self.[](notification)
  slug = notification.component_slug
  instance = handler_class(slug).new(notification.user)
  instance.notification = notification
  instance
end

.handler_class(slug) ⇒ Object

Parameters:

  • slug (String)


27
28
29
30
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 27

def self.handler_class(slug)
  handler_name = "biovision/notifiers/#{slug}_notifier".classify
  handler_name.safe_constantize || BaseNotifier
end

.slugObject



22
23
24
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 22

def self.slug
  to_s.demodulize.to_s.underscore.gsub('_notifier', '')
end

Instance Method Details

#componentObject



32
33
34
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 32

def component
  @component ||= BiovisionComponent[self.class.slug]
end

#entityObject



36
37
38
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 36

def entity
  nil
end

#notify(data) ⇒ Object

Parameters:

  • data (Hash)


47
48
49
50
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 47

def notify(data)
  attributes = { user: user, biovision_component: component, data: data }
  Notification.create(attributes)
end

#viewObject



40
41
42
43
44
# File 'app/lib/biovision/notifiers/base_notifier.rb', line 40

def view
  return if notification.nil?

  "my/notifications/#{self.class.slug}/#{notification.data['type']}"
end