Sha256: ab7b09ba81aca74b0b3389de3d2725f796c507a0c507b8948c835d691bc9dc12

Contents?: true

Size: 1.76 KB

Versions: 23

Compression:

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

    ADDITIONAL_INFO_ATTRS = [:real_name, :phone, :car_info]
    #, :website_url, :personal_text

    def self.serialized_attr_accessor(*args)
      ADDITIONAL_INFO_ATTRS.each do |attr|
        eval "
          def #{attr}
            (self.additional_info || {})[:#{attr}]
          end

          def #{attr}=(value)
            self.additional_info ||= {}
            self.additional_info[:#{attr}] = value
          end"
      end
    end

    # TODO should be stored in DB for editin from admin panel
    #serialized_attr_accessor :real_name, :phone, :website_url, :personal_text
    serialized_attr_accessor

    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

23 entries across 23 versions & 1 rubygems

Version Path
my_forum-0.0.2.1 app/models/my_forum/user.rb
my_forum-0.0.2 app/models/my_forum/user.rb
my_forum-0.0.1.beta60 app/models/my_forum/user.rb
my_forum-0.0.1.beta59 app/models/my_forum/user.rb
my_forum-0.0.1.beta58 app/models/my_forum/user.rb
my_forum-0.0.1.beta57 app/models/my_forum/user.rb
my_forum-0.0.1.beta56 app/models/my_forum/user.rb
my_forum-0.0.1.beta55 app/models/my_forum/user.rb
my_forum-0.0.1.beta54 app/models/my_forum/user.rb
my_forum-0.0.1.beta53 app/models/my_forum/user.rb
my_forum-0.0.1.beta52 app/models/my_forum/user.rb
my_forum-0.0.1.beta51 app/models/my_forum/user.rb
my_forum-0.0.1.beta50 app/models/my_forum/user.rb
my_forum-0.0.1.beta49 app/models/my_forum/user.rb
my_forum-0.0.1.beta48 app/models/my_forum/user.rb
my_forum-0.0.1.beta47 app/models/my_forum/user.rb
my_forum-0.0.1.beta46 app/models/my_forum/user.rb
my_forum-0.0.1.beta45 app/models/my_forum/user.rb
my_forum-0.0.1.beta44 app/models/my_forum/user.rb
my_forum-0.0.1.beta43 app/models/my_forum/user.rb