Sha256: f1da29ba952eea24450c36f75bc850b53801ae4b4595dce110de3ab75cf7179a

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'purgatory/purgatory_module'

class Purgatory < ActiveRecord::Base
  belongs_to :soul, polymorphic: true, autosave: false
  belongs_to :requester, class_name: PurgatoryModule.configuration.user_class_name
  belongs_to :approver, class_name: PurgatoryModule.configuration.user_class_name
  before_create :store_changes
  before_create :destroy_pending_with_same_soul

  validates :soul_type, presence: true

  serialize :requested_changes
  serialize :attr_accessor_fields

  def self.pending
    where(approved_at: nil)
  end
  
  def self.approved
    where ["approved_at IS NOT NULL"]
  end

  def approved?
    approved_at.present?
  end

  def pending?
    approved_at.nil?
  end

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

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

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

  def approve!(approver = nil)
    return false if approved? 
    if soul_with_changes.save
      self.approver = approver
      self.approved_at = Time.now
      self.soul_id = soul.id
      save
      return true
    end
    false
  end

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

  private

  def store_changes
    self.requested_changes = soul.changes
  end

  def destroy_pending_with_same_soul
    Purgatory.pending_with_matching_soul(soul).destroy_all
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
purgatory-2.12.0 lib/purgatory/purgatory.rb