Module: Stex::Acts::Messagable::ClassMethods

Defined in:
lib/stex/acts/messagable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_messagable(options = {}) ⇒ Object

Adds the functionality to send and receive messages to this ActiveRecord class

Examples:

Forwarding group messages to its students and optionally include the tutors

acts_as_messagable :forward_to          => :students,
                   :optional_recipients => [[:tutors, lambda { |r| r.tutors }, Tutor.human_name(:count => 2)]]
# The selector could simplified by +:tutors+ instead of the lambda expression

Parameters:

  • options (Hash) (defaults to: {})

    Available Options and overrides for this model class

Options Hash (options):

  • :sender_name (Proc, Symbol) — default: "Class.human_name #self.id"

    Proc or Proc Name to determine the sender name.

  • :recipient_name (Proc, Symbol) — default: "Class.human_name #self.id"

    Proc or Instance Method Name to determine the recipient name.

  • :forward_to (Proc, Symbol)

    Proc or Instance Method Name. If specified, the system will forward received messages to the method’s return value instead of to the actual record

  • :optional_recipients (Array<Array<Symbol, Proc|Symbol, String>>)

    One ore more recipients that may be included in a message to an instance of this class. These should be selectable on the view part of the application, e.g. checkboxes next to the actual recipient. The elements are used as follows:

    1. An identifier to access this optional recipient group,

    2. The actual selector that should return the recipients,

    3. A string or Proc that tells the system how to display this optional recipient

  • :handler (Proc, Symbol)

    If given, messages are sent to the given proc object / instance method instead of actually created in the database. The handler method has to accept 2 arguments, the message sender and a Hash of options These options include:

    • :subject

    • :content

    • :additional_recipients (optional)

  • :store_additional_recipients (Bool, Symbol, Proc)

    If set or evaluating to true, all recipients (CC) are stored in a Message record. This might make sense in cases when e.g. an email should contain a list of all the people that received it. If the value is a Symbol, the system will assume that there is an instance method with that name.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/stex/acts/messagable.rb', line 171

def acts_as_messagable(options = {})
  klass = Stex::Acts::Messagable.message_class_name

  #Add has_many associations for easy message management
  has_many :received_messages, :class_name => klass, :as => :recipient, :conditions => {:sender_copy => false}
  has_many :unread_messages, :class_name => klass, :as => :recipient, :conditions => {:read_at => nil, :sender_copy => false}
  has_many :read_messages, :class_name => klass, :as => :recipient, :conditions => ['read_at IS NOT NULL AND sender_copy = ?', false]
  has_many :sent_messages, :class_name => klass, :as => :sender, :conditions => {:sender_copy => true}

  cattr_accessor :acts_as_messagable_options

  coptions                       = {}

  #Sender and recipient procs
  coptions[:sender_name]         = options[:sender_name] || lambda { |r| "#{r.class.human_name}: #{r.id}" }
  coptions[:recipient_name]      = options[:recipient_name] || lambda { |r| "#{r.class.human_name}: #{r.id}" }

  #Forward and message handler proc
  coptions[:forward_to]          = options[:forward_to] if options.has_key?(:forward_to)
  coptions[:handler]             = options[:handler] if options.has_key?(:handler)

  #optional recipients
  coptions[:optional_recipients] = options[:optional_recipients] if options[:optional_recipients]

  coptions[:store_additional_recipients] = options[:store_additional_recipients]

  self.acts_as_messagable_options = coptions.freeze

  include Stex::Acts::Messagable::InstanceMethods
end