Feature request: add .on(:create) and .on(:update) for model validation matchers #131

Closed
andmej opened this Issue Jul 11, 2012 · 8 comments

7 participants

@andmej

Hi,

I would love to see something like:

describe Monkey do
    it { should validate_presence_of(:banana).on(:create) }
    it { should_not validate_presence_of(:age).on(:update) }
end

What do you think? Would that be possible?

@calebthompson
thoughtbot, inc. member

Validation options for validates_presence_of:

  • message - A custom error message (default is: "can‘t be blank").
  • on - Specifies when this validation is active (default is :save, other options :create, :update).
  • if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_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.
  • unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.
  • strict - Specifies whether validation should be strict.

https://github.com/rails/rails/blob/3-2-stable/activemodel/lib/active_model/validations/presence.rb

@andmej

@calebthompson, yes, those are the options for validates_presence_of. What's your point?

@calebthompson
thoughtbot, inc. member

I was planning to take a stab at this later, just wanted to have those as notes for myself.

@jsmestad

Would love to have this

@gabebw
thoughtbot, inc. member

I agree that this would be a nice feature. I'll look into this, but I'd make more time for a pull request implementing this.

@calebthompson calebthompson added a commit to calebthompson/shoulda-matchers that referenced this issue Aug 18, 2012
@calebthompson calebthompson [#131] Refactor validate_presence_of_matcher
I'll be adding a number of new options, so I'm refactoring to use options hash
so that all of the arguments can be stored in once place.
373b0d8
@kitop

It would be really great to see something like this.

@brodock

+1 on this too

@drapergeek
thoughtbot, inc. member

This was added in #246. We will be releasing a new version which includes it later today.

@drapergeek drapergeek closed this May 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment