Guest User

Untitled

a guest
Feb 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. module Authorization
  2. module AasmRoles
  3. unless Object.constants.include? "STATEFUL_ROLES_CONSTANTS_DEFINED"
  4. STATEFUL_ROLES_CONSTANTS_DEFINED = true # sorry for the C idiom
  5. end
  6.  
  7. def self.included( recipient )
  8. recipient.extend( StatefulRolesClassMethods )
  9. recipient.class_eval do
  10. before_save :do_before_save
  11. after_save :do_after_save
  12.  
  13.  
  14.  
  15. include StatefulRolesInstanceMethods
  16. include AASM
  17. aasm_column :state
  18. aasm_initial_state :initial => :pending
  19. aasm_state :passive
  20. aasm_state :pending, :enter => :make_activation_code
  21. aasm_state :active, :enter => :do_activate, :exit=>:do_exit_active
  22. aasm_state :suspended, :enter=>:do_enter, :after=>:do_after, :exit=>:do_exit_suspended
  23. aasm_state :deleted, :enter => :do_delete
  24.  
  25. aasm_event :register do
  26. transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
  27. end
  28.  
  29. aasm_event :activate do
  30. transitions :from => :pending, :to => :active
  31. end
  32.  
  33. # aasm_event :suspend do
  34. # transitions :from => [:passive, :pending, :active], :to => :suspended
  35. # end
  36.  
  37. aasm_event :delete do
  38. transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
  39. end
  40.  
  41. aasm_event :unsuspend do
  42. transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
  43. transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
  44. transitions :from => :suspended, :to => :passive
  45. end
  46.  
  47.  
  48.  
  49. aasm_event :suspend do
  50. transitions :from => [:passive, :pending, :active, :suspended], :to => :suspended,
  51. :guard => Proc.new {puts "*"*40 + "guard"; false;},
  52. :on_transition => :do_on_transition
  53. end
  54. end
  55. end
  56.  
  57. module StatefulRolesClassMethods
  58. end # class methods
  59.  
  60. module StatefulRolesInstanceMethods
  61. # Returns true if the user has just been activated.
  62. def recently_activated?
  63. @activated
  64. end
  65. def do_delete
  66. self.deleted_at = Time.now.utc
  67. end
  68.  
  69. def do_activate
  70. # @activated = true
  71. self.activated_at = Time.now.utc
  72. self.deleted_at = self.activation_code = nil
  73. end
  74.  
  75.  
  76.  
  77. def do_enter
  78. puts "*"*40 + "enter"
  79. end
  80.  
  81. def do_after
  82. puts "*"*40 + "after"
  83. end
  84.  
  85. def do_on_transition
  86. puts "*"*40 + "do_on_transition"
  87. end
  88.  
  89. def do_exit_active
  90. puts "*"*40 + "exit_active"
  91. end
  92.  
  93. def do_exit_suspended
  94. puts "*"*40 + "exit_suspended"
  95. end
  96.  
  97. def do_before_save
  98. puts "*"*40 + "before_save"
  99. end
  100.  
  101. def do_after_save
  102. puts "*"*40 + "after_save"
  103. end
  104. end # instance methods
  105. end
  106. end
Add Comment
Please, Sign In to add comment