destroyed_record_collector 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/destroyed_record_collector.rb +92 -0
- metadata +65 -0
@@ -0,0 +1,92 @@
|
|
1
|
+
module DestroyedRecordCollector
|
2
|
+
BACKED_AT_COLUMN_NAME = :backed_up_at
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
base.send(:before_destroy, :write_backup)
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
|
11
|
+
def write_backup
|
12
|
+
begin
|
13
|
+
make_backup unless (self.respond_to?(:exclude_from_backup) and (self.exclude_from_backup == true))
|
14
|
+
rescue Exception => e
|
15
|
+
Rails.logger.debug "Exception occurred: #{e.message.inspect}"
|
16
|
+
e.backtrace.each { |line| Rails.logger.debug(line.inspect) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def make_backup
|
21
|
+
arClass = get_class
|
22
|
+
|
23
|
+
arClass.establish_connection(new_backup_connection_available) unless new_backup_connection_available.nil?
|
24
|
+
arClass.connection.tables #To register an active connection in connection_pool
|
25
|
+
|
26
|
+
if arClass.connected?
|
27
|
+
con_configurations = ActiveRecord::Base.configurations
|
28
|
+
arClass.table_name = (((not con_configurations["#{Rails.env}_backup"].nil?) and (con_configurations[Rails.env] != con_configurations["#{Rails.env}_backup"])) ? self.class.table_name : "#{self.class.table_name}_backup")
|
29
|
+
generate_table(arClass) unless arClass.table_exists?
|
30
|
+
do_record_backup(arClass)
|
31
|
+
else
|
32
|
+
raise "BackupConnectionFailedToEstablish"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def new_backup_connection_available
|
37
|
+
ActiveRecord::Base.configurations["#{Rails.env}_backup"]
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_column(arClass, column_name)
|
41
|
+
arClass.connection.change_table arClass.table_name do |t|
|
42
|
+
t.column(column_name, columns_hash[column_name].type)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def generate_table(arClass)
|
47
|
+
arClass.connection.create_table arClass.table_name do |t|
|
48
|
+
self.attributes.keys.each do |attribute|
|
49
|
+
t.column attribute, columns_hash[attribute].type
|
50
|
+
end
|
51
|
+
t.column BACKED_AT_COLUMN_NAME, :datetime
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def columns_hash
|
56
|
+
self.class.columns_hash
|
57
|
+
end
|
58
|
+
|
59
|
+
def do_record_backup(arClass)
|
60
|
+
backup_entity = arClass.new
|
61
|
+
|
62
|
+
attributes.keys.each { |attribute| add_column(arClass, attribute) unless backup_entity.respond_to?("#{attribute}=") }
|
63
|
+
arClass.reset_column_information
|
64
|
+
backup_entity = arClass.new
|
65
|
+
self.attributes.merge({BACKED_AT_COLUMN_NAME => Time.now}).each do |attribute_name, attribute_value|
|
66
|
+
backup_entity.send("#{attribute_name}=", attribute_value)
|
67
|
+
end
|
68
|
+
backup_entity.save
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_class(class_name = "#{self.class.name}Backup")
|
72
|
+
is_an_armodel?(class_name) ? class_name.constantize : create_class(class_name)
|
73
|
+
end
|
74
|
+
|
75
|
+
def is_an_armodel?(class_name)
|
76
|
+
begin
|
77
|
+
return (class_name.constantize.ancestors.include?(ActiveRecord::Base))
|
78
|
+
rescue Exception => e
|
79
|
+
return false
|
80
|
+
end
|
81
|
+
true
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_class(name, inherited_from = ActiveRecord::Base)
|
85
|
+
Object.const_set(name, Class.new(inherited_from))
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
ActiveRecord::Base.send :include, DestroyedRecordCollector
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: destroyed_record_collector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Annu Yadav
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-06 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " Gem for collecting the deleted record for ActiveRecord to a new table named as (original_name_of_table)_backup in same database or \n (original_name_of_table) in other database if other database configurations are provided in database.yml with [\"#{RUBY_ENV}_backup\"].\n"
|
22
|
+
email: life.annu.yadav@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/destroyed_record_collector.rb
|
31
|
+
homepage: http://rubygems.org/gems/destroyed_record_collector
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Destroyed record collector
|
64
|
+
test_files: []
|
65
|
+
|