Module: RailsJwtAuth::Invitable
- Defined in:
- app/models/concerns/rails_jwt_auth/invitable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#accept_invitation ⇒ Object
Accept an invitation by clearing token and setting invitation_accepted_at.
- #accept_invitation! ⇒ Object
- #accepted_invitation? ⇒ Boolean
- #generate_invitation_token! ⇒ Object
- #invite! ⇒ Object
- #invited? ⇒ Boolean
- #valid_invitation? ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 3 def self.included(base) base.extend ClassMethods base.class_eval do if defined?(Mongoid) && ancestors.include?(Mongoid::Document) # include GlobalID::Identification to use deliver_later method # http://edgeguides.rubyonrails.org/active_job_basics.html#globalid include GlobalID::Identification if RailsJwtAuth.deliver_later field :invitation_token, type: String field :invitation_sent_at, type: Time field :invitation_accepted_at, type: Time field :invitation_created_at, type: Time end end end |
Instance Method Details
#accept_invitation ⇒ Object
Accept an invitation by clearing token and setting invitation_accepted_at
46 47 48 49 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 46 def accept_invitation self.invitation_accepted_at = Time.current self.invitation_token = nil end |
#accept_invitation! ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 51 def accept_invitation! return unless invited? if valid_invitation? accept_invitation self.confirmed_at = Time.current if respond_to?(:confirmed_at) && confirmed_at.nil? else errors.add(:invitation_token, :invalid) end end |
#accepted_invitation? ⇒ Boolean
104 105 106 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 104 def accepted_invitation? invitation_token.nil? && invitation_accepted_at.present? end |
#generate_invitation_token! ⇒ Object
96 97 98 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 96 def generate_invitation_token! generate_invitation_token && save(validate: false) end |
#invite! ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 62 def invite! self.invitation_created_at = Time.current if new_record? unless password || password_digest passw = SecureRandom.base58(16) self.password = passw self.password_confirmation = passw end valid? # users that are registered and were not invited are not reinvitable if !new_record? && !invited? errors.add(RailsJwtAuth.auth_field_name!, :taken) end # users that have already accepted an invitation are not reinvitable if !new_record? && invited? && invitation_accepted_at.present? errors.add(RailsJwtAuth.auth_field_name!, :taken) end return self unless errors.empty? generate_invitation_token if invitation_token.nil? self.invitation_sent_at = Time.current send_invitation_mail if save(validate: false) self end |
#invited? ⇒ Boolean
92 93 94 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 92 def invited? (persisted? && invitation_token.present?) end |
#valid_invitation? ⇒ Boolean
100 101 102 |
# File 'app/models/concerns/rails_jwt_auth/invitable.rb', line 100 def valid_invitation? invited? && invitation_period_valid? end |