lib/purgatory/purgatory.rb in purgatory-2.0.0 vs lib/purgatory/purgatory.rb in purgatory-2.1.0
- old
+ new
@@ -1,17 +1,20 @@
class Purgatory < ActiveRecord::Base
- attr_accessible :requester, :soul
-
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_type, presence: true
- scope :pending, conditions: { approved_at: nil }
- scope :approved, conditions: ["approved_at IS NOT NULL"]
+ def self.pending
+ where(approved_at: nil)
+ end
+
+ def self.approved
+ where ["approved_at IS NOT NULL"]
+ end
def approved?
approved_at.present?
end
@@ -22,17 +25,16 @@
def changes_hash
ActiveSupport::JSON.decode(changes_json)
end
def soul
- super || soul_type.constantize.new
+ @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)
+ changes_hash.each{|k,v| soul.send "#{k}=", v[1]}
+ if soul.save
self.approver = approver
self.approved_at = Time.now
save
return true
end