Sha256: 43f9e6516dbb4bce4092f9ecf9ab6f06d58636ceccb9b0b0a3c07ccca299a1eb

Contents?: true

Size: 800 Bytes

Versions: 1

Compression:

Stored size: 800 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, options = {})
			jt_rails_token_fields[field.to_sym] = options

			if options.fetch(:valiations, true)
				validates field, presence: true, uniqueness: true
			end
		end

	end

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

	def generate_new_token(field)
		size = jt_rails_token_fields[field.to_sym].fetch(:size, 128)

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jt-rails-tokenizable-1.0.2 lib/jt-rails-tokenizable.rb