Sha256: b48584a21ff21ec8ca766e777640ccb02fce1da12af4d96e70c8f3aa9162a0e3

Contents?: true

Size: 1.1 KB

Versions: 20

Compression:

Stored size: 1.1 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

    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

20 entries across 20 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta25 app/models/my_forum/user.rb
my_forum-0.0.1.beta24 app/models/my_forum/user.rb
my_forum-0.0.1.beta23 app/models/my_forum/user.rb
my_forum-0.0.1.beta22 app/models/my_forum/user.rb
my_forum-0.0.1.beta21 app/models/my_forum/user.rb
my_forum-0.0.1.beta20 app/models/my_forum/user.rb
my_forum-0.0.1.beta19 app/models/my_forum/user.rb
my_forum-0.0.1.beta18 app/models/my_forum/user.rb
my_forum-0.0.1.beta17 app/models/my_forum/user.rb
my_forum-0.0.1.beta16 app/models/my_forum/user.rb
my_forum-0.0.1.beta15 app/models/my_forum/user.rb
my_forum-0.0.1.beta14 app/models/my_forum/user.rb
my_forum-0.0.1.beta13 app/models/my_forum/user.rb
my_forum-0.0.1.beta12 app/models/my_forum/user.rb
my_forum-0.0.1.beta11 app/models/my_forum/user.rb
my_forum-0.0.1.beta10 app/models/my_forum/user.rb
my_forum-0.0.1.beta9 app/models/my_forum/user.rb
my_forum-0.0.1.beta8 app/models/my_forum/user.rb
my_forum-0.0.1.beta7 app/models/my_forum/user.rb
my_forum-0.0.1.beta5 app/models/my_forum/user.rb