Sha256: 2ad23354feac68682ae279a04b1f499136fdfb8e6fc9a6f63486cb21c10957de

Contents?: true

Size: 647 Bytes

Versions: 2

Compression:

Stored size: 647 Bytes

Contents

module JT
	module Rails
	end
end

module JT::Rails::Tokenizable
	extend ActiveSupport::Concern

	included do
		before_validation :generate_tokens, on: :create

		mattr_accessor :jt_rails_token_fields do
			[]
		end
	end

	module ClassMethods

		def tokenize(field)
			jt_rails_token_fields << field

			validates field, presence: true, uniqueness: true
		end

	end

	def generate_tokens
		for field in jt_rails_token_fields
			generate_new_token(field)
		end
	end

	def generate_new_token(field)
		self[field] = loop do
			random_token = SecureRandom.hex(128)
			break random_token unless self.class.exists?(field => random_token)
		end
	end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jt-rails-tokenizable-1.0.1 lib/jt-rails-tokenizable.rb
jt-rails-tokenizable-1.0.0 lib/jt-rails-tokenizable.rb