add origin type to error message #7876

Closed
wants to merge 1 commit into
from

Projects

None yet

5 participants

@fsword
fsword commented Oct 8, 2012

Normalize_message will return the localized string for input parameter, 
so the origin value will be lost.
This patch will add the origin type to message object, then you can use
type(more progrmability) to check validate result.
it will be used like this:

post = Post.create(params...)
if post.valid?
  status 200
  ......
elsif post.errors.values.flatten.map(&:type).include? :taken
  status 409
  ......
else
  status 400
end
@fsword fsword add origin type to error message
Normalize_message will return the localized string for input parameter, 
so the origin value will be lost. 
This patch will add the origin type to message object, then you can use 
type(more progrmability) to check validate result.
it will be used like this:
```ruby
post = Post.create(params...)
if post.valid?
  status 200
  ......
elsif post.errors.values.flatten.map(&:type).include? :taken
  status 409
  ......
else
  status 400
end
```
e6ada71
@steveklabnik steveklabnik commented on the diff Oct 8, 2012
activemodel/lib/active_model/errors.rb
@@ -291,8 +291,11 @@ def to_hash(full_messages = false)
# # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
- def add(attribute, message = nil, options = {})
- message = normalize_message(attribute, message, options)
+ def add(attribute, type = nil, options = {})
+ message = normalize_message(attribute, type, options)
+ message.singleton_class.class_eval do
@steveklabnik
steveklabnik Oct 8, 2012 Member

This is gonna be Real Bad for performance, isn't it?

@rafaelfranca
rafaelfranca Oct 8, 2012 Member

Also the second argument is not a type it is a message, so it can be a string.

@fsword
fsword Oct 8, 2012

@stevehlabnik Yes, it is a sample code which is poor performance here. I wish somebody could write a new version
@rafaelfranca type might be called label

@fsword
fsword commented Oct 26, 2012

That isn't what I want. We want to know the model error type without field information sometimes.
eg: when Post.new(args) is duplicated, I will set http status by 409, so I just check errors type( whatever the field name is )

@steveklabnik
Member

I'm sorry, I'm still not 100% sure what this actually does. Can you show a slightly bigger example?

@JonRowe
Contributor
JonRowe commented Mar 5, 2013

@fsword are you still working on this?

@fsword
fsword commented Mar 6, 2013

@JonRowe I made a patch on my project. Like this:

# -*- encoding : utf-8 -*-
# patch/errors
require 'active_model'

module ActiveModel
  class ErrorMessage < String
    def initialize msg, type
      super msg
      @type = type
    end

    def type
      @type
    end
  end

  class Errors
    alias origin_generate_message generate_message
    def generate_message(attribute, type = :invalid, options = {})
      ErrorMessage.new origin_generate_message(attribute, type, options), type
    end
  end
end
@JonRowe
Contributor
JonRowe commented Mar 6, 2013

Is this something that would be useful to Rails in general? /cc @steveklabnik

@steveklabnik
Member

I have no idea, my question in #7876 (comment) is still not answered.

@JonRowe
Contributor
JonRowe commented Mar 6, 2013

@fsword Can you provide @steveklabnik with a bigger example to show what this does in practise? What does the output look like without it and what does it look like with it? Have you solved any performance issues?

If you're happy with this being a local patch on your project maybe it's worth closing the issue.

@steveklabnik
Member

Yes, given the months this is open, I'm giving it a close until we can demonstrate its value.

@JonRowe
Contributor
JonRowe commented Mar 6, 2013

👍 Thanks for your input/attention @steveklabnik :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment