Guest User

Untitled

a guest
Mar 3rd, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.36 KB | None | 0 0
  1. class UserProxyValidator < ActiveModel::EachValidator
  2.   def validate_each(record, field, value)
  3.     dummy = User.new({
  4.       email: "dummy@fictitious.com",
  5.       password: value,
  6.       password_confirmation: value
  7.     })
  8.     if(!dummy.valid?)
  9.       dummy.errors[field].each do |msg|
  10.         record.errors[field] << msg
  11.       end
  12.     end
  13.   end
  14. end
  15.  
  16. class ExistingUserValidator < ActiveModel::EachValidator
  17.   def validate_each(record, field, value)
  18.     dummy = User.find_by_reset_password_token(value)
  19.     if(dummy.nil?)
  20.       record.errors[field] << "Invalid invite code or account already activated"
  21.     end
  22.   end
  23. end
  24.  
  25. class Invite
  26.   include Informal::Model
  27.   attr_accessor :user
  28.   attr_accessor :uid
  29.   attr_accessor :first_name
  30.   attr_accessor :last_name
  31.   attr_accessor :company
  32.   attr_accessor :email
  33.   attr_accessor :terms_and_conditions
  34.   attr_accessor :password
  35.   attr_accessor :password_confirmation
  36.  
  37.   after_initialize do
  38.   end
  39.  
  40.   validates :uid, presence: true, existing_user: true
  41.   validates :password, confirmation: true, user_proxy: true
  42.   validates_acceptance_of :terms_and_conditions
  43.  
  44.   def initialize(data)
  45.     @uid = data[:uid]
  46.     @terms_and_conditions = data[:terms_and_conditions] || false
  47.     super(data)
  48.   end
  49.  
  50.   def save
  51.     if(valid?)
  52.       @user.reset_password!(password, password_confirmation)
  53.     end
  54.   end
  55. end
Add Comment
Please, Sign In to add comment