Module: RequiresApproval

Defined in:
lib/errors.rb,
lib/requires_approval.rb

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/requires_approval.rb', line 8

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#approve_all_attributesObject



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

def approve_all_attributes
  self.approve_attributes(self.fields_requiring_approval)
end

#approve_attributes(*attributes) ⇒ Object

# approve a list of attributes



17
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
# File 'lib/requires_approval.rb', line 17

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 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.is_frozen = false

  self.save
  self.reload
  true
end

#deny_attributes(*attributes) ⇒ Object



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

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)


84
85
86
# File 'lib/requires_approval.rb', line 84

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

#has_pending_changes?Boolean

have we already approved all outstanding changes?

Returns:

  • (Boolean)


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

def has_pending_changes?
  self.pending_changes.present?
end

#is_first_version?Boolean

are we the first version?

Returns:

  • (Boolean)


94
95
96
# File 'lib/requires_approval.rb', line 94

def is_first_version?
  !self.has_approved_version?
end

#no_pending_changes?Boolean

returns true if there are no changes to approve

Returns:

  • (Boolean)


99
100
101
# File 'lib/requires_approval.rb', line 99

def no_pending_changes?
  !self.has_pending_changes?
end

#pending_changesObject

the changes users have requested since the last approval



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/requires_approval.rb', line 104

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