Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # == Schema Information
- #
- # Table name: users
- #
- # id :integer not null, primary key
- # provider :string default("email"), not null
- # uid :string default(""), not null
- # sign_in_count :integer default("0"), not null
- # name :string
- # nickname :string
- # image :string
- # email :string
- # created_at :datetime
- # updated_at :datetime
- # auth_token :string
- # first_name :string
- # last_name :string
- # gender :string
- # location :json
- # birthday :date
- # active :boolean default("true"), not null
- # accepted_terms :boolean default("false"), not null
- # snapchat :string
- # youtube :string
- # instagram :string
- # musically :string
- # phone :string
- # score :decimal(, ) default("0.0"), not null
- # rank :integer
- # introduction :text
- # country_code :string
- # finalist :boolean default("false"), not null
- # eliminated :boolean default("false"), not null
- # season_id :integer
- # facebook :string
- #
- # Indexes
- #
- # index_users_on_active (active)
- # index_users_on_auth_token (auth_token)
- # index_users_on_country_code (country_code)
- # index_users_on_email (email)
- # index_users_on_gender (gender)
- # index_users_on_rank (rank)
- # index_users_on_score (score)
- # index_users_on_uid_and_provider (uid,provider) UNIQUE
- #
- # Main User model
- class User < ActiveRecord::Base
- include Elasticsearch::Model
- scope :active, -> { where(active: true) }
- scope :contestants, -> { joins(:videos).merge( Video.active ) }
- # Include to be able to get finalist state etc by default
- default_scope -> {
- includes(
- :weekly_finalists,
- :active_videos,
- :profile_video
- )
- }
- # Data to send to ElasticSearch
- def as_indexed_json(_options = {})
- as_json(
- only: [:name, :nickname]
- )
- end
- before_create :setup_auth_token
- before_create :set_default_rank
- # default to active videos, override if necessary
- has_many :videos, dependent: :destroy
- has_many :active_videos, -> { completed }, class_name: 'Video'
- has_many :vlogs, dependent: :destroy
- has_many :editorial_user_features, dependent: :destroy
- has_many :editorials, through: :editorial_user_features
- # This is a minor mind fuck.
- # Did the user go to any weekly finals?
- has_many :weekly_finalists, dependent: :destroy
- has_one :profile_video, dependent: :destroy
- def displayed_name
- nickname.present? ? nickname : name
- end
- def age
- return unless birthday.present?
- age = Date.today.year - birthday.year
- age -= 1 if Date.today < birthday + age.years
- age
- end
- def finalist?
- weekly_finalists.any?{|e| e.winner? }
- end
- def can_compete?(stageId)
- active_stage = Stage.find(stageId)
- # Return false if the current stage's voting is closed
- return false if active_stage.voting_end_time < Time.now
- # Return if stage is for finalists only
- # and user is not a finalist
- unless finalist?
- return false if active_stage.finalists_only
- end
- # Return false if user already have a clip for this stage
- return false if videos.where(stage: active_stage).exists?
- # U can haz upload mr user.
- true
- end
- ##
- #### RELATIONSHIPS
- ##
- has_many :follower_relations,
- dependent: :destroy,
- foreign_key: :target_id,
- class_name: 'Relation'
- has_many :followers,
- through: :follower_relations,
- source: :source
- has_many :following_relations,
- dependent: :destroy,
- foreign_key: :source_id,
- class_name: 'Relation'
- has_many :following,
- through: :following_relations,
- source: :target
- # Following helper
- def following?(user)
- following.include? user
- end
- # Query by id directly to save foreign load on check
- def following_by_id?(id)
- following.where(id: id).exists?
- end
- ##
- ### END RELATIONSHIPS
- ##
- private
- def setup_auth_token
- self.auth_token = loop do
- random_token = SecureRandom.urlsafe_base64(64, false)
- break random_token unless self.class.exists?(auth_token: random_token)
- end
- end
- def set_default_rank
- self.rank = User.active.count + 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement