acts_as_approvable 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/acts_as_approvable/approver.rb +28 -10
- data/lib/acts_as_approvable/version.rb +1 -1
- metadata +1 -1
@@ -8,39 +8,52 @@ module ActsAsApprovable
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
# Creates associations with +Approvals+ and add instance methods for getting approval status
|
11
|
-
def acts_as_approvable
|
11
|
+
def acts_as_approvable(options = {})
|
12
12
|
# don't allow multiple calls
|
13
13
|
return if included_modules.include?(ActsAsApprovable::Approver::InstanceMethods)
|
14
14
|
|
15
|
+
# default approval states when there is no approval and after the record is updated
|
16
|
+
options[:when_missing] ||= false
|
17
|
+
options[:post_update] ||= false
|
18
|
+
|
19
|
+
class_attribute :when_missing
|
20
|
+
self.when_missing = options[:when_missing]
|
21
|
+
|
22
|
+
class_attribute :post_update
|
23
|
+
self.post_update = options[:post_update]
|
24
|
+
|
15
25
|
# association with approval
|
16
|
-
has_one :approval, :as => :approvable
|
26
|
+
has_one :approval, :as => :approvable, :dependent => :destroy
|
17
27
|
|
18
28
|
# access to all models that have been approved
|
19
|
-
scope :approved, joins(:approval).where(Approval.arel_table[:approved].eq(true))
|
29
|
+
scope :approved, lambda{ joins(:approval).where(Approval.arel_table[:approved].eq(true)) }
|
20
30
|
|
21
|
-
# make sure every approvable model has an associated approval
|
22
|
-
after_create :
|
31
|
+
# make sure every new approvable model has an associated approval
|
32
|
+
after_create :create_approval
|
33
|
+
after_update :update_approval
|
23
34
|
|
24
35
|
include ActsAsApprovable::Approver::InstanceMethods
|
25
36
|
end
|
26
37
|
end
|
27
38
|
|
28
39
|
module InstanceMethods
|
40
|
+
# Returns whether the approval is approved. Defaults to +default_approval+ if the approval is missing
|
29
41
|
def approved?
|
30
|
-
|
42
|
+
approval.nil? ? when_missing : approval.try(:approved?)
|
31
43
|
end
|
32
44
|
|
45
|
+
# Return whether the the record is not approved
|
33
46
|
def pending?
|
34
47
|
!approved?
|
35
48
|
end
|
36
49
|
|
37
50
|
def approve!(who = nil)
|
38
|
-
|
51
|
+
create_approval if approval.nil?
|
39
52
|
approval.update_attributes :approved => true, :approver => who || nil if pending?
|
40
53
|
end
|
41
54
|
|
42
55
|
def disapprove!(who = nil)
|
43
|
-
|
56
|
+
create_approval if approval.nil?
|
44
57
|
approval.update_attributes :approved => false, :approver => who || nil if approved?
|
45
58
|
end
|
46
59
|
|
@@ -50,8 +63,13 @@ module ActsAsApprovable
|
|
50
63
|
|
51
64
|
private
|
52
65
|
|
53
|
-
def
|
54
|
-
self.approval = Approval.create :approvable => self
|
66
|
+
def create_approval
|
67
|
+
self.approval = Approval.create :approvable => self, :approved => when_missing
|
68
|
+
end
|
69
|
+
|
70
|
+
def update_approval
|
71
|
+
create_approval if approval.nil?
|
72
|
+
self.approval.update_attributes :approved => post_update
|
55
73
|
end
|
56
74
|
end
|
57
75
|
end
|