module Devise
  module Models
    module Deactivatable
      extend ActiveSupport::Concern
      
      included do
        scope :deactivated, -> { where("'deactivated_at' is not NULL") }
      end
      
      def self.required_fields(klass)
        [:deactivated_at]
      end
      
      # Overwrites active_for_authentication? from Devise::Models::Activatable for deactivate purposes
      # by verifying whether a user is active to sign in or not based on deactivated?
      def active_for_authentication?
        !deactivated? && super
      end

      def deactivate!
        self.deactivated_at = Time.now
        save(validate: false) or raise "Devise deactivatable could not save #{inspect}." \
          "Please make sure a model using deactivatable can be saved when deactivating."

        after_deactivate
      end      

      def deactivated?
        !!deactivated_at
      end
      
      # Overwrites invalid_message from Devise::Models::Authenticatable to define
      # the correct reason for blocking the sign in.
      def inactive_message
        deactivated? ? :deactivated : super
      end
      
      protected
      
      # A callback initiated after successfully deactivating. This can be
      # used to insert your own logic that is only run after the user successfully
      # deactivated.
      #
      # Example:
      #
      #   def after_deactivated
      #     # remove oauth tokens
      #   end
      #      
      def after_deactivate
      end
      
      module ClassMethods
        
      end
    end
  end
end
module Devise
  module Models
    module Deactivatable
      extend ActiveSupport::Concern
      
      included do
        scope :deactivated, -> { where.not(deactivated: nil) }
      end
      
      def self.required_fields(klass)
        [:deactivated_at]
      end
      
      # Overwrites active_for_authentication? from Devise::Models::Activatable for deactivate purposes
      # by verifying whether a user is active to sign in or not based on deactivated?
      def active_for_authentication?
        !deactivated? && super
      end

      def deactivate!
        self.deactivated_at = Time.now
        save(validate: false) or raise "Devise deactivatable could not save #{inspect}." \
          "Please make sure a model using deactivatable can be saved when deactivating."

        after_deactivate
      end      

      def deactivated?
        !!deactivated_at
      end
      
      # Overwrites invalid_message from Devise::Models::Authenticatable to define
      # the correct reason for blocking the sign in.
      def inactive_message
        deactivated? ? :deactivated : super
      end
      
      protected
      
      # A callback initiated after successfully deactivating. This can be
      # used to insert your own logic that is only run after the user successfully
      # deactivated.
      #
      # Example:
      #
      #   def after_deactivated
      #     # remove oauth tokens
      #   end
      #      
      def after_deactivate
      end
      
      module ClassMethods
        
      end
    end
  end
end

Use AREL 'not' method to construct proper query
Use AREL 'not' method to construct proper query

This change fixes the constructed query to include the model's table name which is required for PostgreSQL queries to return expected results.

current behavior:
```ruby
User.where("'deactivated_at' is not NULL").count
#=> SELECT COUNT(*) FROM "users" WHERE ('deactivated_at' is not NULL)
#=> 64
```

versus the expected behavior:
```ruby
User.where.not(deactivated_at: nil).count
#=> SELECT COUNT(*) FROM "users" WHERE ("users"."deactivated_at" IS NOT NULL)
#=> 0
```