2015-09-29 21:21:05 -04:00
|
|
|
class User < ActiveRecord::Base
|
2015-10-09 16:45:53 -04:00
|
|
|
has_paper_trail
|
2015-10-08 15:18:21 -04:00
|
|
|
rolify
|
|
|
|
|
2015-10-12 15:56:57 -04:00
|
|
|
after_create :set_admin, if: :first_user?
|
2015-10-08 15:18:21 -04:00
|
|
|
|
2015-09-29 21:21:05 -04:00
|
|
|
def self.create_with_omniauth(auth)
|
|
|
|
where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
|
|
|
|
user.provider = auth[:provider]
|
|
|
|
user.uid = auth[:uid]
|
|
|
|
user.nickname = auth[:info][:name]
|
|
|
|
user.email = auth[:info][:email]
|
|
|
|
end
|
|
|
|
end
|
2015-10-08 15:18:21 -04:00
|
|
|
|
|
|
|
def admin?
|
|
|
|
self.has_role?(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
def moderator?
|
|
|
|
self.has_role?(:moderator)
|
|
|
|
end
|
|
|
|
|
2015-10-12 15:56:57 -04:00
|
|
|
def first_user?
|
|
|
|
User.unscoped do
|
|
|
|
User.count == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-08 15:18:21 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_admin
|
|
|
|
self.add_role :admin
|
|
|
|
end
|
2015-09-29 21:21:05 -04:00
|
|
|
end
|