Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created January 5, 2011 08:04
Show Gist options
  • Save ahawkins/766045 to your computer and use it in GitHub Desktop.
Save ahawkins/766045 to your computer and use it in GitHub Desktop.
module ActsAsSmsable
def self.included(base)
base.class_eval do
has_many :participants_sms_messages, :as => :participant, :dependent => :destroy
has_many :sms_messages, :through => :sms_messages_participants do
def sent_to(*args)
conditions = args.map do |obj|
composite_key = "("
composite_key += "participants_sms_message.participant_id = #{obj.id}"
composite_key += " AND "
composite_key += "pariticpants_sms_message.participant_type = '#{obj.class.base_class}'"
end
all :conditions => "(#{conditions.join(' OR ')})"
end
end
has_many :sent_sms_messages, :as => :sender, :class_name => "SmsMessage"
end
def can_receive_sms?
phone.present?
end
def to_sms_address
if phone && name
"#{name} <#{phone}>"
else
phone
end
end
end
end
#
@user.sms_messages.sent_to(@someone) # Undefined method error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment