Skip to content

Instantly share code, notes, and snippets.

@alkema
Created September 17, 2010 13:38
Show Gist options
  • Save alkema/584239 to your computer and use it in GitHub Desktop.
Save alkema/584239 to your computer and use it in GitHub Desktop.
sms-via-email.rb
class MobileProvider < ActiveRecord::Base
validates_uniqueness_of :name
end
class Mobile < ActiveRecord::Base
include ActionView::Helpers::NumberHelper
belongs_to :user
belongs_to :mobile_provider
before_validation :strip_non_numeric
validates_presence_of [:user, :mobile_provider, :number]
validates_length_of :number, :is => 10
def formatted_number
number_to_phone(self.number, :area_code => true)
end
def sms_email
return "#{self.number}#{self.mobile_provider.sms_email}"
end
private
def strip_non_numeric
self.number.gsub!(/[^0-9]/, "")
end
end
class CreateMobiles < ActiveRecord::Migration
def self.up
create_table :mobiles do |t|
t.integer :user_id
t.integer :mobile_provider_id
t.string :number
t.timestamps
end
end
def self.down
drop_table :mobiles
end
end
class CreateMobileProviders < ActiveRecord::Migration
def self.up
create_table :mobile_providers do |t|
t.string :name
t.string :sms_email
t.timestamps
end
end
def self.down
drop_table :mobile_providers
end
end
class SmsMailer < ClacMailer
def sms_email_msg(user, broadcast)
setup_email(user.mobile.sms_email)
@subject += broadcast.subject
@body[:broadcast] = broadcast
@body[:user] = user
end
end
Data:
:name => 'Rogers Wireless', :sms_email => '@pcs.rogers.com'
:name => 'Fido', :sms_email => '@fido.ca'
:name => 'Bell Mobility', :sms_email => '@txt.bell.ca'
:name => 'Solo Mobile', :sms_email => '@txt.bell.ca'
:name => 'Telus Mobility', :sms_email => '@msg.telus.com'
:name => 'Virgin Mobile', :sms_email => '@vmobile.ca'
:name => 'MTS Mobility', :sms_email => '@text.mtsmobility.com'
:name => 'PC Telecom', :sms_email => '@mobiletxt.ca'
:name => 'SaskTel', :sms_email => '@sms.sasktel.com'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment