开发者

Overriding Rails Error (Validation) Messages from Gems/Plugins

开发者 https://www.devze.com 2022-12-16 14:26 出处:网络
Is there a generally accepted way of overriding error (validation) messages from a gem/plugin in rails?

Is there a generally accepted way of overriding error (validation) messages from a gem/plugin in rails?

For example开发者_如何学Go, I'm using the ActiveMerchant gem, and if someone puts in an American Express credit card number, but selects 'MasterCard', I get a not-very-descriptive "Type is not the correct card type" error.

I can easily get around this by doing something like this:

def validate_card
  unless credit_card.valid?
    credit_card.errors.full_messages.each do |message|
      if message =~ /is not the correct card type/i
        errors.add_to_base "This credit card number is invalid. 
                            Please ensure that you selected the correct type."
      else
        errors.add_to_base message
      end
    end
  end
end

but this technique quickly becomes unmaintainable and is clearly (at least in my opinion) far from 'best-practices'.

Similarly, I could unpack the ActiveMerchant gem and hack it up to put in my own custom error messages, but that also seems unmaintainable as it would require the same hacks to added into future, unpacked, versions of ActiveMerchant.


In all honesty, you're best bet is to either rewrite parts of the gem/plugin to suit your needs. Unfortunately if you decide to update the gem/plugin at any time you will lose your changes.

However, Ruby is a dynamic language, so classes can be reopened, and you can override methods of any module/class from any file. Through the magic of open source, I've tracked down the module/class/method you'd need to meddle with to achieve your goal.

Put the following in a file and ensure it gets loaded after ActiveMerchant (how to load it is dependent on whether you're using the plugin or the gem)

module ActiveMerchant
  module Billing
    class CreditCard

      private
      def validate_card_number #:nodoc:
        errors.add_to_base "This credit card number is invalid. \n" +
         "Please ensure that you selected the correct type." unless 
            CreditCard.valid_number?(number)
      end
    end
  end
end

N.B.: This method relies on ActiveMerchant internals, which is generally a bad idea. But I see it as the lesser of two evils to maintaing your own version of ActiveMerchant. Should you update the gem/plugin and something the above code relies on has changed, it could break in odd ways.

0

精彩评论

暂无评论...
验证码 换一张
取 消