tap/app/models/user.rb

77 lines
2.2 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
2015-02-09 17:06:24 +01:00
# balance_cents :integer default(0), not null
# nickname :string(255)
2015-02-09 17:06:24 +01:00
# admin :boolean
# koelkast :boolean default(FALSE)
# dagschotel_id :integer
# orders_count :integer default(0)
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
#
2014-11-06 14:46:59 +01:00
class User < ActiveRecord::Base
2015-02-09 17:06:24 +01:00
devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable
has_paper_trail only: [:balance, :admin, :orders_count, :koelkast]
2015-02-09 17:06:24 +01:00
2015-02-20 13:34:48 +01:00
has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>" }, default_style: :medium, default_url: "http://lorempixel.com/70/70/"
2014-12-09 17:17:11 +01:00
2014-12-06 17:51:41 +01:00
has_many :orders, -> { includes :products }
has_many :products, through: :orders
2014-12-09 20:28:50 +01:00
belongs_to :dagschotel, class_name: 'Product'
2014-11-06 18:30:53 +01:00
validates :nickname, presence: true, uniqueness: true
2015-02-12 14:39:58 +01:00
validates :name, presence: true
validates :last_name, presence: true
2015-02-10 13:04:35 +01:00
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
2015-02-17 15:35:15 +01:00
validates_attachment :avatar, presence: true
2014-11-06 18:30:53 +01:00
2015-01-06 20:18:01 +01:00
scope :members, -> { where koelkast: false }
def full_name
"#{name} #{last_name}"
2014-11-10 02:30:42 +01:00
end
2014-11-06 18:30:53 +01:00
2014-12-10 10:00:00 +01:00
def balance
2015-02-09 17:06:24 +01:00
self.balance_cents / 100.0
2014-12-10 10:00:00 +01:00
end
def balance=(value)
if value.is_a? String then value.sub!(',', '.') end
2015-02-09 17:06:24 +01:00
self.balance_cents = (value.to_f * 100).to_int
end
# Change URL params for User
def to_param
"#{id} #{nickname}".parameterize
end
# This is needed so Devise doesn't try to validate :email
def email_required?
false
end
def email_changed?
false
end
2014-11-06 14:46:59 +01:00
end