change_log 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +43 -23
- data/lib/change_log.rb +2 -2
- data/lib/change_log/{maintenance.rb → change_logs.rb} +5 -5
- data/lib/change_log/has_change_log.rb +9 -9
- data/lib/change_log/version.rb +1 -1
- metadata +5 -5
data/README.textile
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
h1. Change Log
|
2
2
|
|
3
|
-
A gem to keep all
|
3
|
+
A gem to keep all changes about the record in database.
|
4
|
+
It automatically saves who made the changes at what time and what has been changed into a single database table.
|
5
|
+
You can choose the columns which you want to keep the changes.
|
6
|
+
Ignores columns like 'updated_at', 'created_at' and 'password' etc.
|
4
7
|
|
5
8
|
|
6
9
|
h2. Install Change Log Gem
|
@@ -11,7 +14,7 @@ h2. Install Change Log Gem
|
|
11
14
|
<pre><code>config.gem 'change_log'</code></pre>
|
12
15
|
|
13
16
|
|
14
|
-
2. by bundler:
|
17
|
+
2. or by bundler:
|
15
18
|
<pre><code># Gemfile in your application
|
16
19
|
gem 'change_log'
|
17
20
|
</code></pre>
|
@@ -19,11 +22,13 @@ gem 'change_log'
|
|
19
22
|
Then:
|
20
23
|
<pre><code>bundle install</code></pre>
|
21
24
|
|
22
|
-
* Note:
|
23
|
-
you may get "enable_change_log" method missing error.
|
24
|
-
|
25
|
+
* Note:
|
26
|
+
If you use bundler with Rails 2.3.x, you may get "enable_change_log" method missing error.
|
27
|
+
This is because Rails 2.3.x comes with its own gem handling.
|
28
|
+
You need to override that and replace it with support for Bundler.
|
29
|
+
Visit "Bundler Website":http://gembundler.com/rails23.html to find out how.
|
25
30
|
|
26
|
-
h2. Create a table to keep all
|
31
|
+
h2. Create a table to keep all changes
|
27
32
|
|
28
33
|
Generate a migration file
|
29
34
|
|
@@ -51,32 +56,22 @@ h2. Create a table to keep all maintenance logs
|
|
51
56
|
</code></pre>
|
52
57
|
|
53
58
|
Then:
|
54
|
-
<pre><code>rake db:migrate</code></pre>
|
55
|
-
|
56
|
-
* Note: It is ok if you want to use other table name instead of 'change_logs',
|
57
|
-
choose your preferred table name and run the migration.
|
58
|
-
Just remember in your environment.rb file, you need to tell change_log gem
|
59
|
-
what is your table name:
|
60
|
-
<pre><code># config/environment.rb
|
61
|
-
Maintenance.set_table_name('hr_maintenances')
|
62
|
-
</code></pre>
|
63
|
-
|
64
|
-
* Note: Maintenance model is core ActiveRecord model used by change_log.
|
59
|
+
<pre><code>rake db:migrate</code></pre>
|
65
60
|
|
66
61
|
h2. Use Change Log
|
67
62
|
|
68
|
-
1.
|
69
|
-
<pre><code>config.gem 'change_log'</code></pre>
|
63
|
+
1. *Add current_user Method in application_controller.rb*
|
70
64
|
|
71
|
-
|
65
|
+
This method will tell change_log who is the current user
|
72
66
|
<pre><code>
|
73
|
-
# used by change log
|
74
67
|
def current_user
|
75
|
-
return session[:user] # replace this with your own code
|
68
|
+
return session[:user] # replace this with your own code
|
76
69
|
end
|
77
70
|
</code></pre>
|
78
71
|
|
79
|
-
|
72
|
+
2. *ActiveRecord Models*
|
73
|
+
If you want to enable change_log for any ActiveRecord Model,
|
74
|
+
just put following line in very beginning of model file.
|
80
75
|
<pre><code>
|
81
76
|
enable_change_log :ignore=>[:updated_at]
|
82
77
|
</code></pre>
|
@@ -85,6 +80,31 @@ Put any columns you do not want to keep in the change log table in :ignore optio
|
|
85
80
|
eg. the password hash
|
86
81
|
|
87
82
|
|
83
|
+
3. *About the ChangeLogs Model*
|
84
|
+
ChangeLogs model is core ActiveRecord model used by change_log gem.
|
85
|
+
|
86
|
+
You can use it directly in your model, controller even in helper.
|
87
|
+
|
88
|
+
For example:
|
89
|
+
<pre><code># List all changes
|
90
|
+
ChangeLogs.find(:all)</code></pre>
|
91
|
+
|
92
|
+
<pre><code># List all changes made by user 'peterz'
|
93
|
+
ChangeLogs.find(:all,:conditions=>['user = ?', 'peterz'])</code></pre>
|
94
|
+
|
95
|
+
<pre><code># List all changes for table 'accounts'
|
96
|
+
ChangeLogs.find(:all,:conditions=>['table_name = ?', 'accounts'])</code></pre>
|
97
|
+
|
98
|
+
* Note:
|
99
|
+
It is ok if you want to use other table name instead of 'change_logs',
|
100
|
+
choose your preferred table name and run the migration.
|
101
|
+
Just remember in your environment.rb file, you need to tell change_log gem
|
102
|
+
what is your table name:
|
103
|
+
|
104
|
+
<pre><code># config/environment.rb
|
105
|
+
ChangeLogs.set_table_name('hr_maintenances')
|
106
|
+
</code></pre>
|
107
|
+
|
88
108
|
h2. Testing
|
89
109
|
|
90
110
|
TODO
|
data/lib/change_log.rb
CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
|
|
3
3
|
require 'change_log/config'
|
4
4
|
require 'change_log/controller'
|
5
5
|
require 'change_log/has_change_log'
|
6
|
-
require 'change_log/
|
6
|
+
require 'change_log/change_logs'
|
7
7
|
|
8
8
|
# ChangeLog's module methods can be called in both models and controllers.
|
9
9
|
module ChangeLog
|
@@ -19,7 +19,7 @@ module ChangeLog
|
|
19
19
|
!!ChangeLog.config.enabled
|
20
20
|
end
|
21
21
|
|
22
|
-
# Returns who is
|
22
|
+
# Returns who is responsible for any changes that occur.
|
23
23
|
def self.whodidit
|
24
24
|
change_log_store[:whodidit]
|
25
25
|
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
class
|
1
|
+
class ChangeLogs < ActiveRecord::Base
|
2
2
|
# Set table name to "change_logs"
|
3
3
|
set_table_name 'change_logs'
|
4
4
|
|
5
5
|
private
|
6
6
|
|
7
|
-
# Save
|
8
|
-
def self.
|
9
|
-
record =
|
7
|
+
# Save Change Log details when options
|
8
|
+
def self.update_change_log_record_with(option={})
|
9
|
+
record = ChangeLogs.new(option)
|
10
10
|
record.field_type = get_field_type(option[:table_name],option[:attribute_name]) unless option[:action].eql?('DELETE')
|
11
11
|
record.created_at = Time.now
|
12
12
|
record.save
|
@@ -14,7 +14,7 @@ class Maintenance < ActiveRecord::Base
|
|
14
14
|
|
15
15
|
# return the latest version number for this change
|
16
16
|
def self.get_version_number(id,table_name)
|
17
|
-
latest_version =
|
17
|
+
latest_version = ChangeLogs.maximum(:version,:conditions=>['record_id = ? and table_name = ?',id,table_name])
|
18
18
|
return latest_version.nil? ? 1 : latest_version.next
|
19
19
|
end
|
20
20
|
|
@@ -44,11 +44,11 @@ module ChangeLog
|
|
44
44
|
def record_create
|
45
45
|
# do nothing if the change log is not turned on
|
46
46
|
return '' unless switched_on?
|
47
|
-
# saving changes to
|
47
|
+
# saving changes to change log
|
48
48
|
self.attributes.map do |key,value|
|
49
49
|
unless self.ignore.include?(key.to_sym)
|
50
50
|
option = {:action=>'INSERT', :record_id=>self.id,:table_name=>self.class.table_name, :user=>ChangeLog.whodidit,:attribute_name=>key,:new_value=>value,:version=>1}
|
51
|
-
|
51
|
+
ChangeLogs.update_change_log_record_with(option)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -61,27 +61,27 @@ module ChangeLog
|
|
61
61
|
# do not record changes between nil <=> ''
|
62
62
|
# and ignore the changes for ignored columns
|
63
63
|
unless value[1].eql?(value[0]) || (value[1].blank?&&value[0].blank?) || self.ignore.include?(attribute_name.to_s)
|
64
|
-
option = {: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 =>
|
65
|
-
|
64
|
+
option = {: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)}
|
65
|
+
ChangeLogs.update_change_log_record_with(option)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
def record_destroy
|
71
71
|
return '' unless switched_on?
|
72
|
-
|
72
|
+
ChangeLogs.update_change_log_record_with({: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)})
|
73
73
|
end
|
74
74
|
|
75
|
-
# Return a list of
|
75
|
+
# Return a list of change_log records
|
76
76
|
# Return empty array if not record found
|
77
77
|
def change_logs
|
78
|
-
return
|
78
|
+
return ChangeLogs.find(:all,:conditions=>['table_name= ? and record_id = ?',self.class.table_name,self.id],:order=>"created_at DESC")
|
79
79
|
end
|
80
80
|
|
81
|
-
# Return `true` if current record has a list of
|
81
|
+
# Return `true` if current record has a list of change_log records
|
82
82
|
# otherwise `false`.
|
83
83
|
def has_change_log?
|
84
|
-
return (
|
84
|
+
return (ChangeLogs.count(:conditions=>['table_name= ? and record_id = ?',self.class.table_name,self.id]) > 0) ? true : false
|
85
85
|
end
|
86
86
|
|
87
87
|
private
|
data/lib/change_log/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: change_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Peter Zhang
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-20 00:00:00 +13:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -48,10 +48,10 @@ files:
|
|
48
48
|
- Rakefile
|
49
49
|
- change_log.gemspec
|
50
50
|
- lib/change_log.rb
|
51
|
+
- lib/change_log/change_logs.rb
|
51
52
|
- lib/change_log/config.rb
|
52
53
|
- lib/change_log/controller.rb
|
53
54
|
- lib/change_log/has_change_log.rb
|
54
|
-
- lib/change_log/maintenance.rb
|
55
55
|
- lib/change_log/version.rb
|
56
56
|
has_rdoc: true
|
57
57
|
homepage: http://www.ncs.co.nz
|