Sha256: ab509bc99fe95efb2f2462d0dde2a4250ae7458cf2d40c02ae2a61003326fdb8

Contents?: true

Size: 1.93 KB

Versions: 66

Compression:

Stored size: 1.93 KB

Contents

module Authorization
  module StatefulRoles
    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
        include StatefulRolesInstanceMethods
        
        acts_as_state_machine :initial => :pending
        state :passive
        state :pending, :enter => :make_activation_code
        state :active,  :enter => :do_activate
        state :suspended
        state :deleted, :enter => :do_delete

        event :register do
          transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
        end
        
        event :activate do
          transitions :from => :pending, :to => :active 
        end
        
        event :suspend do
          transitions :from => [:passive, :pending, :active], :to => :suspended
        end
        
        event :delete do
          transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
        end

        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
      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
    end # instance methods
  end
end

Version data entries

66 entries across 66 versions & 12 rubygems

Version Path
dwaite-restful-authentication-1.1.1 lib/authorization/stateful_roles.rb
genki-restful-authentication-1.1.1 lib/authorization/stateful_roles.rb
ggoodale-restful-authentication-1.1.1 lib/authorization/stateful_roles.rb
simonmenke-mr_authentication-0.0.1 vendor/plugins/restful-authentication/lib/authorization/stateful_roles.rb
smukherjee-openbill-0.1.5 vendor/plugins/restful-authentication/lib/authorization/stateful_roles.rb
smukherjee-openbill-0.1.6 vendor/plugins/restful-authentication/lib/authorization/stateful_roles.rb
smukherjee-openbill-0.1.7 vendor/plugins/restful-authentication/lib/authorization/stateful_roles.rb
rails3-restful-authentication-3.0.1 lib/authorization/stateful_roles.rb
tournament-5.0.0 webgui/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
jashmenn-restful-authentication-2.0.0.beta1 lib/authorization/stateful_roles.rb
restful-authentication-1.2.1 lib/authorization/stateful_roles.rb
branston-0.6.6 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
branston-0.6.5 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
branston-0.6.4 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
branston-0.6.3 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
branston-0.6.2 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
tournament-4.2.0 webgui/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
tournament-4.0.2 webgui/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
tournament-4.0.0 webgui/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb
branston-0.6.1 lib/branston/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb