Module: Approval2::ModelAdditions

Extended by:
ActiveSupport::Concern
Defined in:
lib/approval2/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/approval2/model_additions.rb', line 33

def approve
   return "The record version is different from that of the approved version" if !self.approved_record.nil? and self.approved_version != self.approved_record.lock_version    

  #    make the U the A record, also assign the id of the A record, this looses history
  #    self.approval_status = 'A'
  #    self.approved_record.delete unless self.approved_record.nil?
  #    self.update_column(:id, self.approved_id) unless self.approved_id.nil?   
  #    self.approved_id = nil


  if self.approved_record.nil?
    # create action, all we need to do is set the status to approved
    self.approval_status = 'A'
    self.save!
  else
    # copy all attributes of the U record to the A record, and delete the U record
    attributes = self.attributes.select do |attr, value|
      self.class.column_names.include?(attr.to_s) and 
      ['id', 'approved_id', 'approval_status', 'lock_version', 'approved_version', 'created_at', 'updated_at', 'updated_by', 'created_by'].exclude?(attr)
    end
  
    self.class.unscoped do
      approved_record = self.approved_record
      approved_record.assign_attributes(attributes)
      approved_record.last_action = 'U'
      approved_record.updated_by = self.created_by
      self.destroy
      # not enought time to test cases where the approval is being done after changes in validations of the model, such that the saving of the approved 
      # record fails, this can be fixed to return the errors so that they can be shown to the user
      approved_record.save!
    end
  end

  return ""
end

#enable_approve_button?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/approval2/model_additions.rb', line 69

def enable_approve_button?
  self.approval_status == 'U' ? true : false
end

#on_create_create_unapproved_record_entryObject



73
74
75
76
77
# File 'lib/approval2/model_additions.rb', line 73

def on_create_create_unapproved_record_entry
  if approval_status == 'U'
    UnapprovedRecord.create!(:approvable => self)
  end
end

#on_destory_remove_unapproved_record_entriesObject



79
80
81
82
83
# File 'lib/approval2/model_additions.rb', line 79

def on_destory_remove_unapproved_record_entries
  if approval_status == 'U'
    unapproved_record_entry.delete
  end
end

#on_update_remove_unapproved_record_entriesObject



85
86
87
88
89
# File 'lib/approval2/model_additions.rb', line 85

def on_update_remove_unapproved_record_entries
  if approval_status == 'A' and approval_status_was == 'U'
    unapproved_record_entry.delete
  end
end

#validate_unapproved_recordObject



29
30
31
# File 'lib/approval2/model_additions.rb', line 29

def validate_unapproved_record
  errors.add(:base,"Unapproved Record Already Exists for this record") if !unapproved_record.nil? and (approval_status == 'A' and approval_status_was == 'A')
end