change_log 1.0.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +2 -2
- data/Gemfile.lock +20 -1
- data/README.textile +4 -15
- data/lib/change_log/change_logs.rb +12 -9
- data/lib/change_log/has_change_log.rb +9 -41
- data/lib/change_log/version.rb +1 -1
- data/test/boot.rb +2 -2
- data/test/change_log_test.rb +2 -2
- metadata +2 -2
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -4,13 +4,32 @@ PATH
|
|
4
4
|
change_log (1.0.5)
|
5
5
|
|
6
6
|
GEM
|
7
|
-
remote:
|
7
|
+
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
activemodel (3.2.13)
|
10
|
+
activesupport (= 3.2.13)
|
11
|
+
builder (~> 3.0.0)
|
12
|
+
activerecord (3.2.13)
|
13
|
+
activemodel (= 3.2.13)
|
14
|
+
activesupport (= 3.2.13)
|
15
|
+
arel (~> 3.0.2)
|
16
|
+
tzinfo (~> 0.3.29)
|
17
|
+
activerecord-import (0.3.1)
|
18
|
+
activerecord (~> 3.0)
|
19
|
+
activesupport (3.2.13)
|
20
|
+
i18n (= 0.6.1)
|
21
|
+
multi_json (~> 1.0)
|
22
|
+
arel (3.0.2)
|
23
|
+
builder (3.0.4)
|
24
|
+
i18n (0.6.1)
|
25
|
+
multi_json (1.7.7)
|
9
26
|
rake (10.0.3)
|
27
|
+
tzinfo (0.3.37)
|
10
28
|
|
11
29
|
PLATFORMS
|
12
30
|
ruby
|
13
31
|
|
14
32
|
DEPENDENCIES
|
33
|
+
activerecord-import (~> 0.3.1)
|
15
34
|
change_log!
|
16
35
|
rake
|
data/README.textile
CHANGED
@@ -6,17 +6,12 @@ You can choose to skip the column which you do not want to keep change logs.
|
|
6
6
|
For example: 'updated_at', 'created_at' and 'password' etc.
|
7
7
|
|
8
8
|
* Note:
|
9
|
-
Change Log version 1.0.5
|
10
|
-
Another version with fully support to Rails 3.x and multiple databases
|
11
|
-
will be available soon in another branch on github.
|
12
|
-
|
9
|
+
Use Change Log version 1.0.5 if your application is using Rails 2.x.
|
13
10
|
|
14
11
|
h2. Install Change Log Gem
|
15
12
|
|
16
13
|
1. by command:
|
17
14
|
<pre><code># gem install change_log</code></pre>
|
18
|
-
Then in your environment.rb:
|
19
|
-
<pre><code>config.gem 'change_log'</code></pre>
|
20
15
|
|
21
16
|
|
22
17
|
2. or by bundler:
|
@@ -27,12 +22,6 @@ gem 'change_log'
|
|
27
22
|
Then:
|
28
23
|
<pre><code>bundle install</code></pre>
|
29
24
|
|
30
|
-
* Note:
|
31
|
-
If you use bundler with Rails 2.3.x, you may get "enable_change_log" method missing error.
|
32
|
-
This is because Rails 2.3.x comes with its own gem handling.
|
33
|
-
You need to override that and replace it with support for Bundler.
|
34
|
-
Visit "Bundler Website":http://gembundler.com/rails23.html to find out how.
|
35
|
-
|
36
25
|
3. Create a table to keep all changes
|
37
26
|
|
38
27
|
Generate a migration file similar to this:
|
@@ -152,7 +141,7 @@ Just remember in your environment.rb file, you need to tell change_log gem
|
|
152
141
|
what is your table name:
|
153
142
|
|
154
143
|
<pre><code># config/environment.rb
|
155
|
-
ChangeLogs.
|
144
|
+
ChangeLogs.table_name :hr_maintenances
|
156
145
|
</code></pre>
|
157
146
|
|
158
147
|
|
@@ -163,7 +152,7 @@ h2. Wish List
|
|
163
152
|
h3. Author
|
164
153
|
----
|
165
154
|
|
166
|
-
Peter Zhang at NCS New Zealand.
|
155
|
+
Peter Zhang at NCS Software Ltd New Zealand.
|
167
156
|
Email: peterz@ncs.co.nz
|
168
157
|
|
169
|
-
Copyright (c)
|
158
|
+
Copyright (c) 2013 Peter Zhang and NCS Software LTD, released under the MIT license
|
@@ -1,17 +1,20 @@
|
|
1
1
|
class ChangeLogs < ActiveRecord::Base
|
2
2
|
# Set table name to "change_logs"
|
3
|
-
|
4
|
-
:change_logs
|
5
|
-
end
|
3
|
+
self.table_name = :change_logs
|
6
4
|
|
7
5
|
private
|
8
6
|
|
9
|
-
# Save Change Log details
|
10
|
-
def self.update_change_log_record_with(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
# Save Change Log details with changes
|
8
|
+
def self.update_change_log_record_with(changes=[])
|
9
|
+
records = []
|
10
|
+
changes.each do |option|
|
11
|
+
record = ChangeLogs.new(option)
|
12
|
+
record.field_type = get_field_type(option[:table_name],option[:attribute_name]) unless option[:action].eql?('DELETE')
|
13
|
+
record.user = option[:user].id unless option[:user].is_a?(String)
|
14
|
+
record.created_at = Time.now
|
15
|
+
records << record
|
16
|
+
end
|
17
|
+
ChangeLogs.import records
|
15
18
|
end
|
16
19
|
|
17
20
|
# return the latest version number for this change
|
@@ -42,76 +42,44 @@ module ChangeLog
|
|
42
42
|
# ActiveRecord models that declare `enable_change_log`.
|
43
43
|
module InstanceMethods
|
44
44
|
|
45
|
-
# NOTE::This version's change_log is temporarily locked down to Rails 2.3.x
|
46
|
-
# and mysql database. Another version with fully support to Rails 3.x and multiple databases
|
47
|
-
# will be available soon in another branch on github.
|
48
45
|
def record_create
|
49
46
|
# do nothing if the change log is not turned on
|
50
47
|
return '' unless switched_on?
|
51
|
-
|
52
|
-
# generate the single sql insert statement
|
53
|
-
column_values = []
|
48
|
+
changes = []
|
54
49
|
# saving changes to change log
|
55
50
|
self.attributes.map do |key,value|
|
56
51
|
unless self.ignore.include?(key.to_sym)
|
57
|
-
|
58
|
-
value = value.gsub("'", %q(\\\')) unless value.blank? || !value.is_a?(String)
|
59
|
-
user = ChangeLog.whodidit
|
60
|
-
if user.blank?
|
61
|
-
user = 'Unkown'
|
62
|
-
else
|
63
|
-
user = ChangeLog.whodidit.is_a?(String) ? ChangeLog.whodidit : ChangeLog.whodidit.id
|
64
|
-
end
|
65
|
-
time = Time.now.strftime("%Y-%m-%d %T")
|
66
|
-
column_values << '(' + ["'INSERT'", self.id, "'#{self.class.table_name}'", "'#{user}'", "'#{field_type}'", "'#{key}'", "'#{value}'",1, "'#{time}'" ].join(',') + ')'
|
52
|
+
changes << {:action=>'INSERT', :record_id=>self.id,:table_name=>self.class.table_name, :user=>ChangeLog.whodidit,:attribute_name=>key,:new_value=>value,:version=>1}
|
67
53
|
end
|
68
54
|
end
|
69
|
-
|
70
|
-
insert_statement = "INSERT INTO `#{ChangeLogs.table_name}` (`#{column_names.join('`, `')}`) VALUES " + column_values.join( ',' ) + ";"
|
71
|
-
ChangeLogs.connection.execute( insert_statement )
|
55
|
+
ChangeLogs.update_change_log_record_with(changes)
|
72
56
|
end
|
73
57
|
|
74
|
-
# NOTE::This version's change_log is temporarily locked down to Rails 2.3.x
|
75
|
-
# and mysql database. Another version with fully support to Rails 3.x and multiple databases
|
76
|
-
# will be available soon in another branch on github.
|
77
58
|
def record_update
|
78
59
|
# do nothing if the change log is not turned on and no changes has been made
|
79
60
|
return '' unless switched_on? && self.valid? && self.changed?
|
80
|
-
|
81
|
-
# generate the single sql insert statement
|
82
|
-
column_values = []
|
61
|
+
changes = []
|
83
62
|
# saving changes to change log
|
84
63
|
self.changes.each do |attribute_name,value|
|
85
64
|
# do not record changes between nil <=> ''
|
86
65
|
# and ignore the changes for ignored columns
|
87
66
|
unless value[1].eql?(value[0]) || (value[1].blank?&&value[0].blank?) || self.ignore.include?(attribute_name.to_s)
|
88
|
-
|
89
|
-
value[0] = value[0].gsub("'", %q(\\\')) unless value[0].blank? || !value[0].is_a?(String)
|
90
|
-
value[1] = value[1].gsub("'", %q(\\\')) unless value[1].blank? || !value[1].is_a?(String)
|
91
|
-
user = ChangeLog.whodidit
|
92
|
-
if user.blank?
|
93
|
-
user = 'Unkown'
|
94
|
-
else
|
95
|
-
user = ChangeLog.whodidit.is_a?(String) ? ChangeLog.whodidit : ChangeLog.whodidit.id
|
96
|
-
end
|
97
|
-
time = Time.now.strftime("%Y-%m-%d %T")
|
98
|
-
column_values << '(' + ["'UPDATE'", self.id, "'#{self.class.table_name}'", "'#{user}'","'#{field_type}'", "'#{attribute_name}'", "'#{value[0]}'","'#{value[1]}'",ChangeLogs.get_version_number(self.id,self.class.table_name),"'#{time}'"].join(',') + ')'
|
67
|
+
changes << {:action=>'UPDATE',:record_id=>self.id,:table_name=>self.class.table_name,:user=>ChangeLog.whodidit,:attribute_name=>attribute_name,:old_value=>value[0],:new_value=>value[1],:version => ChangeLogs.get_version_number(self.id,self.class.table_name)}
|
99
68
|
end
|
100
69
|
end
|
101
|
-
|
102
|
-
insert_statement = "INSERT INTO `#{ChangeLogs.table_name}` (`#{column_names.join('`, `')}`) VALUES " + column_values.join( ',' ) + ";"
|
103
|
-
ChangeLogs.connection.execute( insert_statement )
|
70
|
+
ChangeLogs.update_change_log_record_with(changes)
|
104
71
|
end
|
105
72
|
|
106
73
|
def record_destroy
|
107
74
|
return '' unless switched_on?
|
108
|
-
|
75
|
+
changes = [{:action=>'DELETE',:table_name=>self.class.table_name,:record_id=>self.id,:user=>ChangeLog.whodidit,:version => ChangeLogs.get_version_number(self.id,self.class.table_name)}]
|
76
|
+
ChangeLogs.update_change_log_record_with(changes)
|
109
77
|
end
|
110
78
|
|
111
79
|
# Return a list of change_log records
|
112
80
|
# Return empty array if not record found
|
113
81
|
def change_logs
|
114
|
-
return ChangeLogs.
|
82
|
+
return ChangeLogs.where(['table_name= ? and record_id = ?',self.class.table_name,self.id]).order("created_at DESC")
|
115
83
|
end
|
116
84
|
|
117
85
|
# Return `true` if current record has a list of change_log records
|
data/lib/change_log/version.rb
CHANGED
data/test/boot.rb
CHANGED
data/test/change_log_test.rb
CHANGED
@@ -10,7 +10,7 @@ class ChangeLogTest < ActiveRecordTestCase
|
|
10
10
|
# insert an new record
|
11
11
|
# then check the total number
|
12
12
|
def test_update_change_log_record_with
|
13
|
-
option = {:action=>'INSERT', :record_id=>3,:table_name=>'test', :user=>'test',:attribute_name=>'test',:new_value=>'50',:version=>1}
|
13
|
+
option = [{:action=>'INSERT', :record_id=>3,:table_name=>'test', :user=>'test',:attribute_name=>'test',:new_value=>'50',:version=>1}]
|
14
14
|
assert_equal true, ChangeLogs.update_change_log_record_with(option)
|
15
15
|
assert_equal 3, ChangeLogs.count()
|
16
16
|
end
|
@@ -29,7 +29,7 @@ class ChangeLogTest < ActiveRecordTestCase
|
|
29
29
|
def test_change_log_logic
|
30
30
|
# find a test, update the total
|
31
31
|
version_number = ChangeLogs.get_version_number(1,'test')
|
32
|
-
option = {:action=>'UPDATE', :record_id=>1,:table_name=>'test', :user=>'peterz',:attribute_name=>'test',:new_value=>'999',:old_value=>'100',:version=>version_number}
|
32
|
+
option = [{:action=>'UPDATE', :record_id=>1,:table_name=>'test', :user=>'peterz',:attribute_name=>'test',:new_value=>'999',:old_value=>'100',:version=>version_number}]
|
33
33
|
assert_equal true, ChangeLogs.update_change_log_record_with(option)
|
34
34
|
change_log = ChangeLogs.find(version_number)
|
35
35
|
assert_equal 'peterz',change_log.user
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: change_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A gem for tracking who did what and when it happened -- keeps all the
|
15
15
|
changes done by user into a maintenance log
|