Class: Purgatory

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/purgatory/purgatory.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.approvedObject



20
21
22
# File 'lib/purgatory/purgatory.rb', line 20

def self.approved
  where ["approved_at IS NOT NULL"]
end

.pendingObject



16
17
18
# File 'lib/purgatory/purgatory.rb', line 16

def self.pending
  where(approved_at: nil)
end

.pending_with_matching_soul(soul) ⇒ Object



69
70
71
# File 'lib/purgatory/purgatory.rb', line 69

def self.pending_with_matching_soul(soul)
  pending.where("soul_id IS NOT NULL AND soul_id = ? AND soul_type = ?", soul.id, soul.class.base_class.name)
end

Instance Method Details

#approve!(approver = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/purgatory/purgatory.rb', line 46

def approve!(approver = nil)
  return false if approved?

  success = nil
  self.with_lock do
    unless approved?
      success = soul_with_changes.save
      if performable_method.present? && success
        success = soul.send(performable_method[:method], *performable_method[:args])
      end

      if success
        self.approver = approver
        self.approved_at = Time.now
        self.soul_id = soul.id
        save
      end
    end
  end

  success
end

#approved?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/purgatory/purgatory.rb', line 24

def approved?
  approved_at.present?
end

#pending?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/purgatory/purgatory.rb', line 28

def pending?
  approved_at.nil?
end

#soulObject



32
33
34
# File 'lib/purgatory/purgatory.rb', line 32

def soul
  @soul ||= (super || (sti_class || soul_type).constantize.new)
end

#soul_with_changesObject



40
41
42
43
44
# File 'lib/purgatory/purgatory.rb', line 40

def soul_with_changes
  requested_changes.each{|k,v| soul.send(:write_attribute, k, v[1])} if requested_changes
  attr_accessor_fields.each{|k,v| soul.instance_variable_set(k, v)} if attr_accessor_fields
  soul
end

#sti_classObject



36
37
38
# File 'lib/purgatory/purgatory.rb', line 36

def sti_class
  requested_changes['type'].try(:last)
end