2014-12-04 19:50:02 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
2014-12-09 18:56:56 +01:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# remember_created_at :datetime
|
2015-03-19 16:22:55 +01:00
|
|
|
# admin :boolean
|
|
|
|
# dagschotel_id :integer
|
|
|
|
# avatar_file_name :string
|
|
|
|
# avatar_content_type :string
|
|
|
|
# avatar_file_size :integer
|
|
|
|
# avatar_updated_at :datetime
|
|
|
|
# orders_count :integer default("0")
|
|
|
|
# koelkast :boolean default("f")
|
2015-09-14 17:52:25 +02:00
|
|
|
# name :string
|
2015-09-02 20:33:35 +02:00
|
|
|
# encrypted_password :string default(""), not null
|
|
|
|
# private :boolean default("f")
|
2014-12-04 19:50:02 +01:00
|
|
|
#
|
|
|
|
|
2014-11-06 14:46:59 +01:00
|
|
|
class User < ActiveRecord::Base
|
2015-09-18 13:20:57 +02:00
|
|
|
include Statistics, Avatarable, FriendlyId
|
|
|
|
friendly_id :name, use: :finders
|
2015-09-14 20:26:16 +02:00
|
|
|
|
2015-09-22 07:13:35 +02:00
|
|
|
devise :omniauthable, :omniauth_providers => [:zeuswpi]
|
2015-02-09 17:06:24 +01:00
|
|
|
|
2014-12-06 17:51:41 +01:00
|
|
|
has_many :orders, -> { includes :products }
|
2015-01-13 13:06:42 +01:00
|
|
|
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
|
|
|
|
2015-01-06 20:18:01 +01:00
|
|
|
scope :members, -> { where koelkast: false }
|
2015-09-18 13:20:57 +02:00
|
|
|
scope :publik, -> { where private: false }
|
2015-01-06 20:18:01 +01:00
|
|
|
|
2015-03-19 22:37:16 +01:00
|
|
|
def self.from_omniauth(auth)
|
2015-09-18 13:20:57 +02:00
|
|
|
where(name: auth.uid).first_or_create do |user|
|
2015-09-14 17:52:25 +02:00
|
|
|
user.name = auth.uid
|
2015-03-23 11:54:15 +01:00
|
|
|
user.avatar = Identicon.data_url_for auth.uid
|
2015-03-19 22:37:16 +01:00
|
|
|
end
|
2015-03-20 02:21:56 +01:00
|
|
|
end
|
|
|
|
|
2016-02-16 15:00:43 +01:00
|
|
|
def calculate_frecency
|
|
|
|
num_orders = Rails.application.config.frecency_num_orders
|
2016-02-16 15:38:44 +01:00
|
|
|
last_datetimes = self.orders.order(created_at: :desc)
|
|
|
|
.limit(num_orders)
|
|
|
|
.pluck(:created_at)
|
|
|
|
self.frecency = last_datetimes.map(&:to_time).map(&:to_i).sum / num_orders
|
2016-02-16 15:03:15 +01:00
|
|
|
self.save
|
2016-02-16 15:00:43 +01:00
|
|
|
end
|
|
|
|
|
2015-09-20 21:21:18 +02:00
|
|
|
def balance
|
|
|
|
@balance || begin
|
2016-03-01 16:05:46 +01:00
|
|
|
if Rails.env.test?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
headers = {
|
|
|
|
"Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}",
|
|
|
|
"Content-type" => "application/json"
|
|
|
|
}
|
|
|
|
result = HTTParty.get(File.join(Rails.application.config.api_url, "users", "#{name}.json"), headers: headers)
|
2015-09-20 21:21:18 +02:00
|
|
|
|
2016-03-01 16:05:46 +01:00
|
|
|
if result.code == 200
|
|
|
|
JSON.parse(result.body)["balance"]
|
|
|
|
end
|
2015-09-20 21:21:18 +02:00
|
|
|
end
|
2015-10-26 18:57:59 +01:00
|
|
|
rescue
|
2015-09-20 21:21:18 +02:00
|
|
|
end
|
2015-02-09 17:06:24 +01:00
|
|
|
end
|
2015-11-30 18:20:54 +01:00
|
|
|
|
2015-12-31 17:30:57 +01:00
|
|
|
# Static Users
|
|
|
|
|
2015-11-30 18:20:54 +01:00
|
|
|
def self.guest
|
|
|
|
@guest || find_or_create_by(name: "Guest") do |user|
|
|
|
|
user.avatar = File.new(File.join("app", "assets", "images", "guest.png"))
|
|
|
|
end
|
|
|
|
end
|
2015-12-01 21:19:35 +01:00
|
|
|
|
|
|
|
def guest?
|
|
|
|
self == User.guest
|
|
|
|
end
|
2015-12-31 17:30:57 +01:00
|
|
|
|
|
|
|
def self.koelkast
|
|
|
|
@koelkast || find_or_create_by(name: "Koelkast") do |user|
|
|
|
|
user.avatar = File.new(File.join("app", "assets", "images", "logo.png"))
|
|
|
|
user.koelkast = true
|
|
|
|
end
|
|
|
|
end
|
2014-11-06 14:46:59 +01:00
|
|
|
end
|