Sha256: c2e9ec665e5db19dbad5863f5258b43715fba3a9700400143b38429567383726

Contents?: true

Size: 971 Bytes

Versions: 6

Compression:

Stored size: 971 Bytes

Contents

require 'janus/hooks/trackable'

module Janus
  module Models
    # = Trackable
    #
    # Simple hook to update some columns of your model whenever a user logs in.
    #
    # == Required columns
    #
    # - +sign_in_count+
    # - +current_sign_in_ip+
    # - +current_sign_in_at+
    # - +last_sign_in_ip+
    # - +last_sign_in_at+
    #
    module Trackable
      extend ActiveSupport::Concern

      included do
        include Janus::Models::Base unless include?(Janus::Models::Base)

        begin
          attr_protected :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip
        rescue
        end
      end

      def track!(ip)
        self.sign_in_count += 1

        self.last_sign_in_at = self.current_sign_in_at
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_at = Time.now
        self.current_sign_in_ip = ip

        save(:validate => false)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
janus-0.10.1 lib/janus/models/trackable.rb
janus-0.10.0 lib/janus/models/trackable.rb
janus-0.9.1 lib/janus/models/trackable.rb
janus-0.9.0 lib/janus/models/trackable.rb
janus-0.8.1 lib/janus/models/trackable.rb
janus-0.8.0 lib/janus/models/trackable.rb