Module: Eventifier::NotificationHelper

Defined in:
app/helpers/eventifier/notification_helper.rb

Instance Method Summary collapse

Instance Method Details

#event_message(event) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/eventifier/notification_helper.rb', line 30

def event_message event
  if event.verb.to_sym == :update
    if event.change_data.keys.count == 1
      key = "events.#{event.eventable_type.downcase}.#{event.verb}.single"
    else
      key = "events.#{event.eventable_type.downcase}.#{event.verb}.multiple"
    end
  else
    key = "events.#{event.eventable_type.downcase}.#{event.verb}"
  end
  message = I18n.translate key, :default => :"events.default.#{event.verb}", "user.name" => event.user.try(:name), :"event.type" => event.eventable_type

  replace_vars(message, event).html_safe
end

#load_event_for_template(event) ⇒ Object

Make is so you can include object in the I18n descriptions and it refers to the eventable object the event is referring to.



69
70
71
72
73
74
# File 'app/helpers/eventifier/notification_helper.rb', line 69

def load_event_for_template event
  def event.object; eventable; end
  def event.object_type; eventable_type; end

  event
end

#notification_message(event) ⇒ Object

A helper method for outputting a notification message.

Uses I18n messages from config/locales/events.en.yml to generate these messages, or defaults to a standard.

Example:

%ul#recent_notifications

- current_user.notifications.each do |notification|
  %li= notification_message notification.event


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/eventifier/notification_helper.rb', line 13

def notification_message event
  default = "notifications.default.#{event.verb}".to_sym
  if event.verb.to_sym == :update
    if event.change_data.keys.count == 1
      key = "notifications.#{event.eventable_type.downcase}.#{event.verb}.attributes.#{event.change_data.keys.first}"
      default = ["notifications.#{event.eventable_type.downcase}.#{event.verb}.single".to_sym, default]
    else
      key = "notifications.#{event.eventable_type.downcase}.#{event.verb}.multiple"
    end
  else
    key = "notifications.#{event.eventable_type.downcase}.#{event.verb}"
  end
  message = I18n.translate key, :default => default, :"user.name" => event.user.name, :"event.type" => event.eventable_type

  replace_vars(message, event).html_safe
end

#pastize(verb) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/eventifier/notification_helper.rb', line 76

def pastize(verb)
  case verb
  when 'create'
    "created"
  when 'update'
    "changed"
  when 'destroy'
    "deleted"
  end
end

#replace_vars(message, event) ⇒ Object

Used to replace {variables} in I18n messages



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/eventifier/notification_helper.rb', line 46

def replace_vars message, event
  event = load_event_for_template event
  message.scan(/{{[^}]*}}/) do |replaceable|
    method = "event."+replaceable.to_s.gsub(/[{|}]/, '').to_s
    replace_text = eval(method) rescue ""

    case replaceable.to_s
      when "{{object.name}}"
        replace_text = "<strong class='target'>#{replace_text}</strong>"
      when "{{object.title}}"
        replace_text = "<strong class='target'>#{replace_text}</strong>"
      when "{{user.name}}"
        replace_text = "<strong class='user'>#{replace_text}</strong>"
      else
        replace_text = "<strong>#{replace_text}</strong>"
    end

    message = message.gsub(replaceable.to_s, replace_text.to_s.gsub("_", " "))
  end
  message
end