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

Deprecate `ActiveModel::Errors` `get`, `set` and `[]=` methods. #18634

Merged
merged 1 commit into from Feb 18, 2015

Conversation

@morgoth
Copy link
Member

morgoth commented Jan 21, 2015

They have inconsistent behaviour currently.

As discussed in #18631

Do you think we can go further and deprecate also add_on_blank and add_on_empty?
Not sure what is the usecase for them and it's easy enough to write it on your own when needed.

@morgoth morgoth force-pushed the morgoth:deprecate-some-errors-methods branch Jan 21, 2015
@egilburg
Copy link
Contributor

egilburg commented Jan 22, 2015

In complex service objects, we use add_on_empty quite a lot, as there are several pathways that may lead to the same error key being set, and I don't want the same error populated multiple times and dumped as redundant text on user validation error display. Sure I could write my own check, but why not keep this simple helper?

@rafaelfranca
rafaelfranca reviewed Jan 22, 2015
View changes
activemodel/lib/active_model/errors.rb Outdated
ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1

To achieve the same use
delete(:#{key}) followed by

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jan 22, 2015

Member

Does it need to call delete?

This comment has been minimized.

Copy link
@morgoth

morgoth Jan 22, 2015

Author Member

It doesn't need to - already fixed.

@morgoth morgoth force-pushed the morgoth:deprecate-some-errors-methods branch Jan 22, 2015
@morgoth
Copy link
Member Author

morgoth commented Jan 22, 2015

@egilburg I can see that add_on_empty is a "light" version of PresenceValidator, but add_on_blank is exactly the same as PresenceValidator, so maybe at least this one can be deprecated?

@egilburg
Copy link
Contributor

egilburg commented Jan 22, 2015

Huh? I thought add on empty means add error if model doesn't have already error with that key.

Eugene

On Jan 22, 2015, at 11:23 AM, Wojciech Wnętrzak notifications@github.com wrote:

@egilburg I can see that add_on_empty is a "light" version of PresenceValidator, but add_on_blank is exactly the same, so maybe at least this one can be deprecated?


Reply to this email directly or view it on GitHub.

@morgoth
Copy link
Member Author

morgoth commented Jan 22, 2015

@egilburg Nope, it adds :empty error when given value is empty.

u = User.new
u.errors.add(:email, :invalid)
u.errors.add_on_empty(:email)
# => [:email]
u.errors.full_messages
# => ["Email is invalid", "Email can't be empty"]

It also adds this error when value is nil, so it behaves almost exactly as add_on_blank

@morgoth
Copy link
Member Author

morgoth commented Jan 25, 2015

@egilburg Did I understand you correctly, that you misused add_on_empty method? If yes, would you agree now on deprecating it?

@egilburg
Copy link
Contributor

egilburg commented Jan 25, 2015

@morgoth yes it doesn't seem to be what I thought it is.

@morgoth
Copy link
Member Author

morgoth commented Jan 27, 2015

@rafaelfranca this PR is good to go. WDYT about deprecating add_on_empty and add_on_blank as stated above?

They have inconsistent behaviour currently.
@morgoth morgoth force-pushed the morgoth:deprecate-some-errors-methods branch to 6ec8ba1 Feb 1, 2015
@rafaelfranca
Copy link
Member

rafaelfranca commented Feb 18, 2015

Yeah, let deprecated those methods too.

@rafaelfranca rafaelfranca merged commit 6ec8ba1 into rails:master Feb 18, 2015
1 check passed
1 check passed
continuous-integration/travis-ci The Travis CI build passed
Details
rafaelfranca added a commit that referenced this pull request Feb 18, 2015
Deprecate `ActiveModel::Errors` `get`, `set` and `[]=` methods.
@morgoth morgoth deleted the morgoth:deprecate-some-errors-methods branch Feb 18, 2015
@chancancode
Copy link
Member

chancancode commented Feb 20, 2015

For people coming here from This week in Rails, see f55bfe7.

Summary:

  • model.errors[:name]
  • model.errors.add(:name, ...)
  • 👎 model.errors.get(:name)
  • 👎 model.errors.set(:name, ...)
  • 👎 model.errors[:name] = ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.