Skip to content

Add AR::Base#valid! method#8639

Merged
rafaelfranca merged 1 commit into
rails:masterfrom
bogdan:valid-with-bang
Jun 27, 2014
Merged

Add AR::Base#valid! method#8639
rafaelfranca merged 1 commit into
rails:masterfrom
bogdan:valid-with-bang

Conversation

@bogdan

@bogdan bogdan commented Dec 28, 2012

Copy link
Copy Markdown
Contributor

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

rrouse commented Dec 28, 2012

Copy link
Copy Markdown

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

@gmile

gmile commented Dec 28, 2012

Copy link
Copy Markdown
Contributor

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

@PikachuEXE

Copy link
Copy Markdown
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

bogdan commented Jan 7, 2013

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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

bogdan commented Jan 7, 2013

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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
Copy Markdown
Contributor

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

@rafaelfranca

Copy link
Copy Markdown
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

JonRowe commented Mar 2, 2013

Copy link
Copy Markdown
Contributor

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

@rafaelfranca

Copy link
Copy Markdown
Member

Yeah, we will revisit this one form 4.1

@vipulnsward

Copy link
Copy Markdown
Member

@rafaelfranca This can be reviewed now I guess?

@egilburg

egilburg commented Sep 7, 2013

Copy link
Copy Markdown
Contributor

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

mikegee commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

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

bogdan commented Jun 23, 2014

Copy link
Copy Markdown
Contributor Author

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