In my User model, I have the usual suspects of email, first_name, last_name, password, etc.
I have several cases where I need to skip all or some of the validations.
Currently, I have an unless condition that looks something like:
validates :first_name, presence: :true, etc..., unless: :skip_first_name_validation?
validates :last_name, presence: :true, etc..., unless: :skip_last_name_validation?
validates :email, presence: :true, etc..., unless: :skip_email_validation?
Of course I also have:
attr_accessor :skip_first_name_validation, :skip_last_name_validation, etc.
And then I use private methods to check the state of each:
def skip_first_name_validation?
skip_first_name_validation
end
def skip_last_name_validation?
skip_last_name_validation
end
def skip_email_validation?
skip_email_validation
end
etc..
From there, whenever I need to skip validations, I just assign each one of these guys a true
value in my controller.
So while all of this works fine, I'm wondering if there's a more elegant way?
Ideally it would be nice if I could use a simple conditional like this for each attribute in my models:
:skip_validation?
And in my controllers, just do something like:
skip_validation(:first_name, :last_name, :password) = true
Can someone offer a suggestion for how I might program this? I'd prefer not using an existing gem/library, but am trying to understand how to program this behavior in rails. Thanks.