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



73
74
75
# File 'app/models/proposal/token.rb', line 73

def expects
  @expects || proposable.proposal_options[:expects]
end

Class Method Details

.find_or_new(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'app/models/proposal/token.rb', line 94

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.



174
175
176
177
178
179
180
181
# File 'app/models/proposal/token.rb', line 174

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.



186
187
188
189
190
191
# File 'app/models/proposal/token.rb', line 186

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)


148
149
150
151
# File 'app/models/proposal/token.rb', line 148

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

#accepted?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/proposal/token.rb', line 129

def accepted?
  !accepted_at.nil?
end

#actionObject



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

def action
  acceptable? ? acceptable_action : nil
end

#expired?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/proposal/token.rb', line 133

def expired?
  Time.now >= self.expires_at
end

#expires=(expires_proc) ⇒ Object

Calls proc to set the expires_at attribute.



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

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)


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

def invite?
  action == :invite
end

#invite_remind?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/models/proposal/token.rb', line 125

def invite_remind?
  action == :invite_remind
end

#notify?Boolean

Returns:

  • (Boolean)


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

def notify?
  action == :notify
end

#notify_remind?Boolean

Returns:

  • (Boolean)


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

def notify_remind?
  action == :notify_remind
end

#proposableObject



77
78
79
# File 'app/models/proposal/token.rb', line 77

def proposable
  @proposable ||= self.proposable_type.constantize
end

#proposable=(type) ⇒ Object



81
82
83
# File 'app/models/proposal/token.rb', line 81

def proposable= type
  self.proposable_type = type.to_s
end

#recipientObject



90
91
92
# File 'app/models/proposal/token.rb', line 90

def recipient
  @recipient ||= self.proposable.where(email: self.email).first
end

#recipient!Object



85
86
87
88
# File 'app/models/proposal/token.rb', line 85

def recipient!
  raise Proposal::RecordNotFound if recipient.nil?
  recipient
end

#remind?Boolean

Returns:

  • (Boolean)


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

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.



160
161
162
163
# File 'app/models/proposal/token.rb', line 160

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.



167
168
169
170
# File 'app/models/proposal/token.rb', line 167

def reminded!
  raise Proposal::RemindError, 'proposal action is not remind' unless remind?
  reminded
end

#reminded?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/proposal/token.rb', line 153

def reminded?
  !reminded_at.nil?
end

#to_sObject



193
194
195
# File 'app/models/proposal/token.rb', line 193

def to_s
  token
end

#validate_acceptedObject



59
60
61
# File 'app/models/proposal/token.rb', line 59

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

#validate_expiryObject



55
56
57
# File 'app/models/proposal/token.rb', line 55

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

#validate_uniquenessObject



44
45
46
47
48
49
50
51
52
53
# File 'app/models/proposal/token.rb', line 44

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