Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AR::Base#valid! method #8639

Merged
merged 1 commit into from
Jun 27, 2014
Merged

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Dec 28, 2012

This is helper method in case when some operations should be done on a record before saving. But they doesn't make sence if record is invalid.

def create_developer_from_linkedin(parameters)
  d = Developer.new(parameters)
  d.valid!
  d.import_linkedin_profile
  d.save!
end

@rrouse
Copy link

rrouse commented Dec 28, 2012

Why would this be preferred over just doing "if valid?...."?

@gmile
Copy link
Contributor

gmile commented Dec 28, 2012

Indeed, @bogdan, can you show a case where #valid? wouldn't be sufficient?

@PikachuEXE
Copy link
Contributor

@bogdan I can rewrite the code provided with this:

def create_developer_from_linkedin(parameters)
  d = Developer.new(parameters)
  if d.valid?
    d.import_linkedin_profile
    d.save!
  end
  # d # if you still want the invalid record returned
end

So you might want to provide another example where valid? does not work but valid! does

@bogdan
Copy link
Contributor Author

bogdan commented Jan 7, 2013

I need to raise exception instead of returning something. This required me to reverse engineer AR::RecordInvalid and see in source how it works. Currently my code looks like this:

def create_developer_from_linkedin(parameters)
  d = Developer.new(parameters)
  if d.valid?
    d.import_linkedin_profile
    d.save!
  else
    raise ActiveRecord::RecordInvalid.new(d)
  end
end

@PikachuEXE
Copy link
Contributor

I think you might want to post the code the make use of the exception raised
Then we can start discussing whether the exception is needed or not

@bogdan
Copy link
Contributor Author

bogdan commented Jan 7, 2013

@PikachuEXE There are a lot of factors why it's raised including subjective ones like "someone did that long time ago with 0% test coverage". I don't want to explain those details. But I think rails should be flexible enough to give a developer ability to choose between raise exception or return value.

@PikachuEXE
Copy link
Contributor

Well since rails 4 are having new destroy! and other bang methods
I think adding this method should be ok
But the name might better be validate! instead of valid!

Anyway it's up to the core members to decide.

@timraymond
Copy link
Contributor

I believe this needs a CHANGELOG entry. Also, could you please document valid!

@rafaelfranca
Copy link
Member

We are going to review all this API in the next release so we'll not accept this right now.

I'll leave this issue open to remember.

@ghost ghost assigned rafaelfranca Jan 25, 2013
@JonRowe
Copy link
Contributor

JonRowe commented Mar 2, 2013

Now that the Rails 4 beta is out, has this been reviewed?

@rafaelfranca
Copy link
Member

Yeah, we will revisit this one form 4.1

@vipulnsward
Copy link
Member

@rafaelfranca This can be reviewed now I guess?

@egilburg
Copy link
Contributor

egilburg commented Sep 7, 2013

I think .validate! makes more sense than .valid!. But I agree it's a nice PR.

I wonder if save! can be refactored to just call validate! then. Essentially no new code would be added, just the invalid error raising message would be pushed from .save! method to .validate! method.

@mikegee
Copy link
Contributor

mikegee commented Jun 21, 2014

This is quite stale now. What is the current blocker to merging this?

I also prefer #validate! over #valid!.

Acts same as valid? but raises AR::RecordInvalid exception
if validation fails
@bogdan
Copy link
Contributor Author

bogdan commented Jun 23, 2014

Renamed method to validate! as most people voted for it.
@rafaelfranca waiting for your review whenever you have time.

@rafaelfranca rafaelfranca merged commit 0fa4c95 into rails:master Jun 27, 2014
rafaelfranca added a commit that referenced this pull request Jun 27, 2014
@bogdan bogdan deleted the valid-with-bang branch January 29, 2016 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants