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
86 87 88 89 90 91 92 93 94 95 |
# File 'app/models/proposal/token.rb', line 86 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.
168 169 170 171 172 173 174 175 |
# File 'app/models/proposal/token.rb', line 168 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.
180 181 182 183 184 185 186 |
# File 'app/models/proposal/token.rb', line 180 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
.
141 142 143 144 |
# File 'app/models/proposal/token.rb', line 141 def acceptable? valid? !expired? && !accepted? end |
#accepted? ⇒ Boolean
121 122 123 |
# File 'app/models/proposal/token.rb', line 121 def accepted? !accepted_at.nil? end |
#action ⇒ Object
97 98 99 |
# File 'app/models/proposal/token.rb', line 97 def action acceptable? ? acceptable_action : nil end |
#expired? ⇒ Boolean
125 126 127 |
# File 'app/models/proposal/token.rb', line 125 def expired? Time.now >= self.expires_at end |
#expires=(expires_proc) ⇒ Object
Calls proc to set the expires_at
attribute.
130 131 132 133 134 135 136 |
# File 'app/models/proposal/token.rb', line 130 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
105 106 107 |
# File 'app/models/proposal/token.rb', line 105 def invite? action == :invite end |
#invite_remind? ⇒ Boolean
117 118 119 |
# File 'app/models/proposal/token.rb', line 117 def invite_remind? action == :invite_remind end |
#notify? ⇒ Boolean
101 102 103 |
# File 'app/models/proposal/token.rb', line 101 def notify? action == :notify end |
#notify_remind? ⇒ Boolean
113 114 115 |
# File 'app/models/proposal/token.rb', line 113 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
82 83 84 |
# File 'app/models/proposal/token.rb', line 82 def recipient @recipient ||= self.proposable.where(email: self.email).first end |
#recipient! ⇒ Object
76 77 78 79 80 |
# File 'app/models/proposal/token.rb', line 76 def recipient! raise Proposal::RecordNotFound if recipient.nil? recipient end |
#remind? ⇒ Boolean
109 110 111 |
# File 'app/models/proposal/token.rb', line 109 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.
153 154 155 156 |
# File 'app/models/proposal/token.rb', line 153 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
.
160 161 162 163 164 |
# File 'app/models/proposal/token.rb', line 160 def reminded! raise Proposal::RemindError, 'proposal action is not remind' unless remind? reminded end |
#reminded? ⇒ Boolean
146 147 148 |
# File 'app/models/proposal/token.rb', line 146 def reminded? !reminded_at.nil? end |
#to_s ⇒ Object
188 189 190 |
# File 'app/models/proposal/token.rb', line 188 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 |