public
Created

Resourcing - Snapshotting

  • Download Gist
ActsAsSnapshot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
require_dependency 'acts_as_snapshot'
 
module ActsAsSnapshot
 
module ActiveRecord
 
 
def self.included(base)
base.extend(ActsAsSnapshot::ActiveRecord::ClassMethods)
end
 
module InstanceMethods
def assign_snapshot
self.snapshot = self.class.current_snapshot
end
 
def disable_save_for_old_snapshot
if !self.snapshot.blank?
self.errors.add(:base, "Can not update snapshot data")
return false
end
end
end
 
module ClassMethods
 
def acts_as_snapshot
 
include ActsAsSnapshot::ActiveRecord::InstanceMethods
 
before_create :assign_snapshot
 
if current_snapshot.blank?
default_scope :conditions => "#{self.table_name}.snapshot IS NULL"
else
default_scope :conditions => "#{self.table_name}.snapshot = #{self.current_snapshot}"
before_create :disable_save_for_old_snapshot
before_save :disable_save_for_old_snapshot
before_destroy :disable_save_for_old_snapshot
end
 
end
 
def current_snapshot
Thread.current[:snapshot_version]
end
 
 
def create_snapshot!(snapshot)
self.unscoped.where("snapshot IS NULL").all.each do |r|
new_r = r.clone
new_r.snapshot = snapshot
new_r.snapshot_parent_id = r.id
self.skip_callback(:create, :before, :assign_snapshot)
new_r.save!
self.set_callback(:create, :before, :assign_snapshot)
end
end
 
 
def rollback_snapshot!(snapshot)
self.unscoped.delete_all(["snapshot = ?", snapshot])
end
 
end
 
end
 
 
module SnapshotFilter
def self.included(base)
base.extend(ActsAsSnapshot::SnapshotFilter::ClassMethods)
end
 
 
module InstanceMethods
def current_snapshot
session[:version].blank? ? nil : session[:version].to_i
end
 
def assign_snapshot_version
old_version = session[:version]
if !params[:version].blank?
if params[:version].to_i == -1
session[:version] = nil
else
session[:version] = params[:version].to_i
end
end
new_version = session[:version]
Thread.current[:snapshot_version] = session[:version]
 
#force reloading the models to take set the default_scope according to the current thread
 
if new_version != old_version
[:bundle, :effort, :experience_level, :resource, :role, :vendor, :work_type, :app, :flight_risk, :vacation].each do |m|
obj_sym = m.to_s.classify.to_sym
if Object.const_defined?(obj_sym)
Object.send(:remove_const, obj_sym)
end
load "#{Rails.root}/app/models/#{m}.rb"
end
end
end
 
 
end
 
module ClassMethods
 
 
def set_snapshot
before_filter :assign_snapshot_version
include ActsAsSnapshot::SnapshotFilter::InstanceMethods
helper_method :current_snapshot
end
 
end
 
end
end
 
ActiveRecord::Base.send(:include, ActsAsSnapshot::ActiveRecord)
ActionController::Base.send(:include, ActsAsSnapshot::SnapshotFilter)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.