Last active
January 18, 2016 17:39
UniqueTokenValidator Concern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module UniqueTokenValidator | |
extend ActiveSupport::Concern | |
included do | |
validates :unique_token, uniqueness: true, allow_nil: true | |
before_validation :generate_unique_token | |
end | |
def generate_unique_token | |
if !self.class::UNIQUE_FIELDS[:condition].present? || | |
self.class::UNIQUE_FIELDS[:condition].call(self) | |
self.unique_token = Digest::SHA1.hexdigest( | |
self.class::UNIQUE_FIELDS.except(:condition).values.flatten.map { |k| send(k).to_s }.join | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment