Skip to content

Instantly share code, notes, and snippets.

@trdev7
Created June 8, 2020 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trdev7/0dfd7b175415f0f6124ad7eee36e32dd to your computer and use it in GitHub Desktop.
Save trdev7/0dfd7b175415f0f6124ad7eee36e32dd to your computer and use it in GitHub Desktop.
User model on Bookthatapp
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default("")
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# 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)
# name :string(255)
# title :string(255)
# created_at :datetime
# updated_at :datetime
# uuid :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# invitation_token :string(255)
# invitation_created_at :datetime
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_id :integer
# invited_by_type :string(255)
# google_token :string(255)
# google_refresh_token :string(255)
#
# Indexes
#
# index_users_on_confirmation_token (confirmation_token) UNIQUE
# index_users_on_invitation_token (invitation_token) UNIQUE
#
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable #, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :invitable,
invite_for: 2.weeks
has_many :resources
has_many :location_constraints, through: :resources
has_many :locations, through: :location_constraints
has_many :shops, through: :roles, class_name: 'Shop', source: :resource, source_type: 'Shop'
has_many :user_events, dependent: :destroy
has_many :users_roles, dependent: :destroy
has_one :google_calendar, dependent: :destroy
ROLES = {admin: 0, staff: 1}
has_paper_trail on: []
attr_accessor :is_new
attr_accessor :invite_to_shop
validates_uniqueness_of :email
validates_with EmailAddress::ActiveRecordValidator, field: :email
def valid_shop?(shop)
return false unless shop
shops.find_by(id: shop.id).present?
end
def remember_me
true
end
def password_match?
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
def admin_for_shop?(shop)
has_role? :admin, shop
end
def role(shop)
roles.where(resource_id: shop, resource_type: 'Shop').first
end
def users_role(shop)
users_roles.joins(:role).find_by(roles: { resource_id: shop, resource_type: 'Shop' })
end
def invited(shop)
users_role(shop).invited rescue false
end
def to_liquid
{
email: email,
name: name
}
end
def invited_for_shop!(shop)
users_roles.joins(:role).find_by(roles: { resource_id: shop, resource_type: 'Shop' }).update(invited: true) rescue false
end
def has_password?
self.encrypted_password.present?
end
def reset_password_token!
token = self.set_reset_password_token
end
def google_auth
{
access_token: google_token,
refresh_token: google_refresh_token
}
end
def google_init?
google_token.present? && google_refresh_token.present?
end
def destroy_google_token
self.update_attributes({ google_token: nil })
google_calendar.destroy if google_calendar.present?
end
def booking_items_for_range(start, finish)
BookingItem.where('(? <= finish) AND (start <= ?)', start, finish).
includes(:booking).joins(:resources).where('resources.user_id = ?', self.id)
end
def user_events_for_range(start, finish)
UserEvent.for_user_and_timerange(self, start, finish)
end
def remove_from_shop(shop, current_user)
remove_role :admin, shop
remove_role :staff, shop
destroy if roles.blank?
create_timeline_record('deleted_from_shop', shop, current_user)
end
def make_admin(shop, current_user)
was_invited = invited(shop)
remove_role :staff, shop
add_role :admin, shop
invited_for_shop!(shop) if was_invited
create_timeline_record(:make_admin, shop, current_user)
end
def make_staff(shop, current_user)
was_invited = invited(shop)
remove_role :admin, shop
add_role :staff, shop
invited_for_shop!(shop) if was_invited
create_timeline_record(:make_staff, shop, current_user)
end
def add_admin(shop, current_user)
add_role(:admin, shop)
invited_for_shop!(shop)
create_timeline_record('admin_added', shop, current_user)
shop.enable_user_auth!
end
def add_staff(shop, current_user)
add_role(:staff, shop)
create_timeline_record('staff_added', shop, current_user)
end
def create_timeline_record(action, shop, current_user)
PaperTrail::Version.create(paper_trail_params(action, shop, current_user))
end
def location_constrainted?(shop)
locations.where(shop_id: shop).present?
end
def restricted_by_location?(shop)
has_role?(:staff, shop) && location_constrainted?(shop)
end
private
def paper_trail_params(action, shop, current_user)
{
item_type: 'User',
whodunnit: current_user.present? ? current_user.name : I18n.t('user.merchant'),
item_id: self.id,
event: action,
shop_id: shop.id,
user_id: current_user.try(:id)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment