operator_recordable 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -2
- data/README.md +6 -0
- data/lib/operator_recordable/configuration.rb +12 -0
- data/lib/operator_recordable/recorder.rb +29 -55
- data/lib/operator_recordable/version.rb +1 -1
- data/lib/operator_recordable.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e05ee5e40a24819f5628847408a64657df213e1f96bf7060b87b9b86f45d6aa
|
4
|
+
data.tar.gz: 6491c0178099e81c2b423e5344e018b76de979e76009d7ef21df9d3bb0bfdec6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d24f2ffdcb2a74658cf298da772d5b5945c56c5283b0087fb0f52c4d07afb56a144265a26633df7fb5c5399d4aed38e43f1a4700f489d5741bccca24867748f1
|
7
|
+
data.tar.gz: a4803ce15c437240371fb6e32433880e2862d2598fe8616f5681fda4d21647550ad017dd5471578d95a9ea4e1b070103b39a729f16ab83526d8a698de7950555
|
data/CHANGELOG.md
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
## Unreleased
|
2
2
|
|
3
3
|
|
4
|
-
## 0.
|
4
|
+
## 0.2.0 (2018-09-27)
|
5
|
+
|
6
|
+
### Cbanges
|
7
|
+
|
8
|
+
* Add association name options
|
9
|
+
* Modify not to define unnecessary class methods for ActiveRecord model
|
10
|
+
|
11
|
+
### Misc
|
12
|
+
|
13
|
+
* Remove unecessary `require`
|
14
|
+
|
15
|
+
|
16
|
+
## 0.1.1 (2018-09-19)
|
5
17
|
|
6
18
|
### Misc
|
7
19
|
|
8
20
|
* Refactor internal structures
|
9
21
|
|
10
22
|
|
11
|
-
## 0.1.0 (2018
|
23
|
+
## 0.1.0 (2018-09-14)
|
12
24
|
|
13
25
|
* Initial release
|
data/README.md
CHANGED
@@ -38,6 +38,9 @@ OperatorRecordable.config = {
|
|
38
38
|
creator_column_name: "created_by",
|
39
39
|
updater_column_name: "updated_by",
|
40
40
|
deleter_column_name: "deleted_by",
|
41
|
+
creator_association_name: "creator",
|
42
|
+
updater_association_name: "updater",
|
43
|
+
deleter_association_name: "deleter",
|
41
44
|
operator_association_options: {},
|
42
45
|
operator_association_scope: nil,
|
43
46
|
store: :thread_store
|
@@ -52,6 +55,9 @@ OperatorRecordable.config = {
|
|
52
55
|
| `creator_column_name` | String | column name of creator. | `"created_by"` |
|
53
56
|
| `updater_column_name` | String | column name of updater. | `"updated_by"` |
|
54
57
|
| `deleter_column_name` | String | column name of deleter. | `"deleted_by"` |
|
58
|
+
| `creator_association_name` | String | association name of creator. | `"creator"` |
|
59
|
+
| `updater_association_name` | String | association name of updater. | `"updater"` |
|
60
|
+
| `deleter_association_name` | String | association name of deleter. | `"deleter"` |
|
55
61
|
| `operator_association_options` | Hash | options of operator associations. e.g. `{ optional: true }` | `{}` |
|
56
62
|
| `operator_association_scope` | Proc | The scope of operator associations. e.g. `-> { with_deleted }` | `nil` |
|
57
63
|
| `store` | Enum | operator store. any value of `:thread_store`, `:request_store` or `current_attributes_store` | `:thread_store` |
|
@@ -12,12 +12,21 @@ module OperatorRecordable
|
|
12
12
|
end
|
13
13
|
|
14
14
|
%i[operator_class_name creator_column_name updater_column_name deleter_column_name
|
15
|
+
creator_association_name updater_association_name deleter_association_name
|
15
16
|
operator_association_options operator_association_scope].each do |name|
|
16
17
|
define_method name do
|
17
18
|
config[name]
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
22
|
+
def column_name_for(type)
|
23
|
+
config[:"#{type}_column_name"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def association_name_for(type)
|
27
|
+
config[:"#{type}_association_name"]
|
28
|
+
end
|
29
|
+
|
21
30
|
private
|
22
31
|
|
23
32
|
attr_reader :config
|
@@ -28,6 +37,9 @@ module OperatorRecordable
|
|
28
37
|
creator_column_name: "created_by",
|
29
38
|
updater_column_name: "updated_by",
|
30
39
|
deleter_column_name: "deleted_by",
|
40
|
+
creator_association_name: "creator",
|
41
|
+
updater_association_name: "updater",
|
42
|
+
deleter_association_name: "deleter",
|
31
43
|
operator_association_options: {},
|
32
44
|
operator_association_scope: nil,
|
33
45
|
store: :thread_store
|
@@ -6,7 +6,6 @@ module OperatorRecordable
|
|
6
6
|
class Recorder < ::Module
|
7
7
|
def initialize(config)
|
8
8
|
define_activate_method(config)
|
9
|
-
define_predicate_methods
|
10
9
|
end
|
11
10
|
|
12
11
|
private
|
@@ -15,55 +14,50 @@ module OperatorRecordable
|
|
15
14
|
m = self
|
16
15
|
|
17
16
|
define_method :record_operator_on do |*actions|
|
18
|
-
|
17
|
+
c = Configuration::Model.new(actions)
|
19
18
|
|
20
|
-
if record_creator?
|
19
|
+
if c.record_creator?
|
21
20
|
m.__send__(:run_creator_dsl, self, config)
|
22
21
|
m.__send__(:define_creator_instance_methods, self, config)
|
23
22
|
end
|
24
23
|
|
25
|
-
if record_updater?
|
24
|
+
if c.record_updater?
|
26
25
|
m.__send__(:run_updater_dsl, self, config)
|
27
26
|
m.__send__(:define_updater_instance_methods, self, config)
|
28
27
|
end
|
29
28
|
|
30
|
-
if record_deleter?
|
29
|
+
if c.record_deleter?
|
31
30
|
m.__send__(:run_deleter_dsl, self, config)
|
32
31
|
m.__send__(:define_deleter_instance_methods, self, config)
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
def run_creator_dsl(
|
38
|
-
|
39
|
-
|
40
|
-
belongs_to :creator, config.operator_association_scope,
|
41
|
-
{ foreign_key: config.creator_column_name,
|
42
|
-
class_name: config.operator_class_name }.merge(config.operator_association_options)
|
43
|
-
end
|
36
|
+
def run_creator_dsl(klass, config)
|
37
|
+
klass.before_create :"assign_#{config.creator_association_name}"
|
38
|
+
run_add_association_dsl(:creator, klass, config)
|
44
39
|
end
|
45
40
|
|
46
|
-
def run_updater_dsl(
|
47
|
-
|
48
|
-
|
49
|
-
belongs_to :updater, config.operator_association_scope,
|
50
|
-
{ foreign_key: config.updater_column_name,
|
51
|
-
class_name: config.operator_class_name }.merge(config.operator_association_options)
|
52
|
-
end
|
41
|
+
def run_updater_dsl(klass, config)
|
42
|
+
klass.before_save :"assign_#{config.updater_association_name}"
|
43
|
+
run_add_association_dsl(:updater, klass, config)
|
53
44
|
end
|
54
45
|
|
55
|
-
def run_deleter_dsl(
|
56
|
-
|
57
|
-
|
58
|
-
belongs_to :deleter, config.operator_association_scope,
|
59
|
-
{ foreign_key: config.deleter_column_name,
|
60
|
-
class_name: config.operator_class_name }.merge(config.operator_association_options)
|
61
|
-
end
|
46
|
+
def run_deleter_dsl(klass, config)
|
47
|
+
klass.before_destroy :"assign_#{config.deleter_association_name}"
|
48
|
+
run_add_association_dsl(:deleter, klass, config)
|
62
49
|
end
|
63
50
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
51
|
+
def run_add_association_dsl(type, klass, config)
|
52
|
+
klass.belongs_to config.association_name_for(type).to_sym,
|
53
|
+
config.operator_association_scope,
|
54
|
+
{ foreign_key: config.column_name_for(type),
|
55
|
+
class_name: config.operator_class_name }.merge(config.operator_association_options)
|
56
|
+
end
|
57
|
+
|
58
|
+
def define_creator_instance_methods(klass, config)
|
59
|
+
klass.class_eval <<~END_OF_DEF, __FILE__, __LINE__ + 1
|
60
|
+
private def assign_#{config.creator_association_name}
|
67
61
|
return unless (op = OperatorRecordable.operator)
|
68
62
|
|
69
63
|
self.#{config.creator_column_name} = op.id
|
@@ -71,9 +65,9 @@ module OperatorRecordable
|
|
71
65
|
END_OF_DEF
|
72
66
|
end
|
73
67
|
|
74
|
-
def define_updater_instance_methods(
|
75
|
-
|
76
|
-
private def
|
68
|
+
def define_updater_instance_methods(klass, config)
|
69
|
+
klass.class_eval <<~END_OF_DEF, __FILE__, __LINE__ + 1
|
70
|
+
private def assign_#{config.updater_association_name}
|
77
71
|
return if !self.new_record? && !self.changed?
|
78
72
|
return unless (op = OperatorRecordable.operator)
|
79
73
|
|
@@ -82,9 +76,9 @@ module OperatorRecordable
|
|
82
76
|
END_OF_DEF
|
83
77
|
end
|
84
78
|
|
85
|
-
def define_deleter_instance_methods(
|
86
|
-
|
87
|
-
private def
|
79
|
+
def define_deleter_instance_methods(klass, config)
|
80
|
+
klass.class_eval <<~END_OF_DEF, __FILE__, __LINE__ + 1
|
81
|
+
private def assign_#{config.deleter_association_name}
|
88
82
|
return if self.frozen?
|
89
83
|
return unless (op = OperatorRecordable.operator)
|
90
84
|
|
@@ -96,25 +90,5 @@ module OperatorRecordable
|
|
96
90
|
end
|
97
91
|
END_OF_DEF
|
98
92
|
end
|
99
|
-
|
100
|
-
def define_predicate_methods
|
101
|
-
define_method :record_creator? do
|
102
|
-
instance_variable_defined?(:@_record_operator_on) &&
|
103
|
-
@_record_operator_on.record_creator?
|
104
|
-
end
|
105
|
-
private :record_creator?
|
106
|
-
|
107
|
-
define_method :record_updater? do
|
108
|
-
instance_variable_defined?(:@_record_operator_on) &&
|
109
|
-
@_record_operator_on.record_updater?
|
110
|
-
end
|
111
|
-
private :record_updater?
|
112
|
-
|
113
|
-
define_method :record_deleter? do
|
114
|
-
instance_variable_defined?(:@_record_operator_on) &&
|
115
|
-
@_record_operator_on.record_deleter?
|
116
|
-
end
|
117
|
-
private :record_deleter?
|
118
|
-
end
|
119
93
|
end
|
120
94
|
end
|
data/lib/operator_recordable.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: operator_recordable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuji Hanamura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|