Module: RequiresApproval
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/errors.rb,
lib/requires_approval.rb,
lib/requires_approval/version.rb
Defined Under Namespace
Modules: ClassMethods Classes: CustomError, DenyingNeverApprovedError, InvalidFieldsError, PartialApprovalForNewObject
Constant Summary collapse
- VERSION =
"1.0.16"
Instance Method Summary collapse
- #approve_all_attributes ⇒ Object
-
#approve_attributes(*attributes) ⇒ Object
# approve a list of attributes.
- #deny_attributes(*attributes) ⇒ Object
-
#has_approved_version? ⇒ Boolean
have any of our versions ever been approved?.
-
#has_pending_changes? ⇒ Boolean
have we already approved all outstanding changes?.
-
#is_first_version? ⇒ Boolean
are we the first version?.
-
#no_pending_changes? ⇒ Boolean
returns true if there are no changes to approve.
-
#pending_changes ⇒ Object
the changes users have requested since the last approval.
- #reload(*args) ⇒ Object
Instance Method Details
#approve_all_attributes ⇒ Object
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 61 62 |
# 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.is_frozen = false self.save self.reload return true else return false end end |
#deny_attributes(*attributes) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/requires_approval.rb', line 64 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?
98 99 100 101 102 103 104 |
# File 'lib/requires_approval.rb', line 98 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?
107 108 109 |
# File 'lib/requires_approval.rb', line 107 def has_pending_changes? self.pending_changes.present? end |
#is_first_version? ⇒ Boolean
are we the first version?
112 113 114 |
# File 'lib/requires_approval.rb', line 112 def is_first_version? !self.has_approved_version? end |
#no_pending_changes? ⇒ Boolean
returns true if there are no changes to approve
117 118 119 |
# File 'lib/requires_approval.rb', line 117 def no_pending_changes? !self.has_pending_changes? end |
#pending_changes ⇒ Object
the changes users have requested since the last approval
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/requires_approval.rb', line 122 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
90 91 92 93 94 95 |
# File 'lib/requires_approval.rb', line 90 def reload(*args) if instance_variable_defined?(:@has_approved_version) remove_instance_variable(:@has_approved_version) end super(*args) end |