Class: Approval

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

Constant Summary collapse

STATUSES =
%w(pending approved rejected)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_from_user_role(user_role) ⇒ Object


44
45
46
47
48
49
50
# File 'app/models/approval.rb', line 44

def self.new_from_user_role(user_role)
  self.new(
    position: user_role.position,
    role: user_role.role,
    user_id: user_role.user_id
  )
end

Instance Method Details

#api_tokenObject


37
38
39
40
41
42
# File 'app/models/approval.rb', line 37

def api_token
  ApiToken.fresh.where(
    cart_id: self.cart_id,
    user_id: self.user_id
  ).last
end

#approved?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'app/models/approval.rb', line 56

def approved?
  self.status == 'approved'
end

#approved_atObject

TODO we should probably store this value


61
62
63
64
65
66
67
# File 'app/models/approval.rb', line 61

def approved_at
  if self.approved?
    self.updated_at
  else
    nil
  end
end

#create_api_token!Object


29
30
31
32
33
34
35
# File 'app/models/approval.rb', line 29

def create_api_token!
  ApiToken.create!(
    cart_id: self.cart_id,
    expires_at: Time.now + 7.days,
    user_id: self.user_id
  )
end

#pending?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'app/models/approval.rb', line 52

def pending?
  self.status == 'pending'
end

#user_roleObject

TODO this should be a proper association


25
26
27
# File 'app/models/approval.rb', line 25

def user_role
  UserRole.find_by(approval_group_id: cart.approval_group.id, user_id: user_id)
end