tap/app/models/user.rb

58 lines
1.7 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# remember_created_at :datetime
# 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 15:52:25 +00:00
# name :string
# encrypted_password :string default(""), not null
# private :boolean default("f")
#
2014-11-06 13:46:59 +00:00
class User < ActiveRecord::Base
2015-09-18 11:20:57 +00:00
include Statistics, Avatarable, FriendlyId
friendly_id :name, use: :finders
2015-09-14 18:26:16 +00:00
2015-09-18 11:20:57 +00:00
devise :database_authenticatable, :omniauthable, :omniauth_providers => [:zeuswpi]
2015-02-09 16:06:24 +00:00
2014-12-06 16:51:41 +00:00
has_many :orders, -> { includes :products }
has_many :products, through: :orders
2014-12-09 19:28:50 +00:00
belongs_to :dagschotel, class_name: 'Product'
2014-11-06 17:30:53 +00:00
2015-01-06 19:18:01 +00:00
scope :members, -> { where koelkast: false }
2015-09-18 11:20:57 +00:00
scope :publik, -> { where private: false }
2015-01-06 19:18:01 +00:00
2015-03-19 21:37:16 +00:00
def self.from_omniauth(auth)
2015-09-18 11:20:57 +00:00
where(name: auth.uid).first_or_create do |user|
2015-09-14 15:52:25 +00:00
user.name = auth.uid
user.avatar = Identicon.data_url_for auth.uid
2015-03-19 21:37:16 +00:00
end
2015-03-20 01:21:56 +00:00
end
2015-09-20 19:21:18 +00:00
def balance
@balance || begin
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)
if result.code == 200
JSON.parse(result.body)["balance"]
else
0
end
end
2015-02-09 16:06:24 +00:00
end
2014-11-06 13:46:59 +00:00
end