lib/purgatory/purgatory.rb in purgatory-1.1.0 vs lib/purgatory/purgatory.rb in purgatory-2.0.0
- old
+ new
@@ -1,14 +1,14 @@
class Purgatory < ActiveRecord::Base
attr_accessible :requester, :soul
- belongs_to :soul, polymorphic: true
+ belongs_to :soul, polymorphic: true, autosave: false
belongs_to :requester, class_name: 'User'
belongs_to :approver, class_name: 'User'
before_create :store_changes
- validates :soul, :requester, presence: true
+ validates :soul_type, presence: true
scope :pending, conditions: { approved_at: nil }
scope :approved, conditions: ["approved_at IS NOT NULL"]
def approved?
@@ -21,17 +21,24 @@
def changes_hash
ActiveSupport::JSON.decode(changes_json)
end
- def approve!(approver)
- return if approved_at.present?
+ def soul
+ super || soul_type.constantize.new
+ end
+
+ def approve!(approver = nil)
+ return false if approved?
changes = changes_hash
+
if soul.update_attributes(changes.update(changes){|k,v| v.last}, without_protection: true)
self.approver = approver
self.approved_at = Time.now
save
+ return true
end
+ false
end
private
def store_changes