acts_as_approvable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 :create_pending_approval
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
- !!approval.try(:approved?)
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
- create_pending_approval if approval.nil?
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
- create_pending_approval if approval.nil?
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 create_pending_approval
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
@@ -2,7 +2,7 @@ module ActsAsApprovable
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_approvable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jay Hayes