activerecord-bitemporal-tablize 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f6945f01b5fba96240435a8cd218539cb58d071662dae1819de71a918fcc876
|
4
|
+
data.tar.gz: 41fe5ea0d74db317f120ae714f3f254683eb017269a26412eae8158a25ec0752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cec227269db0b579e91b3d0360c1a27afad3765cbdd35e8c7dbefe42fe45fa3a843044dd6ab83b9ebda897f766c182c4ae22bf02e69aeeceb3aa6529586597da
|
7
|
+
data.tar.gz: 2f05986586612f84aa48231c138cd2acee85ad17b3587d8fffa22f5777bb1f5b592af21fbdf85ea3794ceadc0cb588ea9a7933520f34bab87b6bd54040f31313
|
data/README.md
CHANGED
@@ -22,7 +22,48 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
ローカル環境でgemをインストールし、`rails console`した後でrequireする
|
26
|
+
ref: https://qiita.com/kyanny/items/d370efe14ef15d9afacb
|
27
|
+
|
28
|
+
```
|
29
|
+
[1] pry(main)> Employee.first.history.print_table
|
30
|
+
(0.8ms) SELECT sqlite_version(*)
|
31
|
+
Employee Load (0.3ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:21:42.229006"], ["valid_to", "2021-06-20 13:21:42.229006"], ["LIMIT", 1]]
|
32
|
+
Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
|
33
|
+
+------------+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
|
34
|
+
| | id | unit_id | name | joined_at | created_at | updated_at | bitemporal_id | valid_from | valid_to | deleted_at | transaction_from | transaction_to |
|
35
|
+
+-9999/12/31-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
|
36
|
+
| | 1 | 2 | 山田 | | 2021-06-20 01:44:43 UTC | 2021-06-20 01:44:43 UTC | 1 | 2021/04/01 | 9999/12/31 | | 2021-06-20 01:44:43 UTC | 9999-12-31 00:00:00 UTC |
|
37
|
+
+-2021/04/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
|
38
|
+
+-2021/04/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
|
39
|
+
| | 1 | 1 | たけし | | 2021-06-20 01:44:43 UTC | 2021-06-20 01:44:43 UTC | 1 | 2020/01/01 | 2021/04/01 | | 2021-06-20 01:44:43 UTC | 9999-12-31 00:00:00 UTC |
|
40
|
+
+-2020/01/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
|
41
|
+
=> nil
|
42
|
+
[2] pry(main)> Employee.first.history.print_table_diff_only
|
43
|
+
Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:21:44.302316"], ["valid_to", "2021-06-20 13:21:44.302316"], ["LIMIT", 1]]
|
44
|
+
Employee Load (0.1ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
|
45
|
+
+------------+---------+--------+-------------------------+------------+------------+
|
46
|
+
| | unit_id | name | updated_at | valid_from | valid_to |
|
47
|
+
+-9999/12/31-+---------+--------+-------------------------+------------+------------+
|
48
|
+
| | 2 | 山田 | 2021-06-20 01:44:43 UTC | 2021/04/01 | 9999/12/31 |
|
49
|
+
+-2021/04/01-+---------+--------+-------------------------+------------+------------+
|
50
|
+
+-2021/04/01-+---------+--------+-------------------------+------------+------------+
|
51
|
+
| | 1 | たけし | 2021-06-20 01:44:43 UTC | 2020/01/01 | 2021/04/01 |
|
52
|
+
+-2020/01/01-+---------+--------+-------------------------+------------+------------+
|
53
|
+
=> nil
|
54
|
+
[3] pry(main)> Employee.first.history.print_table(:name)
|
55
|
+
Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:22:54.269095"], ["valid_to", "2021-06-20 13:22:54.269095"], ["LIMIT", 1]]
|
56
|
+
Employee Load (0.1ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
|
57
|
+
+------------+--------+
|
58
|
+
| | name |
|
59
|
+
+-9999/12/31-+--------+
|
60
|
+
| | 山田 |
|
61
|
+
+-2021/04/01-+--------+
|
62
|
+
+-2021/04/01-+--------+
|
63
|
+
| | たけし |
|
64
|
+
+-2020/01/01-+--------+
|
65
|
+
=> nil
|
66
|
+
```
|
26
67
|
|
27
68
|
## Development
|
28
69
|
|
@@ -3,11 +3,17 @@ module ActiveRecord
|
|
3
3
|
module Bitemporalize
|
4
4
|
module InstanceMethods
|
5
5
|
def history
|
6
|
-
self.class.ignore_valid_datetime.bitemporal_for(bitemporal_id).order(valid_from: :desc)
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
records = self.class.ignore_valid_datetime.bitemporal_for(bitemporal_id).order(valid_from: :desc)
|
7
|
+
|
8
|
+
records.define_singleton_method(:print_table) do |*attributes|
|
9
|
+
print ActiveRecord::Bitemporal::Tablize.new(records: records, attributes: attributes).call
|
10
10
|
end
|
11
|
+
|
12
|
+
records.define_singleton_method(:print_table_diff_only) do |*attributes|
|
13
|
+
print ActiveRecord::Bitemporal::Tablize.new(records: records, attributes: attributes, diff_only: true).call
|
14
|
+
end
|
15
|
+
|
16
|
+
records
|
11
17
|
end
|
12
18
|
end
|
13
19
|
end
|
@@ -7,9 +7,10 @@ class ActiveRecord::Bitemporal::Tablize
|
|
7
7
|
|
8
8
|
attr :records, :result, :attributes
|
9
9
|
|
10
|
-
def initialize(records:, attributes: [])
|
10
|
+
def initialize(records:, attributes: [], diff_only: false)
|
11
11
|
@records = records.map { |record| RecordWrapper.new(record, self) }
|
12
|
-
|
12
|
+
attributes = attributes.empty? ? all_attributes : attributes
|
13
|
+
@attributes = diff_only ? select_having_diff : attributes
|
13
14
|
@result = []
|
14
15
|
end
|
15
16
|
|
@@ -34,6 +35,14 @@ class ActiveRecord::Bitemporal::Tablize
|
|
34
35
|
|
35
36
|
private
|
36
37
|
|
38
|
+
def all_attributes
|
39
|
+
records.first&.then { |r| r.attributes.keys.map(&:to_sym) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def select_having_diff
|
43
|
+
all_attributes&.select { |attribute| records.map(&:"#{attribute}").uniq.size != 1 }
|
44
|
+
end
|
45
|
+
|
37
46
|
def append_header
|
38
47
|
result << "+#{build_roof.join('+')}+"
|
39
48
|
result << "|#{build_wall.join('|')}|"
|
@@ -57,7 +66,7 @@ class ActiveRecord::Bitemporal::Tablize
|
|
57
66
|
records.each do |record|
|
58
67
|
result << "+#{record.build_roof.join('+')}+"
|
59
68
|
result << "|#{record.build_wall.join('|')}|"
|
60
|
-
result << "+#{record.
|
69
|
+
result << "+#{record.build_floor.join('+')}+"
|
61
70
|
end
|
62
71
|
end
|
63
72
|
|