Advertisement
Guest User

Untitled

a guest
Sep 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. include MailboxMethods
  3. include Forumable
  4. include Statusable
  5. include StripeWrapper
  6. acts_as_paranoid
  7. devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
  8.  
  9. unless Rails.env.test?
  10. geocoded_by :full_location
  11. before_save :geocode
  12. end
  13. before_validation :find_free_username!, :clean_training_types!
  14. after_create :send_default_welcome_message
  15. after_update :check_and_change_status_pets_listings, if: proc { |user| user.status_changed? }
  16.  
  17.  
  18. has_attached_file :avatar,
  19. styles: {
  20. medium: '300x300#',
  21. thumb: '100x100>'
  22. },
  23. default_url: '/images/default_avatars/:style.png',
  24. path: '/user/:attachment/:id/:style/:filename',
  25. storage: :s3
  26.  
  27. has_attached_file :business_logo,
  28. styles: {
  29. medium: '300x300',
  30. thumb: '100x100>'
  31. },
  32. default_url: '/images/defaultlogo.png',
  33. path: '/user/:attachment/:id/:style/:filename',
  34. storage: :s3
  35.  
  36. validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  37. validates_attachment_content_type :business_logo, content_type: /\Aimage\/.*\Z/
  38.  
  39. validates :username, uniqueness: { case_sensitive: false }
  40. validates_acceptance_of :agreement, allow_nil: false, accept: true, on: :create
  41. validates :postalcode, length: { maximum: 10 }
  42.  
  43. has_many :facility_photos, class_name: 'User::FacilityPhoto', foreign_key: :user_id, dependent: :destroy
  44.  
  45. has_many :follow_to_users, class_name: 'Follower', foreign_key: 'user_id', dependent: :destroy
  46. has_many :followers, through: :follow_to_users, source: :followed
  47. delegate :count, to: :followers, prefix: true, allow_nil: true
  48.  
  49. has_many :followed_users, class_name: 'Follower', foreign_key: 'followed_id', dependent: :destroy
  50. has_many :follows, through: :followed_users, source: :user
  51. delegate :count, to: :follows, prefix: true, allow_nil: true
  52.  
  53. has_many :user_like_pets, class_name: 'LikePet', foreign_key: 'user_id', dependent: :destroy
  54. has_many :liked_pets, through: :user_like_pets, source: :liked_pet
  55.  
  56. has_many :pets, dependent: :destroy
  57. has_many :listings, dependent: :destroy
  58.  
  59. has_many :simple_pets, dependent: :destroy
  60.  
  61. has_many :breeder_ratings
  62. has_many :approwed_ratings, -> { where(aasm_state: BreederRating::STATUS_APPROVED).where.not(rating: nil) }, class_name: 'BreederRating'
  63. has_many :rated_users, -> { where.not(rating: nil) }, class_name: 'BreederRating', foreign_key: 'user_id', dependent: :destroy
  64. has_many :raters, through: :rated_users, source: :reviewer
  65.  
  66. has_many :picked_pets, class_name: 'PickPet', foreign_key: 'user_id', dependent: :destroy
  67. has_many :pick_pets, through: :picked_pets, source: :pet
  68.  
  69. has_many :picked_listings, class_name: 'PickListing', foreign_key: 'user_id', dependent: :destroy
  70. has_many :pick_listings, through: :picked_listings, source: :listing
  71.  
  72. has_many :social_media, class_name: 'UserSocialMedia', foreign_key: :user_id
  73.  
  74. has_many :invitations
  75.  
  76. has_many :contacts, -> { active }
  77. has_many :invoices
  78.  
  79. enum status: %w(not_active active banned unclaimed)
  80. enum upgrade_status: %w(basic_subscriber advanced_subscriber expert_subscriber)
  81.  
  82. serialize :training_types, Array
  83.  
  84. class << self
  85. def pickapaw_admin
  86. User.find_by_email('admin@pickapaw.com')
  87. end
  88. alias_method :find_admin, :pickapaw_admin
  89.  
  90. def create_user_with_password(email, name)
  91. user = where(email: email).first_or_create
  92. if user.new_record?
  93. password = SecureRandom.hex(4)
  94. firstname, lastname = name.split(' ')
  95. user.skip_confirmation!
  96. user.update_attributes(firstname: firstname, lastname: lastname, password: password, agreement: 1)
  97. end
  98. [user, password]
  99. end
  100. end
  101.  
  102. def subscription_name
  103. upgrade_status.split('_').first.titleize
  104. end
  105.  
  106. def invite_user_from_contact(contact)
  107. invited_user, password = (contact.breeder? ? User::Breeder : User).create_user_with_password(contact.email, contact.fullname)
  108. invited_user.update({
  109. firstname: contact.first_name,
  110. lastname: contact.last_name,
  111. address: contact.address,
  112. city: contact.city,
  113. country: contact.country,
  114. phone: contact.phone,
  115. state: contact.state
  116. })
  117. UserMailer.invite_user_from_sender(self, invited_user, password).deliver_now if password
  118. invited_user
  119. end
  120.  
  121. def last4_card_digits
  122. get_last4_digits(self)
  123. rescue Stripe::InvalidRequestError
  124. 'XXXX'
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement