I have to override spree_user model for adding act...
# general
p
I have to override spree_user model for adding active storage association & need to add validations for password, mobile no, etc... I have tried all the ways which provide by solidus documentation but didn't get solution, can you please explain how to override model file of solidus in ruby on rails? Thank You in advance!!
a
The standard way to do it is using overrides. There's a "Using overrides" section here in the documentation. https://guides.solidus.io/customization/customizing-the-core/#:~:text=In%20the%20Solidus%20ecosystem%2C%20we,override%20extends%20the%20original%20class. Did you try that already? If so what issue did you have?
p
@Alistair Norman Yes, I tried but getting issue of admin panel, i.e. admin user not found is getting at the time of running seed file.
I have to add has_one_attached :profile_image this association in spree_user model, can you please explain me how to add??
a
Can you post the code that you tried adding?
p
Spree::User.class_eval do devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :password, :format => { :with => /^(?=.*[A-Z])(?=.[#!@$&?<>',\[\]}{=\-)(^%`~+.:;_])(?=.*[0-9])(?=.*[a-z])[^ ]{8,}$/, :multiline => true, message: "Password should be a minimum of 8 characters long," \ "contain both uppercase and lowercase characters, at" \ "least one digit, spaces not allowed and one special character " }, if: :password validates :email, :format => { :with => /[^@]+[@][\S]+[.][\S]+/, :multiline => true, } validates :phone_num, presence: true before_validation :parse_phone_num validate :valid_phone_number has_one_attached :profile_image has_one :wishlist, dependent: :destroy has_and_belongs_to_many :spree_roles, join_table: 'spree_roles_users', class_name: 'Spree::Role' # has_many :products, class_name: 'Spree::Product', through: :wishlists, dependent: :destroy private def parse_phone_num @phone = Phonelib.parse(phone_num) self.phone_num = @phone.sanitized end def valid_phone_number unless Phonelib.valid?(phone_num) errors.add(:phone_num, "Invalid or Unrecognized Phone Number") end end end This is the code which I tried
a
What is the exact error that you're encountering? What steps did you take before encountering that error?
There are a few things to look at here: 1. You're using class eval instead of prepend like it suggests in the documentation 2. It's not clear what the error is to me. It sounds like a user is supposed to be created but isn't but you'll have to look at the logs to find out why. 3. Are you adding that whole decorator at once? Try moving in smaller steps and add just one method to the decorator and verify that it's working.
p
Thank You @Alistair Norman, Its working now