Skip to content

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

Merged
rafaelfranca merged 1 commit intorails:masterfrom
morgoth:deprecate-some-errors-methods
Feb 18, 2015
Merged

Deprecate ActiveModel::Errors get, set and []= methods.#18634
rafaelfranca merged 1 commit intorails:masterfrom
morgoth:deprecate-some-errors-methods

Conversation

@morgoth
Copy link
Member

@morgoth 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 deprecate-some-errors-methods branch from 65043c5 to cddd4ea Compare January 21, 2015 22:12
@egilburg
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to call delete?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to - already fixed.

@morgoth morgoth force-pushed the deprecate-some-errors-methods branch from cddd4ea to 4d4f8c9 Compare January 22, 2015 19:18
@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

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

@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 deprecate-some-errors-methods branch from 4d4f8c9 to 6ec8ba1 Compare February 1, 2015 12:14
@rafaelfranca
Copy link
Member

Yeah, let deprecated those methods too.

@rafaelfranca rafaelfranca merged commit 6ec8ba1 into rails:master Feb 18, 2015
rafaelfranca added a commit that referenced this pull request Feb 18, 2015
Deprecate `ActiveModel::Errors` `get`, `set` and `[]=` methods.
@morgoth morgoth deleted the deprecate-some-errors-methods branch February 18, 2015 21:18
@chancancode
Copy link
Member

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants