module Historiographer::Relation

Public Instance Methods

bulk_record_history(records, updates = {}) click to toggle source
# File lib/historiographer/relation.rb, line 31
def bulk_record_history(records, updates = {})
  now = UTC.now
  history_class = self.klass.history_class

  records.new.send(:history_user_absent_action) if updates[:history_user_id].nil?
  history_user_id = updates[:history_user_id]

  new_histories = records.map do |record|
    attrs         = record.attributes.clone
    foreign_key   = history_class.history_foreign_key
  
    attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id)
  
    attrs = attrs.except("id")

    record.histories.build(attrs)
  end

  current_histories = history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id))

  current_histories.update_all(history_ended_at: now)

  history_class.import new_histories
end
delete_all(options={}, histories=true) click to toggle source
Calls superclass method
# File lib/historiographer/relation.rb, line 60
def delete_all(options={}, histories=true)
  unless histories
    super()
  else
    ActiveRecord::Base.transaction do
      records = self
      history_class = records.first.class.history_class
      history_user_id = options[:history_user_id]
      records.first.send(:history_user_absent_action) if history_user_id.nil?
      now = UTC.now

      history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id)).update_all(history_ended_at: now)

      if records.first.respond_to?(:paranoia_destroy)
        new_histories = records.map do |record|
          attrs         = record.attributes.clone
          foreign_key   = history_class.history_foreign_key
    
          now = UTC.now
          attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id, deleted_at: now)
    
          attrs = attrs.except("id")

          record.histories.build(attrs)
        end
        history_class.import new_histories
      end

      super()
    end
  end
end
delete_all_without_history() click to toggle source
# File lib/historiographer/relation.rb, line 56
def delete_all_without_history
  delete_all(nil, false)
end
destroy_all(history_user_id: nil) click to toggle source
# File lib/historiographer/relation.rb, line 97
def destroy_all(history_user_id: nil)
  records.each { |r| r.destroy(history_user_id: history_user_id) }.tap { reset }
end
destroy_all_without_history() click to toggle source
# File lib/historiographer/relation.rb, line 93
def destroy_all_without_history
  records.each(&:destroy_without_history).tap { reset }
end
has_histories?() click to toggle source
# File lib/historiographer/relation.rb, line 5
def has_histories?
  self.klass.respond_to?(:history_class)
end
update_all(updates, histories=true) click to toggle source
Calls superclass method
# File lib/historiographer/relation.rb, line 13
def update_all(updates, histories=true)
  unless histories
    super(updates)
  else
    updates.symbolize_keys!
    model_changes = updates.except(:history_user_id)

    ActiveRecord::Base.transaction do
      changed_records = select do |record|
        !(record.attributes.symbolize_keys >= model_changes)
      end

      super(model_changes)
      bulk_record_history(self.reload.where(id: changed_records.pluck(:id)), updates)
    end
  end
end
update_all_without_history(updates) click to toggle source
# File lib/historiographer/relation.rb, line 9
def update_all_without_history(updates)
  update_all(updates, false)
end