Module: ActivityNotification::Common

Included in:
ORM::ActiveRecord::Notification, ORM::Dynamoid::Notification, ORM::Mongoid::Notification
Defined in:
lib/activity_notification/common.rb

Overview

Common module included in target and notifiable model. Provides methods to resolve parameters from configured field or defined method. Also provides methods to convert into resource name or class name as string.

Instance Method Summary collapse

Instance Method Details

#printable_nameString

Converts to printable model name to show in view or email.

Returns:

  • (String)

    Printable model name



156
157
158
# File 'lib/activity_notification/common.rb', line 156

def printable_name
  "#{self.printable_type} (#{id})"
end

#printable_typeString

TODO:

Is this the best to make readable?

Converts to printable model type name to be humanized.

Returns:

  • (String)

    Printable model type name



150
151
152
# File 'lib/activity_notification/common.rb', line 150

def printable_type
  "#{self.to_class_name.demodulize.humanize}"
end

#resolve_value(thing, *args) ⇒ Object

Used to transform value from metadata to data which belongs model instance. Accepts Symbols, which it will send against this instance, Accepts Procs, which it will execute with this instance. Both Symbols and Procs will be passed arguments of this method. Also accepts Hash of these Symbols or Procs. If any other value will be passed, returns original value.

Parameters:

  • thing (Symbol, Proc, Hash, Object)

    Symbol or Proc to resolve parameter

  • args (Array)

    Arguments to pass to thing as method

Returns:

  • (Object)

    Resolved parameter value



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
# File 'lib/activity_notification/common.rb', line 94

def resolve_value(thing, *args)
  case thing
  when Symbol
    symbol_method = method(thing)
    if symbol_method.arity > 0
      if args.last.kind_of?(Hash)
        symbol_method.call(*args[0...-1], **args[-1])
      else
        symbol_method.call(*args)
      end
    else
      symbol_method.call
    end
  when Proc
    if thing.arity > 1
      thing.call(self, *args)
    elsif thing.arity > 0
      thing.call(self)
    else
      thing.call
    end
  when Hash
    thing.dup.tap do |hash|
      hash.each do |key, value|
        hash[key] = resolve_value(value, *args)
      end
    end
  else
    thing
  end
end

#to_class_nameString

Converts to class name. This function returns base_class name for STI models if the class responds to base_class method.



131
132
133
# File 'lib/activity_notification/common.rb', line 131

def to_class_name
  self.class.respond_to?(:base_class) ? self.class.base_class.name : self.class.name
end

#to_resource_nameString

Converts to singularized model name (resource name).

Returns:

  • (String)

    Singularized model name (resource name)



137
138
139
# File 'lib/activity_notification/common.rb', line 137

def to_resource_name
  self.to_class_name.demodulize.singularize.underscore
end

#to_resources_nameString

Converts to pluralized model name (resources name).

Returns:

  • (String)

    Pluralized model name (resources name)



143
144
145
# File 'lib/activity_notification/common.rb', line 143

def to_resources_name
  self.to_class_name.demodulize.pluralize.underscore
end