Module: Sequel::Plugins::AuditByDay::ClassMethods

Defined in:
lib/sequel/plugins/audit_by_day.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#audit_checked_columnsObject (readonly)

Returns the value of attribute audit_checked_columns.



30
31
32
# File 'lib/sequel/plugins/audit_by_day.rb', line 30

def audit_checked_columns
  @audit_checked_columns
end

#audit_default_valid_fromObject (readonly)

Returns the value of attribute audit_default_valid_from.



30
31
32
# File 'lib/sequel/plugins/audit_by_day.rb', line 30

def audit_default_valid_from
  @audit_default_valid_from
end

#audit_foreign_keyObject (readonly)

Returns the value of attribute audit_foreign_key.



30
31
32
# File 'lib/sequel/plugins/audit_by_day.rb', line 30

def audit_foreign_key
  @audit_foreign_key
end

#audit_version_columnsObject (readonly)

Returns the value of attribute audit_version_columns.



30
31
32
# File 'lib/sequel/plugins/audit_by_day.rb', line 30

def audit_version_columns
  @audit_version_columns
end

Instance Method Details

#audit(master, previous_values, updated_values, update_time, updated_by) ⇒ Object



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
# File 'lib/sequel/plugins/audit_by_day.rb', line 38

def audit(master, previous_values, updated_values, update_time, updated_by)
  changed_values = updated_values.select do |column_name, updated_value|
    audit_checked_columns.include?(column_name.to_s) &&
    previous_values[column_name]!=updated_value
  end

  audit_for_day = find_for master.id, update_time
  audit_for_day ||= new({audit_foreign_key => master.id, :for => update_time})

  attrs = {}
  updated_by_kind = updated_by.audit_kind.to_s
  changed_values.each do |column_name, _|
    unless audit_version_columns[column_name.to_s].has_key? updated_by_kind
      raise AuditKindNotFound, "no audit column for column: #{column_name} and kind: #{updated_by_kind}"
    end
    audit_version_columns[column_name.to_s].each do |kind, column|
      if kind==updated_by_kind
        attrs[column] = updated_by.id
      else
        attrs[column] = nil
      end
    end
    attrs["#{column_name}_at"] = ::Sequel::Plugins::Bitemporal.point_in_time
  end
  audit_for_day.update_attributes attrs.merge(valid_from: audit_default_valid_from)
end

#find_for(audited_id, at) ⇒ Object



33
34
35
36
# File 'lib/sequel/plugins/audit_by_day.rb', line 33

def find_for(audited_id, at)
  where(audit_foreign_key => audited_id, :for => at).
    with_current_version.limit(1).all.first
end