Class: Proposal::Token
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Proposal::Token
- Defined in:
- app/models/proposal/token.rb
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#accept ⇒ Object
Sets
Time.now
for theaccepted_at
field in the database if the proposal is acceptable. -
#accept! ⇒ Object
Equivalent
accept
except it will raise aProposal::ExpiredError
if the proposal has expired or aProposal::AcceptedError
if the proposal has already been accepted. -
#acceptable? ⇒ Boolean
Returns a
true
if the proposal has not expired and the proposal has not already been accepted. - #accepted? ⇒ Boolean
- #action ⇒ Object
- #expired? ⇒ Boolean
-
#expires=(expires_proc) ⇒ Object
Calls proc to set the
expires_at
attribute. - #invite? ⇒ Boolean
- #invite_remind? ⇒ Boolean
- #notify? ⇒ Boolean
- #notify_remind? ⇒ Boolean
- #proposable ⇒ Object
- #proposable=(type) ⇒ Object
- #recipient ⇒ Object
- #recipient! ⇒ Object
- #remind? ⇒ Boolean
-
#reminded ⇒ Object
Sets
Time.now
for thereminded_at
field in the database if the proposal action is:notify_remind
or:invite_remind
. -
#reminded! ⇒ Object
Equivalent to
reminded
except it will raise aProposal::RemindError
if the proposal action is not:notify_remind
or:invite_remind
. - #reminded? ⇒ Boolean
- #to_s ⇒ Object
- #validate_accepted ⇒ Object
- #validate_expiry ⇒ Object
- #validate_uniqueness ⇒ Object
Instance Attribute Details
#expects ⇒ Object
64 65 66 |
# File 'app/models/proposal/token.rb', line 64 def expects @expects ||= proposable.[:expects] end |
Class Method Details
.find_or_new(options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/proposal/token.rb', line 85 def self.find_or_new constraints = .slice :email, :proposable_type resource = [:resource] if !resource.nil? && resource.respond_to?(:id) constraints.merge! resource_type: resource.class.to_s, resource_id: resource.id end token = pending.not_expired.where(constraints).first token.nil? ? new() : token end |
Instance Method Details
#accept ⇒ Object
Sets Time.now
for the accepted_at
field in the database if the proposal is acceptable.
165 166 167 168 169 170 171 172 |
# File 'app/models/proposal/token.rb', line 165 def accept if acceptable? touch :accepted_at true else false end end |
#accept! ⇒ Object
Equivalent accept
except it will raise a Proposal::ExpiredError
if the proposal has expired or a Proposal::AcceptedError
if the proposal has already been accepted.
177 178 179 180 181 182 |
# File 'app/models/proposal/token.rb', line 177 def accept! raise Proposal::ExpiredError, 'token has expired' if expired? raise Proposal::AcceptedError, 'token has been used' if accepted? touch :accepted_at true end |
#acceptable? ⇒ Boolean
Returns a true
if the proposal has not expired and the proposal has not already been accepted. Also calls valid?
to set ActiveModel::Validator
validators for expires_at
and accepted_at
.
139 140 141 142 |
# File 'app/models/proposal/token.rb', line 139 def acceptable? valid? !expired? && !accepted? end |
#accepted? ⇒ Boolean
120 121 122 |
# File 'app/models/proposal/token.rb', line 120 def accepted? !accepted_at.nil? end |
#action ⇒ Object
96 97 98 |
# File 'app/models/proposal/token.rb', line 96 def action acceptable? ? acceptable_action : nil end |
#expired? ⇒ Boolean
124 125 126 |
# File 'app/models/proposal/token.rb', line 124 def expired? Time.now >= self.expires_at end |
#expires=(expires_proc) ⇒ Object
Calls proc to set the expires_at
attribute.
129 130 131 132 133 134 |
# File 'app/models/proposal/token.rb', line 129 def expires= expires_proc unless expires_proc.is_a? Proc raise ArgumentError, 'expires must be a proc' end self.expires_at = expires_proc.call end |
#invite? ⇒ Boolean
104 105 106 |
# File 'app/models/proposal/token.rb', line 104 def invite? action == :invite end |
#invite_remind? ⇒ Boolean
116 117 118 |
# File 'app/models/proposal/token.rb', line 116 def invite_remind? action == :invite_remind end |
#notify? ⇒ Boolean
100 101 102 |
# File 'app/models/proposal/token.rb', line 100 def notify? action == :notify end |
#notify_remind? ⇒ Boolean
112 113 114 |
# File 'app/models/proposal/token.rb', line 112 def notify_remind? action == :notify_remind end |
#proposable ⇒ Object
68 69 70 |
# File 'app/models/proposal/token.rb', line 68 def proposable @proposable ||= self.proposable_type.constantize end |
#proposable=(type) ⇒ Object
72 73 74 |
# File 'app/models/proposal/token.rb', line 72 def proposable= type self.proposable_type = type.to_s end |
#recipient ⇒ Object
81 82 83 |
# File 'app/models/proposal/token.rb', line 81 def recipient @recipient ||= self.proposable.where(email: self.email).first end |
#recipient! ⇒ Object
76 77 78 79 |
# File 'app/models/proposal/token.rb', line 76 def recipient! raise Proposal::RecordNotFound if recipient.nil? recipient end |
#remind? ⇒ Boolean
108 109 110 |
# File 'app/models/proposal/token.rb', line 108 def remind? [:notify_remind, :invite_remind].include? action end |
#reminded ⇒ Object
Sets Time.now
for the reminded_at
field in the database if the proposal action is :notify_remind
or :invite_remind
. This method can be called repeatedly.
151 152 153 154 |
# File 'app/models/proposal/token.rb', line 151 def reminded touch :reminded_at if remind? remind? end |
#reminded! ⇒ Object
Equivalent to reminded
except it will raise a Proposal::RemindError
if the proposal action is not :notify_remind
or :invite_remind
.
158 159 160 161 |
# File 'app/models/proposal/token.rb', line 158 def reminded! raise Proposal::RemindError, 'proposal action is not remind' unless remind? reminded end |
#reminded? ⇒ Boolean
144 145 146 |
# File 'app/models/proposal/token.rb', line 144 def reminded? !reminded_at.nil? end |
#to_s ⇒ Object
184 185 186 |
# File 'app/models/proposal/token.rb', line 184 def to_s token end |
#validate_accepted ⇒ Object
50 51 52 |
# File 'app/models/proposal/token.rb', line 50 def validate_accepted errors.add :token, "has been accepted" if accepted? end |
#validate_expiry ⇒ Object
46 47 48 |
# File 'app/models/proposal/token.rb', line 46 def validate_expiry errors.add :token, "has expired" if expired? end |
#validate_uniqueness ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/proposal/token.rb', line 35 def validate_uniqueness if self.class.pending.not_expired.where({ email: self.email, proposable_type: self.proposable_type, resource_type: self.resource_type, resource_id: self.resource_id }).exists? errors.add :email, "already has an outstanding proposal" end end |