Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add origin type to error message #7876

Closed
wants to merge 1 commit into from

5 participants

@fsword

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
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 Collaborator

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

@rafaelfranca Owner

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

@fsword
fsword added a note

@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

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

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
Collaborator

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

@JonRowe

@fsword are you still working on this?

@fsword

@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

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

@steveklabnik
Collaborator

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

@JonRowe

@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
Collaborator

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

@JonRowe

:+1: Thanks for your input/attention @steveklabnik :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @fsword

    add origin type to error message

    fsword authored
    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
    ```
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 2 deletions.
  1. +5 −2 activemodel/lib/active_model/errors.rb
View
7 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 Collaborator

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

@rafaelfranca Owner

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

@fsword
fsword added a note

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ define_method :type do type end
+ end
if exception = options[:strict]
exception = ActiveModel::StrictValidationFailed if exception == true
raise exception, full_message(attribute, message)
Something went wrong with that request. Please try again.