How to override validation call in activerecord in gem?

I am using apn_on_rails to alert using iphone push with rails.

At the moment, the check on the token is no longer valid, because the check requires a space every 8 characters: validates_format_of: token, with => / ^ [a-z0-9] {8} \ s [a-z0-9] { 8} \ s [a-z0-9] {8} \ s [a-z0 -9] {8} \ s [A-z0-9] {8} \ s [A-z0-9] {8} \ s [A-z0-9] {8} \ s [A-z0-9] {8} $ /

http://github.com/markbates/apn_on_rails/blob/master/lib/apn_on_rails/app/models/apn/device.rb

But the device token I got from Objective-C does not have a place.

So I would like to override the check to do this: validates_format_of: token: with => / ^ [a-z0-9] {64} $ /

How can I do this without changing the source inside the gem?

Thank.

+3
source share
3 answers

Or do a code check:

[56, 48, 40, 32, 24, 16, 8].each { |i| code.insert(i, ' ') }
+1
source

You are not the first in this matter - I found the following article entitled Useful Information on Rails Validation with Metaprogramming .

+1
source

, , . , . , wesgarrison :

def token= token
  @token = [56, 48, 40, 32, 24, 16, 8].each { |i| token.insert(i, ' ') }
end

- , .

+1

Source: https://habr.com/ru/post/1732812/


All Articles