# File lib/archivist/base.rb, line 69 def acts_as_archive(options={}) has_archive(options) end
# File lib/archivist/base.rb, line 77 def enable_archive_mass_assignment!(klass) archive_class = archive_for(klass) attrs = archive_class.attribute_names archive_class.send(:attr_accessible,*attrs.map(&:to_sym)) end
# File lib/archivist/base.rb, line 17 def has_archive(options={}) options = ARCHIVIST_DEFAULTS.merge(options) options[:allow_multiple_archives] = true if options[:associate_with_original] class_eval(%Q{ alias_method :delete!, :delete class << self alias_method :delete_all!, :delete_all def archive_indexes #{Array(options[:indexes]).collect{|i| i.to_s}.inspect} end def archive_options #{options.inspect} end def has_archive? true end def acts_as_archive? warn("DEPRECATION WARNING: #acts_as_archive is provided for compatibility with AAA and will be removed soon, please use has_archive?",caller ) has_archive? end end class Archive < ActiveRecord::Base self.record_timestamps = false self.table_name = "archived_#{self.table_name}" self.primary_key = "#{self.primary_key}" include Archivist::ArchiveMethods end #{build_copy_self_to_archive(options[:allow_multiple_archives])} },File.expand_path(__FILE__),21) if ActiveRecord::VERSION::STRING >= "3.1.0" enable_archive_mass_assignment!(self) end attach_serializations!(self) include_modules!(self,options[:included_modules]) if options[:included_modules] build_associations!(self) if options[:associate_with_original] include InstanceMethods extend ClassExtensions include DB end
# File lib/archivist/base.rb, line 73 def archive_for(klass) "#{klass.to_s}::Archive".constantize end
# File lib/archivist/base.rb, line 83 def attach_serializations!(klass) archive_class = archive_for(klass) klass.serialized_attributes.each do |column,type| archive_class.send(:serialize,column,type) end end
# File lib/archivist/base.rb, line 97 def build_associations!(klass) archive_class = archive_for(klass) klass.send(:has_many,"archived_#{klass.table_name}".to_sym,:class_name => archive_class.to_s) archive_class.send(:belongs_to, klass.table_name.to_sym, :class_name => klass.to_s) end
# File lib/archivist/base.rb, line 103 def build_copy_self_to_archive(allow_multiple=false) if allow_multiple #we put the original pk in the fk instead "def copy_self_to_archive self.class.transaction do attrs = self.attributes.merge(:deleted_at=>DateTime.now) archived = #{self.to_s}::Archive.new(attrs.reject{|k,v| k=='id'}) archived.#{self.to_s.underscore}_id = attrs['id'] #{yield_and_save} end end" else "def copy_self_to_archive self.class.transaction do #it would be really shitty for us to loose data in the middle of this attrs = self.attributes.merge(:deleted_at=>DateTime.now) archived = #{self.to_s}::Archive.new if archived.class.where(:id=>self.id).empty? #create a new one if necessary, else update archived.id = attrs[\"id\"] archived.attributes = attrs.reject{|k,v| k=='id'} else archived = archived.class.where(:id=>attrs[\"id\"]).first archived.update_attributes(attrs) end #{yield_and_save} end end" end end
# File lib/archivist/base.rb, line 90 def include_modules!(klass,modules) archive_class = archive_for(klass) [*modules].each do |mod| archive_class.send(:include,mod) end end
# File lib/archivist/base.rb, line 131 def yield_and_save "yield(archived) if block_given? archived.save" end