Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.50 KB | None | 0 0
  1. # == Schema Information
  2. #
  3. # Table name: users
  4. #
  5. #  id             :integer          not null, primary key
  6. #  provider       :string           default("email"), not null
  7. #  uid            :string           default(""), not null
  8. #  sign_in_count  :integer          default("0"), not null
  9. #  name           :string
  10. #  nickname       :string
  11. #  image          :string
  12. #  email          :string
  13. #  created_at     :datetime
  14. #  updated_at     :datetime
  15. #  auth_token     :string
  16. #  first_name     :string
  17. #  last_name      :string
  18. #  gender         :string
  19. #  location       :json
  20. #  birthday       :date
  21. #  active         :boolean          default("true"), not null
  22. #  accepted_terms :boolean          default("false"), not null
  23. #  snapchat       :string
  24. #  youtube        :string
  25. #  instagram      :string
  26. #  musically      :string
  27. #  phone          :string
  28. #  score          :decimal(, )      default("0.0"), not null
  29. #  rank           :integer
  30. #  introduction   :text
  31. #  country_code   :string
  32. #  finalist       :boolean          default("false"), not null
  33. #  eliminated     :boolean          default("false"), not null
  34. #  season_id      :integer
  35. #  facebook       :string
  36. #
  37. # Indexes
  38. #
  39. #  index_users_on_active            (active)
  40. #  index_users_on_auth_token        (auth_token)
  41. #  index_users_on_country_code      (country_code)
  42. #  index_users_on_email             (email)
  43. #  index_users_on_gender            (gender)
  44. #  index_users_on_rank              (rank)
  45. #  index_users_on_score             (score)
  46. #  index_users_on_uid_and_provider  (uid,provider) UNIQUE
  47. #
  48.  
  49. # Main User model
  50. class User < ActiveRecord::Base
  51.   include Elasticsearch::Model
  52.  
  53.   scope :active, -> { where(active: true) }
  54.  
  55.   scope :contestants, -> { joins(:videos).merge( Video.active ) }
  56.  
  57.   # Include to be able to get finalist state etc by default
  58.   default_scope -> {
  59.     includes(
  60.       :weekly_finalists,
  61.       :active_videos,
  62.       :profile_video
  63.     )
  64.   }
  65.  
  66.   # Data to send to ElasticSearch
  67.   def as_indexed_json(_options = {})
  68.     as_json(
  69.       only: [:name, :nickname]
  70.     )
  71.   end
  72.  
  73.   before_create :setup_auth_token
  74.   before_create :set_default_rank
  75.  
  76.   # default to active videos, override if necessary
  77.   has_many :videos, dependent: :destroy
  78.   has_many :active_videos, -> { completed }, class_name: 'Video'
  79.  
  80.   has_many :vlogs, dependent: :destroy
  81.  
  82.   has_many :editorial_user_features, dependent: :destroy
  83.   has_many :editorials, through: :editorial_user_features
  84.  
  85.   # This is a minor mind fuck.
  86.   # Did the user go to any weekly finals?
  87.   has_many :weekly_finalists, dependent: :destroy
  88.  
  89.   has_one :profile_video, dependent: :destroy
  90.  
  91.   def displayed_name
  92.     nickname.present? ? nickname : name
  93.   end
  94.  
  95.   def age
  96.     return unless birthday.present?
  97.  
  98.     age = Date.today.year - birthday.year
  99.     age -= 1 if Date.today < birthday + age.years
  100.  
  101.     age
  102.   end
  103.  
  104.   def finalist?
  105.     weekly_finalists.any?{|e| e.winner? }
  106.   end
  107.  
  108.   def can_compete?(stageId)
  109.     active_stage = Stage.find(stageId)
  110.  
  111.     # Return false if the current stage's voting is closed
  112.     return false if active_stage.voting_end_time < Time.now
  113.  
  114.     # Return if stage is for finalists only
  115.     # and user is not a finalist
  116.     unless finalist?
  117.       return false if active_stage.finalists_only
  118.     end
  119.  
  120.     # Return false if user already have a clip for this stage
  121.     return false if videos.where(stage: active_stage).exists?
  122.  
  123.     # U can haz upload mr user.
  124.     true
  125.   end
  126.  
  127.   ##
  128.   #### RELATIONSHIPS
  129.   ##
  130.   has_many :follower_relations,
  131.            dependent: :destroy,
  132.            foreign_key: :target_id,
  133.            class_name: 'Relation'
  134.   has_many :followers,
  135.            through: :follower_relations,
  136.            source: :source
  137.  
  138.   has_many :following_relations,
  139.            dependent: :destroy,
  140.            foreign_key: :source_id,
  141.            class_name: 'Relation'
  142.   has_many :following,
  143.            through: :following_relations,
  144.            source: :target
  145.  
  146.   # Following helper
  147.   def following?(user)
  148.     following.include? user
  149.   end
  150.  
  151.   # Query by id directly to save foreign load on check
  152.   def following_by_id?(id)
  153.     following.where(id: id).exists?
  154.   end
  155.  
  156.   ##
  157.   ### END RELATIONSHIPS
  158.   ##
  159.  
  160.   private
  161.  
  162.   def setup_auth_token
  163.     self.auth_token = loop do
  164.       random_token = SecureRandom.urlsafe_base64(64, false)
  165.       break random_token unless self.class.exists?(auth_token: random_token)
  166.     end
  167.   end
  168.  
  169.   def set_default_rank
  170.     self.rank = User.active.count + 1
  171.   end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement