module Stex::Acts::Messagable
Public Class Methods
Follows possible forward and optional recipient chains down to the white rabb… final recipient which then either handles the message hash or receives a message through the system
@param [Array<Messagable>] recipients
recipients to be inspected
@return [Array<Messagable>]
Messagables which do not have a +:forward+-option set.
# File lib/stex/acts/messagable.rb, line 82 def self.determine_message_recipients(recipients = []) result = [] Array(recipients).each do |recipient_or_array| #Determine optional recipients and add them to the result set if recipient_or_array.is_a?(Array) recipient = recipient_or_array.first optional_recipients = Array(recipient_or_array.last).map { |i| recipient.messagable_accessor(:optional_recipients).assoc(i.to_sym).second }.flatten result += determine_message_recipients(optional_recipients) else recipient = recipient_or_array end raise ArgumentError.new('Invalid Recipient: ' + recipient.inspect) unless Stex::Acts::Messagable.messagable?(recipient) #Handle +forward_to+ options on the recipient record. #If it's set, we don't want to add the current recipient to the result list, #but rather the ones it's pointing at. if recipient.messagable_accessor(:forward_to) result += determine_message_recipients(recipient.messagable_accessor(:forward_to)) else result << recipient end end result end
Includes the class methods
@private
# File lib/stex/acts/messagable.rb, line 117 def self.included(base) base.class_eval do base.send :extend, ClassMethods end end
@return [Bool] true
if the given model acts_as_messagable
# File lib/stex/acts/messagable.rb, line 7 def self.messagable?(model) model.respond_to?(:acts_as_messagable_options) end
@return [ActiveRecord::Base] (Notification)
The class used to create new messages in the system
# File lib/stex/acts/messagable.rb, line 26 def self.message_class self.message_class_name.constantize end
Sets the class which is used to create messages in the system. Defaults to Notification
@param [ActiveRecord::Base, String, Symbol] klass
Class to be used for message handling
# File lib/stex/acts/messagable.rb, line 18 def self.message_class=(klass) @@message_class_name = klass.to_s end
@return [String]
The message class' name
# File lib/stex/acts/messagable.rb, line 34 def self.message_class_name @@message_class_name ||= 'Notification' @@message_class_name.classify end
Helper method to run certain acts_as_messagable
options. It basically determines the value’s class and either directly runs given Proc objects or tries to handle symbols as instance method. The :if
options on ActiveRecord validations work the same way.
@param [Messagable] instance
The sender or recipient the given option was defined in
@param [Proc, String, Symbol] proc_or_symbol
Either a Proc object or an instance method name. If a +String+ or +Symbol+ is given, a method with this name has to be defined in +instance+
@param [Object] default (nil)
If +proc_or_symbol+ is neither +Proc+ nor +String+ or +Symbol+, this value is returned instead.
@return [Object] The executed method or default
# File lib/stex/acts/messagable.rb, line 60 def self.run_method(instance, proc_or_symbol, default = nil) if proc_or_symbol.is_a?(Symbol) || proc_or_symbol.is_a?(String) raise Exception.new("Expected #{proc_or_symbol} to be an instance method of #{instance.class.name}") unless instance.respond_to?(proc_or_symbol) instance.send(proc_or_symbol) elsif proc_or_symbol.is_a?(Proc) proc_or_symbol.call(instance) else default end end