Sha256: e13ce96a98ab9009c17233de9119f724283444708bf0f2e0a9b697444d895a6a

Contents?: true

Size: 922 Bytes

Versions: 10

Compression:

Stored size: 922 Bytes

Contents

require 'digest/sha1'

module DataCatalog
  
  class User
  
    include MongoMapper::Document

    # == Attributes

    key :name,     String
    key :email,    String
    key :role,     String
    key :_api_key, String
    timestamps!

    # == Indices

    ensure_index :email

    # == Validations
    
    validates_presence_of :name
    validates_presence_of :role
    validate :validate_role

    VALID_ROLES = %w(basic curator admin)

    def validate_role
      unless VALID_ROLES.include?(role)
        errors.add(:role, "must be in #{VALID_ROLES.inspect}")
      end
    end
    
    # == Callbacks
    
    def before_create
      unless _api_key
        self._api_key = generate_api_key
      end
    end

    def generate_api_key
      salt = Config.environment_config["api_key_salt"]
      s = "#{Time.now.to_f}#{salt}#{rand(100_000_000)}#{name}#{email}"
      Digest::SHA1.hexdigest(s)
    end

  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sinatra_resource-0.3.1 examples/datacatalog/models/user.rb
sinatra_resource-0.3.0 examples/datacatalog/models/user.rb
sinatra_resource-0.2.6 examples/datacatalog/models/user.rb
sinatra_resource-0.2.5 examples/datacatalog/models/user.rb
sinatra_resource-0.2.4 examples/datacatalog/models/user.rb
sinatra_resource-0.2.3 examples/datacatalog/models/user.rb
sinatra_resource-0.2.2 examples/datacatalog/models/user.rb
sinatra_resource-0.2.1 examples/datacatalog/models/user.rb
sinatra_resource-0.2.0 examples/datacatalog/models/user.rb
sinatra_resource-0.1.0 examples/datacatalog/models/user.rb