Module: TokenMaster::Core
- Defined in:
- lib/token_master/core.rb
Overview
TokenMaster::Core
provides the core functionality of the TokenMaster gem. The Core
module performs all of the logic of completing tokenable actions, and provides descriptive messages of the status or abilities of calls made.
Class Method Summary collapse
-
.do_by_token!(klass, key, token, **params) ⇒ Object
Completes the tokenable action for a tokenable model instance using a token, setting
tokenable_completed_at
to the time at completion. -
.force_tokenable!(model, key, **params) ⇒ Object
Completes the token action for a tokenable instance without a token, setting the
tokenable_completed_at
to the time at completion.
Usually implemented when you want to complete multiple tokenable actions at once, e.g., a user completes the invite action by setting up passwords, by default also completes the confirm action. -
.resend_instructions!(model, key, token_length = nil) ⇒ Object
Calls set_token! and send_instructions! to generate a new token and send instructions again (accepts a block, such as a mailer method, for sending instructions).
Note, any previously generated token for the user will be invalid. -
.send_instructions!(model, key) ⇒ Object
Accepts a block to pass on a generated token through a block, such as a mailer method, and sets
tokenable_sent_at
to the time the method is called. -
.set_token!(model, key, token_length = nil) ⇒ String
Generates a tokenable action token, sets the token and the time of creation on the tokenable model instance.
-
.status(model, key) ⇒ String
Provides the status of the tokenable action, whether the action has been completed, the token has been sent, the token is expired, or the token has only been created.
Class Method Details
.do_by_token!(klass, key, token, **params) ⇒ Object
Completes the tokenable action for a tokenable model instance using a token, setting tokenable_completed_at
to the time at completion
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/token_master/core.rb', line 20 def do_by_token!(klass, key, token, **params) check_manageable! klass, key token_column = { token_col(key) => token } model = klass.find_by(token_column) check_token_active! model, key check_params! key, params model.update!( params.merge(completed_at_col(key) => Time.now) ) model end |
.force_tokenable!(model, key, **params) ⇒ Object
Completes the token action for a tokenable instance without a token, setting the tokenable_completed_at
to the time at completion.
Usually implemented when you want to complete multiple tokenable actions at once, e.g., a user completes the invite action by setting up passwords, by default also completes the confirm action
43 44 45 46 47 48 49 50 51 |
# File 'lib/token_master/core.rb', line 43 def force_tokenable!(model, key, **params) check_manageable! model.class, key check_params! key, params model.update!( params.merge(completed_at_col(key) => Time.now) ) model end |
.resend_instructions!(model, key, token_length = nil) ⇒ Object
Calls set_token! and send_instructions! to generate a new token and send instructions again (accepts a block, such as a mailer method, for sending instructions).
Note, any previously generated token for the user will be invalid.
104 105 106 107 |
# File 'lib/token_master/core.rb', line 104 def resend_instructions!(model, key, token_length = nil) set_token!(model, key, token_length) send_instructions!(model, key) end |
.send_instructions!(model, key) ⇒ Object
Accepts a block to pass on a generated token through a block, such as a mailer method, and sets tokenable_sent_at
to the time the method is called
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/token_master/core.rb', line 84 def send_instructions!(model, key) check_manageable! model.class, key check_token_set! model, key check_instructions_sent! model, key yield if block_given? model.update(sent_at_col(key) => Time.now) model.save(validate: false) end |
.set_token!(model, key, token_length = nil) ⇒ String
Generates a tokenable action token, sets the token and the time of creation on the tokenable model instance
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/token_master/core.rb', line 59 def set_token!(model, key, token_length = nil) check_manageable! model.class, key token_length ||= TokenMaster.config.get_token_length(key.to_sym) token = generate_token token_length model.update({ token_col(key) => token, created_at_col(key) => Time.now, sent_at_col(key) => nil, completed_at_col(key) => nil }) model.save(validate: false) token end |
.status(model, key) ⇒ String
Provides the status of the tokenable action, whether the action has been completed, the token has been sent, the token is expired, or the token has only been created
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/token_master/core.rb', line 119 def status(model, key) check_manageable! model.class, key return 'completed' if completed?(model, key) return 'sent' if instructions_sent?(model, key) if token_set?(model, key) return 'expired' unless token_active?(model, key) return 'created' end 'no token' end |