Class: Proposal::Token

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/proposal/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expectsObject



64
65
66
# File 'app/models/proposal/token.rb', line 64

def expects
  @expects || proposable.proposal_options[: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 options
  constraints = options.slice :email, :proposable_type
  resource = options[: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(options) : token
end

Instance Method Details

#acceptObject

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::AccepetedError 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::AccepetedError, '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.

Returns:

  • (Boolean)


139
140
141
142
# File 'app/models/proposal/token.rb', line 139

def acceptable?
  valid?
  !expired? && !accepted?
end

#accepted?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/proposal/token.rb', line 120

def accepted?
  !accepted_at.nil?
end

#actionObject



96
97
98
# File 'app/models/proposal/token.rb', line 96

def action
  acceptable? ? acceptable_action : nil
end

#expired?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


104
105
106
# File 'app/models/proposal/token.rb', line 104

def invite?
  action == :invite
end

#invite_remind?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/proposal/token.rb', line 116

def invite_remind?
  action == :invite_remind
end

#notify?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/proposal/token.rb', line 100

def notify?
  action == :notify
end

#notify_remind?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/proposal/token.rb', line 112

def notify_remind?
  action == :notify_remind
end

#proposableObject



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

#recipientObject



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

Returns:

  • (Boolean)


108
109
110
# File 'app/models/proposal/token.rb', line 108

def remind?
  [:notify_remind, :invite_remind].include? action
end

#remindedObject

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

Returns:

  • (Boolean)


144
145
146
# File 'app/models/proposal/token.rb', line 144

def reminded?
  !reminded_at.nil?
end

#to_sObject



184
185
186
# File 'app/models/proposal/token.rb', line 184

def to_s
  token
end

#validate_acceptedObject



50
51
52
# File 'app/models/proposal/token.rb', line 50

def validate_accepted
  errors.add :token, "has been accepted" if accepted?
end

#validate_expiryObject



46
47
48
# File 'app/models/proposal/token.rb', line 46

def validate_expiry
  errors.add :token, "has expired" if expired?
end

#validate_uniquenessObject



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