change_log 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,11 @@ It automatically saves who made the changes at what time and what has been chang
5
5
  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
+ * Note:
9
+ Change Log version 1.0.2 is temporarily locked down to Rails 2.3.x and mysql database only.
10
+ Another version with fully support to Rails 3.x and multiple databases
11
+ will be available soon in another branch on github.
12
+
8
13
 
9
14
  h2. Install Change Log Gem
10
15
 
@@ -116,16 +121,41 @@ ChangeLogs.find(:all,:conditions=>['user = ?', 'peterz'])</code></pre>
116
121
  <pre><code># List all changes for table 'accounts'
117
122
  ChangeLogs.find(:all,:conditions=>['table_name = ?', 'accounts'])</code></pre>
118
123
 
119
- * Note:
120
- It is ok if you want to use other table name instead of 'change_logs',
121
- choose your preferred table name and run the migration.
122
- Just remember in your environment.rb file, you need to tell change_log gem
123
- what is your table name:
124
+ 4. *Turn ChangeLogs off in testing environment*
125
+ You can globally turn it off for your testing.
126
+ <pre><code># config/environment.rb
127
+ ChangeLog.enabled = false if RAILS_ENV == 'test'
128
+ </code></pre>
129
+
130
+ 5. *Database and table name*
131
+ change_log gem can save changes into separate database from the main application.
132
+ The database could be MySQL, SQLite or any other database that active record is happy to connect with.
133
+
134
+ Here is an example of database.yml when using separate database for 'change_logs':
135
+ <pre><code>change_logs:
136
+ adapter: mysql2
137
+ encoding: utf8
138
+ database: change_logs
139
+ username: username
140
+ password: ********
141
+ host: hostname
142
+ port: 3306
143
+ </code></pre>
144
+
145
+ And also you need to tell change_log gem to establish the connection.
146
+ <pre><code># config/environment.rb
147
+ ChangeLogs.establish_connection(:change_logs)
148
+ </code></pre>
149
+
150
+ Table name is also configurable. Instead of 'change_logs', choose your preferred table name and run the migration.
151
+ Just remember in your environment.rb file, you need to tell change_log gem
152
+ what is your table name:
124
153
 
125
154
  <pre><code># config/environment.rb
126
155
  ChangeLogs.set_table_name('hr_maintenances')
127
156
  </code></pre>
128
157
 
158
+
129
159
  h2. Wish List
130
160
  Please email me if you have any enquiry.
131
161
 
@@ -136,4 +166,4 @@ h3. Author
136
166
  Peter Zhang at NCS New Zealand.
137
167
  Email: peterz@ncs.co.nz
138
168
 
139
- Copyright (c) 2011 Peter Zhang and NCS LTD, released under the MIT license
169
+ Copyright (c) 2011 Peter Zhang and NCS LTD, released under the MIT license
@@ -1,6 +1,8 @@
1
1
  class ChangeLogs < ActiveRecord::Base
2
2
  # Set table name to "change_logs"
3
- set_table_name 'change_logs'
3
+ def table_name
4
+ :change_logs
5
+ end
4
6
 
5
7
  private
6
8
 
@@ -41,30 +41,53 @@ module ChangeLog
41
41
  # Wrap the following methods in a module so we can include them only in the
42
42
  # ActiveRecord models that declare `enable_change_log`.
43
43
  module InstanceMethods
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.
44
48
  def record_create
45
49
  # do nothing if the change log is not turned on
46
50
  return '' unless switched_on?
51
+
52
+ # generate the single sql insert statement
53
+ column_values = []
47
54
  # saving changes to change log
48
55
  self.attributes.map do |key,value|
49
56
  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
- ChangeLogs.update_change_log_record_with(option)
57
+ field_type = ChangeLogs.get_field_type(self.class.table_name,key)
58
+ value = value.gsub("'", %q(\\\')) unless value.blank?
59
+ column_values << '(' + ["'INSERT'", self.id, "'#{self.class.table_name}'", "'#{ChangeLog.whodidit}'", "'#{field_type}'", "'#{key}'", "'#{value}'",1].join(',') + ')'
52
60
  end
53
61
  end
62
+ column_names = ['action','record_id','table_name','user','field_type','attribute_name','new_value','version']
63
+ insert_statement = "INSERT INTO `#{ChangeLogs.table_name}` (`#{column_names.join('`, `')}`) VALUES " + column_values.join( ',' ) + ";"
64
+ ActiveRecord::Base.connection.execute( insert_statement )
54
65
  end
55
66
 
67
+ # NOTE::This version's change_log is temporarily locked down to Rails 2.3.x
68
+ # and mysql database. Another version with fully support to Rails 3.x and multiple databases
69
+ # will be available soon in another branch on github.
56
70
  def record_update
57
71
  # do nothing if the change log is not turned on and no changes has been made
58
72
  return '' unless switched_on? && self.valid? && self.changed?
59
73
 
74
+ # generate the single sql insert statement
75
+ column_values = []
76
+ # saving changes to change log
60
77
  self.changes.each do |attribute_name,value|
61
78
  # do not record changes between nil <=> ''
62
79
  # and ignore the changes for ignored columns
63
80
  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 => ChangeLogs.get_version_number(self.id,self.class.table_name)}
65
- ChangeLogs.update_change_log_record_with(option)
81
+ field_type = ChangeLogs.get_field_type(self.class.table_name,attribute_name)
82
+ value[0] = value[0].gsub("'", %q(\\\')) unless value[0].blank?
83
+ value[1] = value[1].gsub("'", %q(\\\')) unless value[1].blank?
84
+
85
+ column_values << '(' + ["'UPDATE'", self.id, "'#{self.class.table_name}'", "'#{ChangeLog.whodidit}'","'#{field_type}'", "'#{attribute_name}'", "'#{value[0]}'","'#{value[1]}'",ChangeLogs.get_version_number(self.id,self.class.table_name)].join(',') + ')'
66
86
  end
67
- end
87
+ end
88
+ column_names = ['action','record_id','table_name','user','field_type','attribute_name','old_value','new_value','version']
89
+ insert_statement = "INSERT INTO `#{ChangeLogs.table_name}` (`#{column_names.join('`, `')}`) VALUES " + column_values.join( ',' ) + ";"
90
+ ActiveRecord::Base.connection.execute( insert_statement )
68
91
  end
69
92
 
70
93
  def record_destroy
@@ -96,4 +119,4 @@ module ChangeLog
96
119
  end
97
120
  end
98
121
 
99
- ActiveRecord::Base.send :include, ChangeLog::Model
122
+ ActiveRecord::Base.send :include, ChangeLog::Model
@@ -1,3 +1,3 @@
1
1
  module ChangeLog
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
metadata CHANGED
@@ -1,34 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: change_log
3
- version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- - 1
10
- version: 1.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Peter Zhang
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-01-25 00:00:00 +13:00
19
- default_executable:
12
+ date: 2013-05-08 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
- description: A gem for tracking who did what and when it happened -- keeps all the changes done by user into a maintenance log
23
- email:
14
+ description: A gem for tracking who did what and when it happened -- keeps all the
15
+ changes done by user into a maintenance log
16
+ email:
24
17
  - peterz@ncs.co.nz
25
18
  executables: []
26
-
27
19
  extensions: []
28
-
29
20
  extra_rdoc_files: []
30
-
31
- files:
21
+ files:
32
22
  - Gemfile
33
23
  - MIT-LICENSE
34
24
  - README.textile
@@ -49,41 +39,31 @@ files:
49
39
  - test/lib/activerecord_test_case.rb
50
40
  - test/lib/activerecord_test_connector.rb
51
41
  - test/lib/load_fixtures.rb
52
- has_rdoc: true
53
42
  homepage: http://www.ncs.co.nz
54
43
  licenses: []
55
-
56
44
  post_install_message:
57
45
  rdoc_options: []
58
-
59
- require_paths:
46
+ require_paths:
60
47
  - lib
61
- required_ruby_version: !ruby/object:Gem::Requirement
48
+ required_ruby_version: !ruby/object:Gem::Requirement
62
49
  none: false
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
68
- - 0
69
- version: "0"
70
- required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
55
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
- version: "0"
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
79
60
  requirements: []
80
-
81
61
  rubyforge_project: change_log
82
- rubygems_version: 1.3.7
62
+ rubygems_version: 1.8.24
83
63
  signing_key:
84
64
  specification_version: 3
85
65
  summary: Change log gem records every changes for the active record model
86
- test_files:
66
+ test_files:
87
67
  - test/boot.rb
88
68
  - test/change_log_test.rb
89
69
  - test/database.yml