Skip to content

Instantly share code, notes, and snippets.

@slayer
Created December 4, 2012 01:35
Show Gist options
  • Select an option

  • Save slayer/4199734 to your computer and use it in GitHub Desktop.

Select an option

Save slayer/4199734 to your computer and use it in GitHub Desktop.
class Notification < ActiveRecord::Base
self.table_name = 'some_notifications'
NONE = 0
EMAIL = 1
EMAIL2 = 2
SMS = 3
CALL = 4
QUEUED = 0
SENT = 1
ACCEPTED = 2
EXPIRED = 3
ARCHIVED = 9
STATES = {
:queued => QUEUED,
:sent => SENT,
:accepted => ACCEPTED,
:expired => EXPIRED,
:archived => ARCHIVED,
}
NOTIFICATION_METHODS = {
:none => NONE,
:email => EMAIL,
:email2 => EMAIL2,
:sms => SMS,
:call => CALL,
}
belongs_to :user, :class_name => User.to_s
belongs_to :parent
# validations
validates_presence_of :parent
validates_presence_of :user
default_scope :order => "id"
NOTIFICATION_METHODS.each do |key, value|
scope key, :conditions => {:state => value}
end
STATES.each do |key, value|
scope key, :conditions => {:state => value}
end
scope :valid, :conditions => {:state.not_eq => ARCHIVED}
# LONG LIST OF SCOPES
# State of notification
state_machine :state, :initial => :queued do
# EVENTS & TRANSITIONS
STATES.each do |key, value|
state key, :value => value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment