sequel-audit_by_day 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,8 +32,8 @@ Given following models:
32
32
  # end
33
33
  #
34
34
  class User < Sequel::Model
35
- def admin_in_audit?
36
- admin
35
+ def audit_kind
36
+ admin ? "admin" : "user"
37
37
  end
38
38
  end
39
39
  #
@@ -65,8 +65,8 @@ end
65
65
  # Time :expired_at
66
66
  # Date :valid_from
67
67
  # Date :valid_to
68
- # foreign_key :title_updated_by_id, :users, on_delete: :set_null
69
- # foreign_key :admin_user_id, :users, on_delete: :set_null
68
+ # foreign_key :title_updated_by_user_id, :users, on_delete: :set_null
69
+ # foreign_key :title_updated_by_admin_id, :users, on_delete: :set_null
70
70
  # index [:master_id, :created_at, :valid_from, :valid_to]
71
71
  # end
72
72
  # end
@@ -6,44 +6,57 @@ module Sequel
6
6
  def self.configure(master, opts={})
7
7
  audit_foreign_key = opts[:foreign_key]
8
8
  default_valid_from = opts.fetch(:default_valid_from){ Time.utc(1000) }
9
+ updated_by_regexp = opts.fetch(:updated_by_regexp){ /(.+)_updated_by_(.+)_id/ }
9
10
  raise Error, ":foreign_key options is required for audit" unless audit_foreign_key
10
11
  raise Error, ":foreign_key column does not exists for audit" unless master.columns.include? audit_foreign_key
11
- updated_by_suffix = "_updated_by_id"
12
- version_columns = master.version_class.columns.collect do |column|
13
- column_str = column.to_s
14
- if column_str.end_with? updated_by_suffix
15
- column_str = column_str.gsub(updated_by_suffix, "").to_sym
16
- end
17
- end.compact
12
+ version_columns = {}
13
+ master.version_class.columns.each do |column|
14
+ next unless column.to_s =~ updated_by_regexp
15
+ column_name, column_kind = $1, $2
16
+ version_columns[column_name] ||= {}
17
+ version_columns[column_name][column_kind] = column
18
+ end
18
19
  master.instance_eval do
19
20
  @audit_foreign_key = audit_foreign_key
20
- @audit_checked_columns = Set.new version_columns
21
- @default_valid_from = default_valid_from
21
+ @audit_checked_columns = Set.new version_columns.keys
22
+ @audit_version_columns = version_columns
23
+ @audit_default_valid_from = default_valid_from
22
24
  end
23
25
  end
24
26
 
25
27
  module ClassMethods
26
- attr_reader :audit_foreign_key, :audit_checked_columns, :default_valid_from
28
+ attr_reader :audit_foreign_key, :audit_checked_columns,
29
+ :audit_version_columns, :audit_default_valid_from
27
30
 
28
31
  def find_for(audited_id, at)
29
32
  where(audit_foreign_key => audited_id, :for => at).with_current_version.first
30
33
  end
31
34
 
32
35
  def audit(master, previous_values, updated_values, update_time, updated_by)
33
- changed_values = updated_values.select do |column, updated_value|
34
- audit_checked_columns.include?(column) &&
35
- previous_values[column]!=updated_value
36
+ changed_values = updated_values.select do |column_name, updated_value|
37
+ audit_checked_columns.include?(column_name.to_s) &&
38
+ previous_values[column_name]!=updated_value
36
39
  end
40
+
37
41
  audit_for_day = find_for master.id, update_time
38
42
  audit_for_day ||= new({audit_foreign_key => master.id, :for => update_time})
39
- if updated_by.respond_to?(:admin_in_audit?) && updated_by.admin_in_audit?
40
- attrs = {admin_user_id: updated_by.id}
41
- else
42
- attrs = Hash[changed_values.collect{|column, _| ["#{column}_updated_by_id", updated_by]}]
43
- attrs[:admin_user_id] = nil
43
+
44
+ attrs = {}
45
+ updated_by_kind = updated_by.audit_kind.to_s
46
+ changed_values.each do |column_name, _|
47
+ unless audit_version_columns[column_name.to_s].has_key? updated_by_kind
48
+ raise AuditKindNotFound, "no audit column for column: #{column_name} and kind: #{updated_by_kind}"
49
+ end
50
+ audit_version_columns[column_name.to_s].each do |kind, column|
51
+ if kind==updated_by_kind
52
+ attrs[column] = updated_by.id
53
+ else
54
+ attrs[column] = nil
55
+ end
56
+ end
44
57
  end
45
58
  audit_for_day.update_attributes attrs.merge({
46
- partial_update: true, valid_from: default_valid_from
59
+ partial_update: true, valid_from: audit_default_valid_from
47
60
  })
48
61
  end
49
62
  end
@@ -1,5 +1,5 @@
1
1
  module Sequel
2
2
  module AuditByDay
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-audit_by_day
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  segments:
61
61
  - 0
62
- hash: -3607155131098875955
62
+ hash: -471555587537663744
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  segments:
70
70
  - 0
71
- hash: -3607155131098875955
71
+ hash: -471555587537663744
72
72
  requirements: []
73
73
  rubyforge_project:
74
74
  rubygems_version: 1.8.24