lib/tokenize.rb in jt-rails-tokenizable-1.1.2 vs lib/tokenize.rb in jt-rails-tokenizable-1.2.0
- old
+ new
@@ -1,10 +1,10 @@
module JT::Rails::Tokenizable::Tokenize
extend ActiveSupport::Concern
included do |base|
- before_validation :jt_rails_generate_tokens, on: :create
+ before_validation :jt_rails_generate_tokens_if_missing, on: :create
base.class_eval do
# jt_rails_token_fields is shared only by a class and its subclass
def self.jt_rails_token_fields
@@ -23,9 +23,15 @@
if options.fetch(:valiations, true)
validates field, presence: true, uniqueness: true
end
end
+ end
+
+ def jt_rails_generate_tokens_if_missing
+ for field in self.class.jt_rails_token_fields.keys
+ generate_new_token(field) if self[field.to_sym].blank?
+ end
end
def jt_rails_generate_tokens
for field in self.class.jt_rails_token_fields.keys
generate_new_token(field)