Created
September 3, 2008 06:09
-
-
Save zarigani/8548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Authorization | |
| module AasmRoles | |
| unless Object.constants.include? "STATEFUL_ROLES_CONSTANTS_DEFINED" | |
| STATEFUL_ROLES_CONSTANTS_DEFINED = true # sorry for the C idiom | |
| end | |
| def self.included( recipient ) | |
| recipient.extend( StatefulRolesClassMethods ) | |
| recipient.class_eval do | |
| before_save :do_before_save | |
| after_save :do_after_save | |
| include StatefulRolesInstanceMethods | |
| include AASM | |
| aasm_column :state | |
| aasm_initial_state :initial => :pending | |
| aasm_state :passive | |
| aasm_state :pending, :enter => :make_activation_code | |
| aasm_state :active, :enter => :do_activate, :exit=>:do_exit_active | |
| aasm_state :suspended, :enter=>:do_enter, :after=>:do_after, :exit=>:do_exit_suspended | |
| aasm_state :deleted, :enter => :do_delete | |
| aasm_event :register do | |
| transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) } | |
| end | |
| aasm_event :activate do | |
| transitions :from => :pending, :to => :active | |
| end | |
| # aasm_event :suspend do | |
| # transitions :from => [:passive, :pending, :active], :to => :suspended | |
| # end | |
| aasm_event :delete do | |
| transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted | |
| end | |
| aasm_event :unsuspend do | |
| transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? } | |
| transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? } | |
| transitions :from => :suspended, :to => :passive | |
| end | |
| aasm_event :suspend do | |
| transitions :from => [:passive, :pending, :active, :suspended], :to => :suspended, | |
| :guard => Proc.new {puts "*"*40 + "guard"; false;}, | |
| :on_transition => :do_on_transition | |
| end | |
| end | |
| end | |
| module StatefulRolesClassMethods | |
| end # class methods | |
| module StatefulRolesInstanceMethods | |
| # Returns true if the user has just been activated. | |
| def recently_activated? | |
| @activated | |
| end | |
| def do_delete | |
| self.deleted_at = Time.now.utc | |
| end | |
| def do_activate | |
| # @activated = true | |
| self.activated_at = Time.now.utc | |
| self.deleted_at = self.activation_code = nil | |
| end | |
| def do_enter | |
| puts "*"*40 + "enter" | |
| end | |
| def do_after | |
| puts "*"*40 + "after" | |
| end | |
| def do_on_transition | |
| puts "*"*40 + "do_on_transition" | |
| end | |
| def do_exit_active | |
| puts "*"*40 + "exit_active" | |
| end | |
| def do_exit_suspended | |
| puts "*"*40 + "exit_suspended" | |
| end | |
| def do_before_save | |
| puts "*"*40 + "before_save" | |
| end | |
| def do_after_save | |
| puts "*"*40 + "after_save" | |
| end | |
| end # instance methods | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment