Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8003 (closed enhancement: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Added unless conditional to validations.

Reported by: monki Assigned to: bitsweat
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: minor Keywords: validation conditional verified
Cc: demisone, obrie

Description

Working on some validations earlier I thought it would be much cleaner and easier in some cases to be able to use either :if or :unless on a validation. It better fits my expectations as a rubyist. Also, it simplifies things when you are using a function in the if clause as you won't have to define both an affirmative and negative action.

I wanted it for open_id_authentication, so in my user model I can do something like:

def using_open_id?
  !identity_url.blank?
end

validates_presence_of :identity_url, :if => using_open_id?
validates_presence_of :username, :unless => using_open_id?
validates_presence_of :password, :unless => using_open_id?

I changed the couple lines pertinent to if/unless within validations.rb as well as adding an explanation to the several places that it should come up in that document. As for the tests, I renamed the existing test_conditional_x tests with test_if_x tests. Then I made a copy of each of these name test_unless_x that uses unless and asserts the opposite result. Hopefully that should cover all the bases.

Attachments

add_unless_conditional_to_validations.diff (15.1 kB) - added by monki on 04/05/07 04:40:19.
changes to ActiveRecord validation lib code and tests
add_unless_conditional_to_validations.2.diff (15.1 kB) - added by josh on 04/14/07 04:10:40.
Fixed spelling typo.
add_unless_conditional_to_validations.3.diff (15.2 kB) - added by kampers on 07/20/07 08:33:45.
New revision of patch against [7200] with clarification in docs

Change History

04/05/07 04:40:19 changed by monki

  • attachment add_unless_conditional_to_validations.diff added.

changes to ActiveRecord validation lib code and tests

04/05/07 15:00:24 changed by josh

  • keywords changed from validation conditional to validation conditional verified.
  • severity changed from normal to minor.

04/14/07 04:10:40 changed by josh

  • attachment add_unless_conditional_to_validations.2.diff added.

Fixed spelling typo.

05/10/07 21:16:54 changed by demisone

  • cc set to demisone.

05/19/07 23:02:22 changed by obrie

  • cc changed from demisone to demisone, obrie.

06/08/07 00:23:23 changed by josh

  • owner changed from core to bitsweat.

07/19/07 23:14:30 changed by mpalmer

  • keywords changed from validation conditional verified to validation conditional.

07/20/07 04:47:10 changed by mpalmer

I like this patch, it has a nice symmetry of options to it. Documentation OK, patch applies, tests exercise the changes well, it's all quite tidy. +1 as far as I'm concerned.

07/20/07 08:31:53 changed by kampers

Just tested this out. Tests cover the change, patch applies correctly. And I really dig this idea... it seems obvious. Nice one.

I'd like to suggest a minor change to your API docs, though. Right now you have:

# * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
# not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }).  The
# method, proc or string should return or evaluate to a true or false value.

I think the example should document use of :unless, instead of mirroring the :if doc with different conditions. Also--as long as I'm changing it to :unless => :disallow_validation--I think it might read better as :unless => :skip_validation. It's less double-negativey, which is the whole point of having an unless in ruby.

Other than that minor issue, +1.

07/20/07 08:33:45 changed by kampers

  • attachment add_unless_conditional_to_validations.3.diff added.

New revision of patch against [7200] with clarification in docs

07/21/07 16:48:50 changed by monki

I was just getting a chance to look over your comments (and try to clarify docs a bit), but you beat me to it. Thanks for taking the initiative, it does make the docs much clearer.

07/21/07 22:29:23 changed by mpalmer

  • keywords changed from validation conditional to validation conditional verified.

If we count Josh's setting of verified in April as a +1, we've got 3 points. Core, here we come!

07/24/07 01:48:21 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [7215]) Added :unless clause to validations (closes #8003) [monki]