inactivable 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/inactivable.gemspec +21 -0
- data/lib/inactivable.rb +91 -0
- data/lib/inactivable/exceptions.rb +12 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b22387d9d9c31c74aea591458d433599cb42b0b
|
4
|
+
data.tar.gz: e74882840153c660e78131e2cdf409cd8de5fd24
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 103cbe653130c598f833a5a5fb683f475ca3c0f2ac1deea6f3400ec7d12659cce0a97083d6b4adc910b83f1306b5b2c5c31e31106450b032d2258c311389985b
|
7
|
+
data.tar.gz: 2d19804373d2f4aa3212035b484e76b992d112455d3f56a511a7efcfe986ce609c5bbf05b6bde7ab9d1e8cee9520bb7181ffae403b57e47e11247eabd232d57b
|
data/inactivable.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = "inactivable"
|
4
|
+
s.version = "0.0.0"
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Rajeev Kannav Sharma"]
|
7
|
+
s.date = "2013-05-19"
|
8
|
+
s.description = "Inactivable :- inactivate! - Model Object to for clone it other table + remove from present, also can be reactivate!"
|
9
|
+
s.email = "rajeevsharma86@gmail.com"
|
10
|
+
s.files = [
|
11
|
+
"inactivable.gemspec",
|
12
|
+
"lib/inactivable/exceptions.rb",
|
13
|
+
"lib/inactivable.rb"
|
14
|
+
]
|
15
|
+
s.homepage = "http://github.com/rajeevkannav/inactivable"
|
16
|
+
s.licenses = ["MIT"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubygems_version = "2.0.3"
|
19
|
+
s.summary = "Core Usage of Soft Delete Via Inactivable"
|
20
|
+
end
|
21
|
+
|
data/lib/inactivable.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# Todo : remove warning: already initialized constant inactivable
|
2
|
+
# Todo : Exceptions
|
3
|
+
# Todo : CallBacks Next
|
4
|
+
# Todo : Table Collide system
|
5
|
+
require 'inactivable/exceptions'
|
6
|
+
module Inactivable
|
7
|
+
BACKED_AT_COLUMN_NAME = "inactivated_at"
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
def inactivate!
|
14
|
+
inactivated_object = self.class.inactivated_model_class.new(attributes.merge(BACKED_AT_COLUMN_NAME => Time.now))
|
15
|
+
inactivated_object.send("#{self.class.primary_key}=", attributes[self.class.primary_key])
|
16
|
+
inactivated_object.save
|
17
|
+
self.delete ##
|
18
|
+
inactivated_object
|
19
|
+
end
|
20
|
+
|
21
|
+
def reactivate!
|
22
|
+
_attributes = attributes.clone
|
23
|
+
_attributes.delete(nil)
|
24
|
+
_attributes.delete(BACKED_AT_COLUMN_NAME)
|
25
|
+
|
26
|
+
reactivated_object = self.class.reactivated_model_class.new(_attributes)
|
27
|
+
reactivated_object.send("#{self.class.reactivated_model_class.primary_key}=", _attributes[self.class.reactivated_model_class.primary_key])
|
28
|
+
reactivated_object.save
|
29
|
+
puts self.inspect
|
30
|
+
self.delete
|
31
|
+
reactivated_object
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
|
36
|
+
#TODO: make it more generic so where instead
|
37
|
+
def inactivated_list(option)
|
38
|
+
inactivated_model_class.find(option)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_other_model_name
|
42
|
+
self.columns_hash[BACKED_AT_COLUMN_NAME].nil? ? "Inactive#{self.name}" : self.name.gsub("Inactive", "")
|
43
|
+
end
|
44
|
+
|
45
|
+
def inactivated_model_class
|
46
|
+
inactivated_klass = Object.const_set(get_other_model_name, Class.new(ActiveRecord::Base))
|
47
|
+
adjust_table_definition(inactivated_klass)
|
48
|
+
inactivated_klass.send("primary_key=", primary_key)
|
49
|
+
inactivated_klass.send(:include, Inactivable)
|
50
|
+
inactivated_klass
|
51
|
+
end
|
52
|
+
|
53
|
+
def adjust_table_definition(model)
|
54
|
+
is_schema_changed = false
|
55
|
+
unless model.table_exists?
|
56
|
+
puts "Creating #{model.table_name}"
|
57
|
+
connection.create_table "#{model.table_name}", :id => false do |t|
|
58
|
+
t.column :inactivated_at, :datetime
|
59
|
+
end
|
60
|
+
is_schema_changed = true
|
61
|
+
else
|
62
|
+
puts "#{model.table_name} already exists"
|
63
|
+
end
|
64
|
+
|
65
|
+
#TODO: add functionality to update column definition on column definition change
|
66
|
+
self.columns_hash.each do |column_name, column_object|
|
67
|
+
if model.columns_hash[column_name].nil?
|
68
|
+
puts "Adding column #{column_name}"
|
69
|
+
connection.change_table "#{model.table_name}" do |t|
|
70
|
+
t.column(column_name, columns_hash[column_name].type)
|
71
|
+
end
|
72
|
+
is_schema_changed = true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
if is_schema_changed
|
77
|
+
model.connection.schema_cache.clear!
|
78
|
+
model.reset_column_information
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def reactivated_model_class
|
83
|
+
reactivated_klass = Object.const_set(get_other_model_name, Class.new(ActiveRecord::Base))
|
84
|
+
reactivated_klass.send("primary_key=", primary_key)
|
85
|
+
reactivated_klass.send(:include, Inactivable)
|
86
|
+
reactivated_klass
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Exceptions
|
2
|
+
|
3
|
+
class NotAnActiveRecordClassError < StandardError;
|
4
|
+
end
|
5
|
+
class DeactivatedAlreadyError < StandardError;
|
6
|
+
end
|
7
|
+
class BackupConnectionFailedToEstablishError < StandardError;
|
8
|
+
end
|
9
|
+
class ActivatedAlreadyError < StandardError;
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inactivable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rajeev Kannav Sharma
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Inactivable :- inactivate! - Model Object to for clone it other table
|
14
|
+
+ remove from present, also can be reactivate!
|
15
|
+
email: rajeevsharma86@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- inactivable.gemspec
|
21
|
+
- lib/inactivable/exceptions.rb
|
22
|
+
- lib/inactivable.rb
|
23
|
+
homepage: http://github.com/rajeevkannav/inactivable
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.0.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Core Usage of Soft Delete Via Inactivable
|
47
|
+
test_files: []
|