abbish_sequel_plugins 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/abbish_sequel_plugins.rb +12 -0
- data/lib/model/record_protection.rb +58 -0
- data/lib/model/record_timestamp.rb +56 -0
- data/lib/model/record_version.rb +53 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12695cf6acd408a1b30f5f36c847a2c1b1437fa7
|
4
|
+
data.tar.gz: a4ea39d1dbc60656040140eb3f0d6eb31086c419
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d097148453c3ab92a8072e47899204b8eb8980f019fd49d10fde137b8f56746cbda40464ee7ebfcdd11c3d49a6461ccceb3a4cf2eb20251e9c91eced09e5895
|
7
|
+
data.tar.gz: 44e448b1d7483b1d62736cd4fbac2f3a9827cf68fa9c241e5b48de58812ba1c170116ce49e52c6c31b331eebe1f10f0b21058c04722e5d7deb2522673e660b48
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.dirname(__FILE__) + '/model/record_version'
|
3
|
+
require File.dirname(__FILE__) + '/model/record_protection'
|
4
|
+
require File.dirname(__FILE__) + '/model/record_timestamp'
|
5
|
+
|
6
|
+
module Abbish
|
7
|
+
module Sequel
|
8
|
+
module Plugins
|
9
|
+
Version = '0.0.3'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Abbish
|
2
|
+
module Sequel
|
3
|
+
module Plugins
|
4
|
+
module Model
|
5
|
+
|
6
|
+
RecordProtectedError = Class.new(::StandardError)
|
7
|
+
|
8
|
+
module RecordProtection
|
9
|
+
def self.configure(model, options = {})
|
10
|
+
options = {
|
11
|
+
:enabled => true,
|
12
|
+
:column_protected => :record_protected,
|
13
|
+
:raise_protected_message => 'Cannot destroy protected record',
|
14
|
+
}.merge(options)
|
15
|
+
|
16
|
+
model.instance_eval do
|
17
|
+
self.record_protection_options = options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def record_protection_options=(options)
|
23
|
+
@record_protection_options = options
|
24
|
+
end
|
25
|
+
|
26
|
+
def record_protection_options
|
27
|
+
@record_protection_options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
|
33
|
+
def record_protected?
|
34
|
+
self.class.record_protection_options[:enabled] ? send("#{self.class.record_protection_options[:column_protected]}") == 1 : false
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_record_protected
|
38
|
+
send("#{self.class.record_protection_options[:column_protected]}=", 1) if self.class.record_protection_options[:enabled]
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_record_protected!
|
42
|
+
if self.class.record_protection_options[:enabled]
|
43
|
+
set_record_protected
|
44
|
+
self.save
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def before_destroy
|
49
|
+
super
|
50
|
+
raise RecordProtectedError, self.class.record_protection_options[:raise_protected_message] if record_protected?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Abbish
|
2
|
+
module Sequel
|
3
|
+
module Plugins
|
4
|
+
module Model
|
5
|
+
module RecordTimestamp
|
6
|
+
def self.configure(model, options = {})
|
7
|
+
options = {
|
8
|
+
:enabled => true,
|
9
|
+
:column_created_time => :record_created_time,
|
10
|
+
:column_updated_time => :record_updated_time,
|
11
|
+
}.merge(options)
|
12
|
+
|
13
|
+
model.instance_eval do
|
14
|
+
self.record_timestamp_options = options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def record_timestamp_options=(options)
|
20
|
+
@record_timestamp_options = options
|
21
|
+
end
|
22
|
+
|
23
|
+
def record_timestamp_options
|
24
|
+
@record_timestamp_options
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
|
30
|
+
def before_create
|
31
|
+
super
|
32
|
+
self.record_created_time = _get_time if self.class.record_timestamp_options[:enabled]
|
33
|
+
end
|
34
|
+
|
35
|
+
def before_update
|
36
|
+
super
|
37
|
+
if self.modified?
|
38
|
+
self.record_updated_time = _get_time if self.class.record_timestamp_options[:enabled]
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def _get_time
|
47
|
+
return Time.now
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Abbish
|
2
|
+
module Sequel
|
3
|
+
module Plugins
|
4
|
+
module Model
|
5
|
+
module RecordVersion
|
6
|
+
def self.configure(model, options = {})
|
7
|
+
options = {
|
8
|
+
:enabled => true,
|
9
|
+
:column_version => :record_version,
|
10
|
+
}.merge(options)
|
11
|
+
|
12
|
+
model.instance_eval do
|
13
|
+
self.record_version_options = options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def record_version_options=(options)
|
19
|
+
@record_version_options = options
|
20
|
+
end
|
21
|
+
|
22
|
+
def record_version_options
|
23
|
+
@record_version_options
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
|
29
|
+
def before_create
|
30
|
+
super
|
31
|
+
self.record_version = _get_version if self.class.record_version_options[:enabled]
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
def before_update
|
36
|
+
super
|
37
|
+
if self.modified?
|
38
|
+
self.record_version = _get_version if self.class.record_version_options[:enabled]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def _get_version
|
45
|
+
return Digest::MD5.hexdigest Time.now.to_f.to_s
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abbish_sequel_plugins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- abbish
|
@@ -10,13 +10,17 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2015-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: For more detail please visit https://github.com/abbish/abbish_sequel_plugins
|
14
14
|
email:
|
15
15
|
- me@abbish.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
|
-
files:
|
19
|
+
files:
|
20
|
+
- lib/abbish_sequel_plugins.rb
|
21
|
+
- lib/model/record_protection.rb
|
22
|
+
- lib/model/record_timestamp.rb
|
23
|
+
- lib/model/record_version.rb
|
20
24
|
homepage: https://github.com/abbish/abbish-sequel-plugins
|
21
25
|
licenses:
|
22
26
|
- MIT
|