change_log 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.change_log.gemspec.un~ +0 -0
- data/MIT-LICENSE +20 -0
- data/README +69 -0
- data/lib/change_log/config.rb +11 -0
- data/lib/change_log/controller.rb +29 -0
- data/lib/change_log/has_change_log.rb +99 -0
- data/lib/change_log/maintenance.rb +30 -0
- data/lib/change_log/version.rb +1 -1
- metadata +10 -3
Binary file
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 PETER ZHANG
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
h1. Change Log
|
2
|
+
|
3
|
+
A gem to keep all maintenances in database
|
4
|
+
|
5
|
+
|
6
|
+
h2. Install Change Log Gem
|
7
|
+
1. command:
|
8
|
+
<pre><code># gem install change_log</code</pre>
|
9
|
+
|
10
|
+
2. bundler
|
11
|
+
<pre><code>
|
12
|
+
# Gemfile in your application
|
13
|
+
gem 'change_log','0.0.1'
|
14
|
+
</code</pre>
|
15
|
+
then:
|
16
|
+
<pre><code>
|
17
|
+
bundle install
|
18
|
+
</code</pre>
|
19
|
+
|
20
|
+
h2. Create a table to keep all maintenance logs
|
21
|
+
|
22
|
+
1. generate a migration file
|
23
|
+
|
24
|
+
<pre><code>
|
25
|
+
class AddChangeLog < ActiveRecord::Migration
|
26
|
+
def self.up
|
27
|
+
create_table :change_log do |t|
|
28
|
+
t.integer :version, :null=>false # store version of each change
|
29
|
+
t.string :record_id,:limit=>30 # store the actual record id
|
30
|
+
t.string :table_name, :limti=>60 # store the table name
|
31
|
+
t.string :attribute_name,:limit=>60 # store the column name
|
32
|
+
t.string :user, :limit=>20 # store the user who made the change
|
33
|
+
t.string :action, :limit=>6 # store the change action: create, read, update, delete
|
34
|
+
t.text :old_value # the value before change
|
35
|
+
t.text :new_value # value after change
|
36
|
+
t.string :field_type, :limit=>30 # the column type eg. date, text, varchar, int etc
|
37
|
+
t.timestamps
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.down
|
42
|
+
drop_table :change_log
|
43
|
+
end
|
44
|
+
end
|
45
|
+
</code></pre>
|
46
|
+
|
47
|
+
Then:
|
48
|
+
<pre><code>rake db:migrate</code></pre>
|
49
|
+
|
50
|
+
h2. Use Change Log
|
51
|
+
1. Add current_user method in application_controller.rb
|
52
|
+
<pre><code>
|
53
|
+
# used by change log
|
54
|
+
def current_user # :nodoc:
|
55
|
+
return session[:user] # replace this with your own code to tell change_log who is the current user
|
56
|
+
end
|
57
|
+
</code></pre>
|
58
|
+
|
59
|
+
h2. Testing
|
60
|
+
TODO::
|
61
|
+
|
62
|
+
|
63
|
+
Author
|
64
|
+
------
|
65
|
+
|
66
|
+
Peter Zhang at NCS New Zealand.
|
67
|
+
Email: peterz@ncs.co.nz
|
68
|
+
|
69
|
+
Copyright (c) 2011 Peter Zhang, released under the MIT license
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ChangeLog
|
2
|
+
module Controller
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.before_filter :set_change_log_whodidit
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
# Returns the user who is responsible for any changes that occur.
|
11
|
+
# By default this calls `current_user` and returns the result.
|
12
|
+
#
|
13
|
+
# Override this method in your controller to call a different
|
14
|
+
# method, e.g. `current_login_user`, or anything.
|
15
|
+
def user_for_change_log
|
16
|
+
current_user rescue nil
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# Tells ChangeLog who is responsible for any changes.
|
22
|
+
def set_change_log_whodidit
|
23
|
+
::ChangeLog.whodidit = user_for_change_log
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ActionController::Base.send :include, ChangeLog::Controller
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module ChangeLog
|
2
|
+
module Model
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Declare this in your model to keep a change log for every create, update, and destroy.
|
11
|
+
#
|
12
|
+
# Options:
|
13
|
+
# :ignore an array of attributes will be ingored when saving changes into change log.
|
14
|
+
def enable_change_log(options = {})
|
15
|
+
send :include, InstanceMethods
|
16
|
+
|
17
|
+
cattr_accessor :ignore, :whodidit
|
18
|
+
self.ignore = (options[:ignore] || []).map &:to_s
|
19
|
+
|
20
|
+
# Indicates whether or not ChangeLog is active for this class.
|
21
|
+
# This is independent of whether ChangeLog is globally enabled or disabled.
|
22
|
+
cattr_accessor :change_log_active
|
23
|
+
self.change_log_active = true
|
24
|
+
|
25
|
+
after_create :record_create
|
26
|
+
before_update :record_update
|
27
|
+
after_destroy :record_destroy
|
28
|
+
end
|
29
|
+
|
30
|
+
# Switches ChangeLog off for this class.
|
31
|
+
def change_log_off
|
32
|
+
self.change_log_active = false
|
33
|
+
end
|
34
|
+
|
35
|
+
# Switches ChangeLog on for this class.
|
36
|
+
def change_log_on
|
37
|
+
self.change_log_active = true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Wrap the following methods in a module so we can include them only in the
|
42
|
+
# ActiveRecord models that declare `enable_change_log`.
|
43
|
+
module InstanceMethods
|
44
|
+
def record_create
|
45
|
+
# do nothing if the change log is not turned on
|
46
|
+
return '' unless switched_on?
|
47
|
+
# saving changes to maintenance log
|
48
|
+
self.attributes.map do |key,value|
|
49
|
+
unless self.ignore.include?(key.to_sym)
|
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
|
+
Maintenance.update_maintenance_record_with(option)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def record_update
|
57
|
+
# do nothing if the change log is not turned on and no changes has been made
|
58
|
+
return '' unless switched_on? && self.valid? && self.changed?
|
59
|
+
|
60
|
+
self.changes.each do |attribute_name,value|
|
61
|
+
# do not record changes between nil <=> ''
|
62
|
+
# and ignore the changes for ignored columns
|
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 => Maintenance.get_version_number(self.id,self.class.table_name)}
|
65
|
+
Maintenance.update_maintenance_record_with(option)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def record_destroy
|
71
|
+
return '' unless switched_on?
|
72
|
+
Maintenance.update_maintenance_record_with({:action=>'DELETE',:table_name=>self.class.table_name,:record_id=>self.id,:user=>ChangeLog.whodidit,:version => Maintenance.get_version_number(self.id,self.class.table_name)})
|
73
|
+
end
|
74
|
+
|
75
|
+
# Return a list of maintenance records
|
76
|
+
# Return empty array if not record found
|
77
|
+
def change_logs
|
78
|
+
return Maintenance.find(:all,:conditions=>['table_name= ? and record_id = ?',self.class.table_name,self.id],:order=>"created_at DESC")
|
79
|
+
end
|
80
|
+
|
81
|
+
# Return `true` if current record has a list of maintenance records
|
82
|
+
# otherwise `false`.
|
83
|
+
def has_change_log?
|
84
|
+
return (Maintenance.count(:conditions=>['table_name= ? and record_id = ?',self.class.table_name,self.id]) > 0) ? true : false
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
# Returns `true` if ChangeLog is globally enabled and active for this class,
|
90
|
+
# `false` otherwise.
|
91
|
+
def switched_on?
|
92
|
+
ChangeLog.enabled? && self.class.change_log_active
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
ActiveRecord::Base.send :include, ChangeLog::Model
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Maintenance < ActiveRecord::Base
|
2
|
+
# Set table name to "change_logs"
|
3
|
+
set_table_name 'change_logs'
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
# Save maintenance details when options
|
8
|
+
def self.update_maintenance_record_with(option={})
|
9
|
+
record = Maintenance.new(option)
|
10
|
+
record.field_type = get_field_type(option[:table_name],option[:attribute_name]) unless option[:action].eql?('DELETE')
|
11
|
+
record.created_at = Time.now
|
12
|
+
record.save
|
13
|
+
end
|
14
|
+
|
15
|
+
# return the lastest version number for this change
|
16
|
+
def self.get_version_number(id,table_name)
|
17
|
+
latest_version = Maintenance.maximum(:version,:conditions=>['record_id = ? and table_name = ?',id,table_name])
|
18
|
+
return latest_version.nil? ? 1 : latest_version.next
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get_field_type(table_name,field_name)
|
22
|
+
return 'Error' if table_name.blank?||field_name.blank?
|
23
|
+
ActiveRecord::Base.connection.columns(table_name).each do |field|
|
24
|
+
return field.sql_type if field.name.eql?(field_name)
|
25
|
+
end
|
26
|
+
rescue
|
27
|
+
return 'Error'
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
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: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Peter Zhang
|
@@ -42,11 +42,18 @@ extensions: []
|
|
42
42
|
extra_rdoc_files: []
|
43
43
|
|
44
44
|
files:
|
45
|
+
- .change_log.gemspec.un~
|
45
46
|
- .gitignore
|
46
47
|
- Gemfile
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README
|
47
50
|
- Rakefile
|
48
51
|
- change_log.gemspec
|
49
52
|
- lib/change_log.rb
|
53
|
+
- lib/change_log/config.rb
|
54
|
+
- lib/change_log/controller.rb
|
55
|
+
- lib/change_log/has_change_log.rb
|
56
|
+
- lib/change_log/maintenance.rb
|
50
57
|
- lib/change_log/version.rb
|
51
58
|
has_rdoc: true
|
52
59
|
homepage: http://www.ncs.co.nz
|