Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- include MailboxMethods
- include Forumable
- include Statusable
- include StripeWrapper
- acts_as_paranoid
- devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
- unless Rails.env.test?
- geocoded_by :full_location
- before_save :geocode
- end
- before_validation :find_free_username!, :clean_training_types!
- after_create :send_default_welcome_message
- after_update :check_and_change_status_pets_listings, if: proc { |user| user.status_changed? }
- has_attached_file :avatar,
- styles: {
- medium: '300x300#',
- thumb: '100x100>'
- },
- default_url: '/images/default_avatars/:style.png',
- path: '/user/:attachment/:id/:style/:filename',
- storage: :s3
- has_attached_file :business_logo,
- styles: {
- medium: '300x300',
- thumb: '100x100>'
- },
- default_url: '/images/defaultlogo.png',
- path: '/user/:attachment/:id/:style/:filename',
- storage: :s3
- validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
- validates_attachment_content_type :business_logo, content_type: /\Aimage\/.*\Z/
- validates :username, uniqueness: { case_sensitive: false }
- validates_acceptance_of :agreement, allow_nil: false, accept: true, on: :create
- validates :postalcode, length: { maximum: 10 }
- has_many :facility_photos, class_name: 'User::FacilityPhoto', foreign_key: :user_id, dependent: :destroy
- has_many :follow_to_users, class_name: 'Follower', foreign_key: 'user_id', dependent: :destroy
- has_many :followers, through: :follow_to_users, source: :followed
- delegate :count, to: :followers, prefix: true, allow_nil: true
- has_many :followed_users, class_name: 'Follower', foreign_key: 'followed_id', dependent: :destroy
- has_many :follows, through: :followed_users, source: :user
- delegate :count, to: :follows, prefix: true, allow_nil: true
- has_many :user_like_pets, class_name: 'LikePet', foreign_key: 'user_id', dependent: :destroy
- has_many :liked_pets, through: :user_like_pets, source: :liked_pet
- has_many :pets, dependent: :destroy
- has_many :listings, dependent: :destroy
- has_many :simple_pets, dependent: :destroy
- has_many :breeder_ratings
- has_many :approwed_ratings, -> { where(aasm_state: BreederRating::STATUS_APPROVED).where.not(rating: nil) }, class_name: 'BreederRating'
- has_many :rated_users, -> { where.not(rating: nil) }, class_name: 'BreederRating', foreign_key: 'user_id', dependent: :destroy
- has_many :raters, through: :rated_users, source: :reviewer
- has_many :picked_pets, class_name: 'PickPet', foreign_key: 'user_id', dependent: :destroy
- has_many :pick_pets, through: :picked_pets, source: :pet
- has_many :picked_listings, class_name: 'PickListing', foreign_key: 'user_id', dependent: :destroy
- has_many :pick_listings, through: :picked_listings, source: :listing
- has_many :social_media, class_name: 'UserSocialMedia', foreign_key: :user_id
- has_many :invitations
- has_many :contacts, -> { active }
- has_many :invoices
- enum status: %w(not_active active banned unclaimed)
- enum upgrade_status: %w(basic_subscriber advanced_subscriber expert_subscriber)
- serialize :training_types, Array
- class << self
- def pickapaw_admin
- User.find_by_email('admin@pickapaw.com')
- end
- alias_method :find_admin, :pickapaw_admin
- def create_user_with_password(email, name)
- user = where(email: email).first_or_create
- if user.new_record?
- password = SecureRandom.hex(4)
- firstname, lastname = name.split(' ')
- user.skip_confirmation!
- user.update_attributes(firstname: firstname, lastname: lastname, password: password, agreement: 1)
- end
- [user, password]
- end
- end
- def subscription_name
- upgrade_status.split('_').first.titleize
- end
- def invite_user_from_contact(contact)
- invited_user, password = (contact.breeder? ? User::Breeder : User).create_user_with_password(contact.email, contact.fullname)
- invited_user.update({
- firstname: contact.first_name,
- lastname: contact.last_name,
- address: contact.address,
- city: contact.city,
- country: contact.country,
- phone: contact.phone,
- state: contact.state
- })
- UserMailer.invite_user_from_sender(self, invited_user, password).deliver_now if password
- invited_user
- end
- def last4_card_digits
- get_last4_digits(self)
- rescue Stripe::InvalidRequestError
- 'XXXX'
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement