i have a validation on the create and save methods for the user model in rails with devise, but i have no clue as to why user instance is created even if promo code is nil and the errors that should be added are not being displayed in my view with google oauth2
does anyone know what the problem is?
This is my route:
devise_for :users, controllers: { confirmations: 'users/confirmations', omniauth_callbacks: 'users/omniauth_callbacks', registrations: 'users/registrations' }
this is my user.rb method:
def self.from_omniauth(auth, promo_code)
user = find_by(email: auth.info.email)
if user
user.update(provider: auth.provider, uid: auth.uid)
else
user = where(provider: auth.provider, uid: auth.uid).first_or_initialize do |new_user|
new_user.email = auth.info.email
new_user.username = "#{auth.info.first_name[0]}#{auth.info.last_name.strip}"
new_user.first_name = auth.info.first_name
new_user.last_name = auth.info.last_name
new_user.password = Devise.friendly_token[0,20]
if promo_code != nil || promo_code != ""
new_user.promo_code = promo_code
end
end
end
user.save
unless user.confirmed?
user.confirm
end
user
end
this is my omniauth_callbacks_controller
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
promo_code = cookies[:promo_code]
@user= User.from_omniauth(request.env['omniauth.auth'], promo_code)
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
end
this is my registrations controller
def new
super
@user = User.new
if session['devise.google_data'] && @user.apply_omniauth(session['devise.google_data'])
@user.valid?
end
end
def create
if session['devise.google_data'] && @user.apply_omniauth(session['devise.google_data'])
super do |resource|
resource.save(validate: true)
end
else
super
end
end
and this is the validation
validate :check_promo_code_limit, on: [:create, :save]
def check_promo_code_limit
promo_codes = {
"MARKI99" => 1,
"PIERRE" => 2,
"BREEZE" => 100
}
if promo_code.nil?
errors.add(:promo_code, :need_promo_code)
elsif promo_codes[promo_code] == nil
errors.add(:promo_code, :inexistant_promo_code)
elsif User.where(promo_code: promo_code).count >= promo_codes[promo_code]
errors.add(:promo_code, :custom_validation_error)
end
end
This is the record that is created with a promo_code that is nil
#<User id: 118, email: "[email protected]", created_at: "2024-01-23 13:09:10.568103000 +0000", updated_at: "2024-01-23 13:09:10.580275000 +0000", first_name: "Marc", last_name: "Bou Saleh", username: "MBou Saleh", foot_amount: 25.0, timezone: nil, max_slippage: 50.0, club_name: nil, show_tutorial: nil, is_expert: nil, tutorial_home: false, trade_modal_tutorial: false, eligible: "To be eligible for this tournament, you are missin...", seen_video: false, is_private: true, is_admin: false, promo_code: nil, is_influencer: false, influencer_promo_code: nil, commission_value: nil, provider: "google_oauth2", uid: "103103713083988234843", seen_video_phone: false>
I solved it this way:
this is new my user.rb