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
55
56
57
58
59
60
# File 'lib/requires_approval.rb', line 18

def approve_attributes(*attributes)

  return true unless self.has_pending_changes?

  # 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 self.save
    # if we have approved all requested changes, make our latest
    # unapproved version approved -
    # this is ALWAYS true for a new record even though its pending_changes
    # hash is forced to have values
    if self.is_first_version? || self.no_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.update_attributes(:is_frozen => false)
    self.reload
    return true
  else
    return false
  end
end

#deny_attributes(*attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/requires_approval.rb', line 62

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 and
  # make sure it isn't frozen
  unless self.has_pending_changes?
    self.latest_unapproved_version.destroy
    self.update_attribute(:is_frozen, false)
  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)


96
97
98
99
100
101
102
# File 'lib/requires_approval.rb', line 96

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

  @has_approved_version
end

#has_pending_changes?Boolean

have we already approved all outstanding changes?

Returns:

  • (Boolean)


105
106
107
# File 'lib/requires_approval.rb', line 105

def has_pending_changes?
  self.pending_changes.present?
end

#is_first_version?Boolean

are we the first version?

Returns:

  • (Boolean)


110
111
112
# File 'lib/requires_approval.rb', line 110

def is_first_version?
  !self.has_approved_version?
end

#no_pending_changes?Boolean

returns true if there are no changes to approve

Returns:

  • (Boolean)


115
116
117
# File 'lib/requires_approval.rb', line 115

def no_pending_changes?
  !self.has_pending_changes?
end

#pending_changesObject

the changes users have requested since the last approval



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/requires_approval.rb', line 120

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
    if self.is_first_version? ||
      self.send(field) != self.latest_unapproved_version.send(field)

      # otherwise we get the change set
      ret[field] = {
        # our first version is always nil, regardless of the
        # defaults in that table
        "was" => self.is_first_version? ? nil : self.send(field),
        "became" => self.latest_unapproved_version.send(field)
      }
    end
  end
  ret
end

#reload(*args) ⇒ Object



88
89
90
91
92
93
# File 'lib/requires_approval.rb', line 88

def reload(*args)
  if instance_variable_defined?(:@has_approved_version)
    remove_instance_variable(:@has_approved_version)
  end
  super(*args)
end