tap/app/models/user.rb

60 lines
1.9 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# 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)
# admin :boolean
2014-12-09 19:28:50 +00:00
# dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
#
2014-11-06 13:46:59 +00:00
class User < ActiveRecord::Base
2014-12-09 16:17:11 +00:00
devise :database_authenticatable, :registerable,
:rememberable, :trackable
2014-12-10 08:20:12 +00:00
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium, default_url: "http://babeholder.pixoil.com/img/70/70"
2014-12-09 16:17:11 +00:00
2014-12-06 16:51:41 +00:00
has_many :orders, -> { includes :products }
2014-12-09 19:28:50 +00:00
belongs_to :dagschotel, class_name: 'Product'
2014-11-06 17:30:53 +00:00
validates :nickname, presence: true, uniqueness: true
validates :name, presence: true
validates :last_name, presence: true
2014-12-09 19:28:50 +00:00
validates :password, length: { in: 8..128 }, confirmation: true, on: :create
2014-12-09 21:32:54 +00:00
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
2014-11-06 17:30:53 +00:00
def full_name
"#{name} #{last_name}"
2014-11-10 01:30:42 +00:00
end
2014-11-06 17:30:53 +00:00
def pay(amount)
2014-12-10 09:00:00 +00:00
write_attribute(:balance, read_attribute(:balance) - amount)
self.save
end
def balance
(read_attribute(:balance) || 0) / 100.0
end
def balance=(value)
if value.is_a? String then value.sub!(',', '.') end
write_attribute(:balance, (value.to_f * 100).to_int)
end
2014-11-06 13:46:59 +00:00
end