Sha256: 60702656cc5968e83f2c90f3fcc71e61275a1d487d64647a6dab28b4e119da63

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module ArchivalRecordCore
  module ArchivalRecordActiveRecordMethods

    def self.included(base)
      base.extend ARClassMethods
      base.send :include, ARInstanceMethods
    end

    module ARClassMethods

      def archival?
        included_modules.include?(ArchivalRecordCore::ArchivalRecord::InstanceMethods)
      end

    end

    module ARInstanceMethods

      def archival?
        self.class.archival?
      end

    end

    module ARRelationMethods

      def archive_all!
        error_message = "The #{klass} must implement 'act_on_archivals' in order to call `archive_all!`"
        raise NotImplementedError.new(error_message) unless archival?

        head_archive_number = Digest::MD5.hexdigest("#{klass}#{Time.now.utc.to_i}")
        each { |record| record.archive!(head_archive_number) }.tap { reset }
      end

      def unarchive_all!
        error_message = "The #{klass} must implement 'act_on_archivals' in order to call `unarchive_all!`"
        raise NotImplementedError.new(error_message) unless archival?

        each(&:unarchive!).tap { reset }
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
archival_record-2.0.2 lib/archival_record_core/archival_record_active_record_methods.rb
archival_record-2.0.1 lib/archival_record_core/archival_record_active_record_methods.rb
archival_record-2.0.0 lib/archival_record_core/archival_record_active_record_methods.rb