Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PropMixin
Defined in:
app/models/cart.rb

Constant Summary collapse

ORIGINS =
%w(navigator ncr)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PropMixin

#deserialized_properties, #getProp, #setProp, #set_props

Class Method Details

.copy_existing_approvals_to(new_cart, cart_name) ⇒ Object


195
196
197
198
199
200
201
202
# File 'app/models/cart.rb', line 195

def self.copy_existing_approvals_to(new_cart, cart_name)
  previous_cart = Cart.where(name: cart_name).last
  if previous_cart && previous_cart.status == 'rejected'
    previous_cart.approvals.each do |approval|
      new_cart.copy_existing_approval(approval)
    end
  end
end

.existing_or_new_cart(params) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/cart.rb', line 105

def self.existing_or_new_cart(params)
  name = params['cartName'].presence || params['cartNumber'].to_s

  pending_cart = Cart.find_by(name: name, status: 'pending')
  if pending_cart
    cart = reset_existing_cart(pending_cart)
  else
    #There is no existing cart or the existing cart is already approved
    cart = Cart.new(name: name, status: 'pending', external_id: params['cartNumber'])
  end

  cart
end

.initialize_cart_with_items(params) ⇒ Object


96
97
98
99
100
101
102
103
# File 'app/models/cart.rb', line 96

def self.initialize_cart_with_items params
  cart = self.existing_or_new_cart params
  cart.initialize_approval_group params
  cart.initialize_flow(params)
  self.copy_existing_approvals_to(cart, name)

  cart
end

.reset_existing_cart(cart) ⇒ Object


187
188
189
190
191
192
193
# File 'app/models/cart.rb', line 187

def self.reset_existing_cart(cart)
  cart.approvals.map(&:destroy)
  cart.cart_items.destroy_all
  cart.approval_group = nil

  cart
end

Instance Method Details

#add_approver(email) ⇒ Object


136
137
138
139
# File 'app/models/cart.rb', line 136

def add_approver(email)
  user = User.find_or_create_by(email_address: email)
  self.approvals.create!(user_id: user.id, role: 'approver')
end

#all_approvals_received?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'app/models/cart.rb', line 83

def all_approvals_received?
  self.approver_approvals.where('status != ?', 'approved').empty?
end

#approved_approvalsObject


79
80
81
# File 'app/models/cart.rb', line 79

def approved_approvals
  self.approver_approvals.where(status: 'approved')
end

#approver_approvalsObject


43
44
45
# File 'app/models/cart.rb', line 43

def approver_approvals
  self.approvals.where(role: 'approver')
end

#approversObject


47
48
49
50
# File 'app/models/cart.rb', line 47

def approvers  # TODO do through SQL

  self.approver_approvals.map(&:user)
end

#awaiting_approvalsObject


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

def awaiting_approvals
  self.approver_approvals.pending
end

#awaiting_approversObject


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

def awaiting_approvers  # TODO do through SQL

  self.awaiting_approvals.map(&:user)
end

#copy_existing_approval(approval) ⇒ Object


182
183
184
185
# File 'app/models/cart.rb', line 182

def copy_existing_approval(approval)
  new_approval = self.approvals.create!(user_id: approval.user_id, role: approval.role)
  Dispatcher.new.email_approver(new_approval)
end

#create_approval_from_user_role(user_role) ⇒ Object


169
170
171
172
173
174
# File 'app/models/cart.rb', line 169

def create_approval_from_user_role(user_role)
  approval = Approval.new_from_user_role(user_role)
  approval.cart = self
  approval.save!
  approval
end

#create_approver_approvals(emails) ⇒ Object


141
142
143
144
145
# File 'app/models/cart.rb', line 141

def create_approver_approvals(emails)
  emails.each do |email|
    self.add_approver(email)
  end
end

#create_requester(email) ⇒ Object


151
152
153
154
# File 'app/models/cart.rb', line 151

def create_requester(email)
  user = User.find_or_create_by(email_address: email)
  self.set_requester(user)
end

#currently_awaiting_approversObject

users with outstanding cart_notification_emails


70
71
72
73
74
75
76
77
# File 'app/models/cart.rb', line 70

def currently_awaiting_approvers
  if self.parallel?
    self.awaiting_approvers
  else # linear
    approval = self.ordered_awaiting_approvals.first
    [approval.user]
  end
end

#gsa_advantage?Boolean

Returns:

  • (Boolean)

231
232
233
234
# File 'app/models/cart.rb', line 231

def gsa_advantage?  # TODO set the origin

  self.origin.blank?
end

#has_rejection?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'app/models/cart.rb', line 39

def has_rejection?
  self.rejections.any?
end

#import_cart_item(params) ⇒ Object


204
205
206
207
208
209
210
211
# File 'app/models/cart.rb', line 204

def import_cart_item(params)
  params = params.dup
  params.delete_if {|k,v| v.blank? }

  ci = CartItem.from_params(params)
  ci.cart = self
  ci.save!
end

#import_cart_items(cart_items_params) ⇒ Object


213
214
215
216
217
# File 'app/models/cart.rb', line 213

def import_cart_items(cart_items_params)
  cart_items_params.each do |params|
    self.import_cart_item(params)
  end
end

#import_initial_comments(comments) ⇒ Object


132
133
134
# File 'app/models/cart.rb', line 132

def import_initial_comments(comments)
  self.comments.create!(user_id: self.requester.id, comment_text: comments.strip)
end

#initialize_approval_group(params) ⇒ Object


119
120
121
122
123
124
125
126
# File 'app/models/cart.rb', line 119

def initialize_approval_group(params)
  if params['approvalGroup']
    self.approval_group = ApprovalGroup.find_by_name(params['approvalGroup'])
    if self.approval_group.nil?
      raise ApprovalGroupError.new('Approval Group Not Found')
    end
  end
end

#initialize_flow(params) ⇒ Object


128
129
130
# File 'app/models/cart.rb', line 128

def initialize_flow(params)
  self.flow = params['flow'].presence || self.flow || self.approval_group.try(:flow) || 'parallel'
end

#ncr?Boolean

Returns:

  • (Boolean)

227
228
229
# File 'app/models/cart.rb', line 227

def ncr?
  self.origin == 'ncr'
end

#observersObject


91
92
93
94
# File 'app/models/cart.rb', line 91

def observers  # TODO: Pull from approvals, not approval groups

  approval_group.user_roles.where(role: 'observer')
end

#ordered_approvalsObject


61
62
63
# File 'app/models/cart.rb', line 61

def ordered_approvals
  self.approver_approvals.order('position ASC')
end

#ordered_awaiting_approvalsObject


65
66
67
# File 'app/models/cart.rb', line 65

def ordered_awaiting_approvals
  self.ordered_approvals.pending
end

#originObject


223
224
225
# File 'app/models/cart.rb', line 223

def origin
  self.getProp('origin')
end

#parallel?Boolean

Returns:

  • (Boolean)

249
250
251
# File 'app/models/cart.rb', line 249

def parallel?
  self.flow == 'parallel'
end

#pending?Boolean

Returns:

  • (Boolean)

253
254
255
256
# File 'app/models/cart.rb', line 253

def pending?  # TODO validates :status, inclusion: {in: Approval::STATUSES}

  self.status.blank? || self.status == 'pending'
end

#process_approvals_from_approval_groupObject


176
177
178
179
180
# File 'app/models/cart.rb', line 176

def process_approvals_from_approval_group
  approval_group.user_roles.each do |user_role|
    self.create_approval_from_user_role(user_role)
  end
end

#process_approvals_without_approval_group(params) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/cart.rb', line 156

def process_approvals_without_approval_group(params)
  if params['approvalGroup'].present?
    raise ApprovalGroupError.new('Approval Group already exists')
  end
  approver_emails = params['toAddress'].select(&:present?)
  self.create_approver_approvals(approver_emails)

  requester_email = params['fromAddress']
  if requester_email
    self.create_requester(requester_email)
  end
end

#public_identifierObject


245
246
247
# File 'app/models/cart.rb', line 245

def public_identifier
  self.send(self.public_identifier_method)
end

#public_identifier_methodObject

TODO use this when retrieving


237
238
239
240
241
242
243
# File 'app/models/cart.rb', line 237

def public_identifier_method
  if self.ncr?
    :id
  else
    :external_id
  end
end

#rejectionsObject


35
36
37
# File 'app/models/cart.rb', line 35

def rejections
  self.approvals.where(status: 'rejected')
end

#requesterObject


87
88
89
# File 'app/models/cart.rb', line 87

def requester
  approvals.where(role: 'requester').first.try(:user)
end

#set_defaultsObject


219
220
221
# File 'app/models/cart.rb', line 219

def set_defaults
  self.status ||= 'pending'
end

#set_requester(user) ⇒ Object


147
148
149
# File 'app/models/cart.rb', line 147

def set_requester(user)
  self.approvals.create!(user_id: user.id, role: 'requester')
end

#update_approval_statusObject


27
28
29
30
31
32
33
# File 'app/models/cart.rb', line 27

def update_approval_status
  if self.has_rejection?
    self.update_attributes(status: 'rejected')
  elsif self.all_approvals_received?
    self.update_attributes(status: 'approved')
  end
end