Sha256: e3aef09aec44457f11c3216a8b160a37ee0e01a3913128df299ba29e690c341e

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

module MyForum
  class User < ActiveRecord::Base
    require 'digest'

    has_many :posts, class_name: 'MyForum::Post'
    has_many :attachments
    has_many :user_roles
    has_many :roles, through: :user_roles
    has_many :user_group_links
    has_many :user_groups, through: :user_group_links

    has_one :avatar

    default_scope { where(is_deleted: false) }
    scope :online, -> { where("updated_at > ?", 10.minutes.ago) }
    scope :today_visited, -> { where("updated_at > ?", Time.now.beginning_of_day) }

    enum gender: [:female, :male, :alien]
    serialize :additional_info


    validates_uniqueness_of :login, :email

    before_save :encrypt_password

    def valid_password?(submitted_password)
      password == encrypt(submitted_password)
    end

    private

    def encrypt_password
      return unless password_changed?

      self.salt = make_salt unless valid_password?(password)
      self.password = encrypt(password)
    end

    def encrypt(string)
      secure_hash("#{salt}--#{string}")
    end

    def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
    end

    def secure_hash(string)
      Digest::SHA2.hexdigest(string)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta37 app/models/my_forum/user.rb
my_forum-0.0.1.beta36 app/models/my_forum/user.rb
my_forum-0.0.1.beta35 app/models/my_forum/user.rb
my_forum-0.0.1.beta34 app/models/my_forum/user.rb
my_forum-0.0.1.beta33 app/models/my_forum/user.rb
my_forum-0.0.1.beta32 app/models/my_forum/user.rb
my_forum-0.0.1.beta31 app/models/my_forum/user.rb
my_forum-0.0.1.beta30 app/models/my_forum/user.rb
my_forum-0.0.1.beta29 app/models/my_forum/user.rb
my_forum-0.0.1.beta28 app/models/my_forum/user.rb