Module: RequiresApproval

Extended by:
ActiveSupport::Concern
Defined in:
lib/errors.rb,
lib/requires_approval.rb

Defined Under Namespace

Modules: ClassMethods Classes: CustomError, DenyingNeverApprovedError, InvalidFieldsError, PartialApprovalForNewObject

Instance Method Summary collapse

Instance Method Details

#approve_all_attributesObject



13
14
15
# File 'lib/requires_approval.rb', line 13

def approve_all_attributes
  self.approve_attributes(self.fields_requiring_approval)
end

#approve_attributes(*attributes) ⇒ Object

# approve a list of attributes



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/requires_approval.rb', line 18

def approve_attributes(*attributes)

  # validate an normalize our attributes    
  attributes = self.check_attributes_for_approval(attributes)

  # make sure that all attributes are provided if we have never
  # been approved
  fields_not_being_approved = (self.fields_requiring_approval - attributes)

  if fields_not_being_approved.present? && self.never_approved?
    raise PartialApprovalForNewObject.new(
      "You must approve #{self.fields_requiring_approval.join(", ")} " + 
      "for a new #{self.class.name}"
    )
  end

  attributes.flatten.each do |attr|
    write_attribute(attr, self.latest_unapproved_version.send(attr))
  end

  # if we have approved all requested changes, make our latest
  # unapproved version approved
  unless self.has_pending_changes?
    self.latest_unapproved_version.update_attribute(:is_approved, true)
  else
    # makes our latest_unapproved_version approved and 
    # creates another unapproved version with any remaining 
    # attributes
    self.create_approval_version_record
  end

  self.is_frozen = false

  self.save
  self.reload
  true
end

#deny_attributes(*attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/requires_approval.rb', line 56

def deny_attributes(*attributes)

  unless self.has_approved_version?
    raise DenyingNeverApprovedError.new
  end

  attributes = self.check_attributes_for_approval(attributes)

  attributes.flatten.each do |attr|
    self.latest_unapproved_version.send("#{attr}=", self.send(attr))
    true
  end

  # if we have denied all changes, remove the record
  unless self.has_pending_changes?
    self.latest_unapproved_version.destroy
  else
    self.latest_unapproved_version.save
  end
  
  self.reload
  true
end

#has_approved_version?Boolean

have any of our versions ever been approved?

Returns:

  • (Boolean)


81
82
83
# File 'lib/requires_approval.rb', line 81

def has_approved_version?
  self.versions.where(:is_approved => true).count > 0
end

#has_pending_changes?Boolean

have we already approved all outstanding changes?

Returns:

  • (Boolean)


86
87
88
# File 'lib/requires_approval.rb', line 86

def has_pending_changes?
  self.pending_changes.present?
end

#pending_changesObject

the changes users have requested since the last approval



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/requires_approval.rb', line 91

def pending_changes
  return {} if self.latest_unapproved_version.blank?
  
  ret = {}
  # check each field requiring approval
  self.fields_requiring_approval.each do |field|
    # if it is the same in the unapproved as in the parent table
    # we skip it
    unless self.send(field) == self.latest_unapproved_version.send(field)
      # otherwise we get the change set
      ret[field] = {
        "was" => self.send(field), 
        "became" => self.latest_unapproved_version.send(field)
      }
    end
  end
  ret
end