Table name: users
id :integer not null, primary key name :string(255) email :string(255) not null crypted_password :string(255) salt :string(255) activation_state :string(255) activation_token :string(255) activation_token_expires_at :datetime reset_password_token :string(255) reset_password_token_expires_at :datetime reset_password_email_sent_at :datetime remember_me_token :string(255) remember_me_token_expires_at :datetime created_at :datetime updated_at :datetime type :string(255) admin :boolean default(FALSE) uuid :string(255) default("") availability_defaults :text response_time :text searchable :boolean default(FALSE) last_name :string(255) phone :string(255) credits :integer default(0) balanced_student_uri :string(255) credits_booked :integer default(0) points :integer default(0) streak :integer default(0) level :integer default(1) exp_title :string(255) minimum_level :integer default(1) balanced_api_id :text is_featured :boolean default(FALSE) is_registration_completed :boolean default(FALSE) is_manually_deactivated :boolean default(FALSE) billing_address_1 :string(255) billing_address_2 :string(255) billing_city :string(255) billing_state :string(255) billing_zipcode :string(255) shipping_address_1 :string(255) shipping_address_2 :string(255) shipping_city :string(255) shipping_state :string(255) shipping_zipcode :string(255) location :string(255) experience :string(255) summary :text awards :string(255) skills :string(255) industry :string(255) profile_step :integer headline :string(255) onboard_step :integer pending_chat_with :integer cached_notification_count :integer default(0) cached_message_count :integer default(0) auth_token :string(255) university_id :integer level_changed :boolean skills_count :integer default(0) courses_count :integer default(0) rate_in_cents :integer default(2000)
To paginate the pages
# File app/models/tutor.rb, line 140 def self.paginate(options = {}) page(options[:page]).per(options[:per_page]) end
Returns true if bank account is added by user
# File app/models/tutor.rb, line 326 def added_payment_method? bank_uri.present? end
Returns true if courses exists
# File app/models/tutor.rb, line 321 def added_courses? courses.exists? end
Returns true if subjects exists
# File app/models/tutor.rb, line 316 def added_subjects? subjects.exists? end
To find the tutor's availability (i.e. is available for video session or in person session)
# File app/models/tutor.rb, line 180 def availability_types TutorAvailabilityType::TYPES.each_with_index do |availability_type, index| tutor_availability_types.build(availability_type: index) unless tutor_availability_types.exists?(availability_type: index) end tutor_availability_types.sort {|a, b| a.availability_type <=> b.availability_type} end
Returns average rating for the tutoring session
# File app/models/tutor.rb, line 255 def average_rating recieved_reviews.average('average_review') || 0.0 end
Return current #billed_length or 0
# File app/models/tutor.rb, line 196 def billed_hours (self.billed_length / 60.0).round 1 end
Return current #billed_length or 0
# File app/models/tutor.rb, line 191 def billed_length read_attribute(:billed_length) || 0 end
calculate user commission.
# File app/models/tutor.rb, line 301 def calc_commission(user, type = 'TutoringSession') if type == 'TutoringSession' recurring_session = TutoringSession.where("tutor_id = ? and user_id = ? and status = ?", self.id, user.id, 'c').count end if recurring_session > 1 commission_discount else 0 end end
Convert the comma separated category ids into array format.
# File app/models/tutor.rb, line 122 def category_ids=(ids) ids = ids.split(',') if ids.is_a?(String) super(ids) end
Returns commission discount base on user level.
# File app/models/tutor.rb, line 282 def commission_discount case self.user_level when 1 discount = 3 when 2 discount = 5 when 3 discount = 8 when 4 discount = 11 when 5 discount = 13 when 6 discount = 15 end return discount end
Returns tutoring session where current user is student and session is confirmed
# File app/models/tutor.rb, line 232 def confirmed_sessions_as_student TutoringSession.where("user_id = ? and end_time <= ? and status = 'c'",self.id,Time.now.in_time_zone) end
Returns tutoring session where current user is tutor and session is confirmed
# File app/models/tutor.rb, line 227 def confirmed_sessions_as_tutor TutoringSession.where("tutor_id = ? and end_time <= ? and status = 'c'",self.id,Time.now.in_time_zone) end
Returns tutoring session where current user is student
# File app/models/tutor.rb, line 222 def tutoring_sessions_as_student TutoringSession.where("user_id = ? and end_time > ? and ( status is null or status != 'r')", self.id , Time.now.in_time_zone) end
Returns tutoring session where current user is tutor
# File app/models/tutor.rb, line 217 def tutoring_sessions_as_tutor TutoringSession.where("tutor_id = ? and end_time > ? and ( status is null or status != 'r')", self.id , Time.now.in_time_zone ) end
Returns union of common friends and user to whom tutor conducted the tutoring sessions.
# File app/models/tutor.rb, line 237 def invoiceable_users tutored_users.union(common_friends).uniq end
Set user level rate on max session rate range.
# File app/models/tutor.rb, line 263 def max_session_rate case self.user_level when 1 session_rate = 20 when 2 session_rate = 25 when 3 session_rate = 30 when 4 session_rate = 35 when 5 session_rate = 40 when 6 session_rate = 45 end return session_rate end
Return reviews as negative if average count of review is less than 3
# File app/models/tutor.rb, line 245 def negative_reviews self.reviews_received.where("average_review < ?", 3).count end
Return reviews as positive if average count of review is greater than 3
# File app/models/tutor.rb, line 250 def positive_reviews self.reviews_received.where("average_review > ?", 3).count end
Rate in form of currency
# File app/models/tutor.rb, line 172 def rate Money.new (rate_in_cents_with_charge || 0), Money.default_currency end
rate of a tutor in cents
# File app/models/tutor.rb, line 167 def rate=(value) self.rate_in_cents = Money.new(value.to_f * 100.0, Money.default_currency).cents end
Rate in cents with charges of the app
# File app/models/tutor.rb, line 157 def rate_in_cents_with_charge (self.rate_in_cents.nil? ? 0 : self.rate_in_cents) + 500 end
Rate without charges of the app
# File app/models/tutor.rb, line 162 def rate_without_charge Money.new self.rate_in_cents || 0, Money.default_currency end
Convert the comma separated course ids into array format.
# File app/models/tutor.rb, line 134 def course_ids=(ids) ids = ids.split(',') if ids.is_a?(String) super(ids) end
Return total invoiced amount TODO: Denormalize??
# File app/models/tutor.rb, line 202 def total_invoiced_amount(options={ }) query = self.invoices.joins(:tutoring_session).where("tutoring_sessions.status = 'c'") unless options.empty? query = query.where(options) end Money.new query.sum(:payable_amount) end
Return total paid amount TODO: Denormalize??
# File app/models/tutor.rb, line 212 def total_paid_amount total_invoiced_amount paid: true end
Convert the comma separated subject ids into array format.
# File app/models/tutor.rb, line 128 def subject_ids=(ids) ids = ids.split(',') if ids.is_a?(String) super(ids) end
Store nested subjects for this tutor
# File app/models/tutor.rb, line 147 def subject_tokens=(ids) self.subject_ids = ids.split(',').uniq end
Trigger background job to create or update groups
# File app/models/tutor.rb, line 336 def trigger_add_group(object) CreateOrUpdateGroups.perform_async(object.group.id) end
Trigger background job to remove groups
# File app/models/tutor.rb, line 341 def trigger_remove_group(object) object.group.trigger_pusher_group_event end
To update elastic seach index
# File app/models/tutor.rb, line 331 def update_elasticsearch_index(object) Indexer.perform_async(:update, object.class.to_s, object.id) end
validate :validate_rate_base_on_user_level
# File app/models/tutor.rb, line 79 def validate_rate_base_on_user_level if self.rate_in_cents > maximum_rate_as_per_user_level errors.add(:rate, "Your current level is #{self.user_level}. The maximum rate at this level is $ #{self.maximum_rate_as_per_user_level/100}") end end