activerecord-bitemporal 1.1.0 → 2.0.0

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: 939636118c751abd0690d9956750820ffcbb970fef7e4c9b6ab29642d461bf79
4
- data.tar.gz: caf2c137271a7c635318384b42c771d215849fb1ece08de062a0170f014ad0b4
3
+ metadata.gz: 712d3f7bf1cb73ff98bbe7f5ca15f860c62096e419b89d44e6a419ba865eec38
4
+ data.tar.gz: 4071219bd39db37fc51dee4d1689f722c0a8c2ee6eb16120373d21ecf234def8
5
5
  SHA512:
6
- metadata.gz: '05108b579eb4585ef4835677902933167209be9a7ed13482122f088c5b5a85ea528260b74ed619f3fcbbbb0d2ae85f711a2c06803b2c60d8821b88b9528fa914'
7
- data.tar.gz: e38f7f394438418cbab0a9f237ef4352cd0b2e6e07dff1fbbd4be7cda37c318ff2ce85a2c332e92e015d47980b06e49e74ce76e3c0a4b232d25bb869a092b604
6
+ metadata.gz: fb0f10288f109171dd11aed5221fe0297224c6d798278ba60c18cad092cf4b757d2c5aff9463c9a9fafe0f1556f1d71d47d4d89d435f96f8debdda51883c0e42
7
+ data.tar.gz: 588eb1741174be2031b8e326fe34da93efebda700ad37bb26a260250a3e25d6b1ecb3ffc1964a7fb99c8ea4035d4a660e59ec617d5732320f944467a3d71b202
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Breaking Changed
6
+ - [[Proposal] Changed valid_in to exclude valid_from = to and valid_to = from. by osyo-manga · Pull Request #95](https://github.com/kufu/activerecord-bitemporal/pull/95)
7
+
8
+ ### Added
9
+
10
+ ### Changed
11
+ - [[Proposal] Add range argument to .valid_allin. by Dooor · Pull Request #98](https://github.com/kufu/activerecord-bitemporal/pull/98)
12
+
13
+ ### Deprecated
14
+
15
+ ### Removed
16
+
17
+ ### Fixed
18
+ - [Fix JOIN query does not have valid_from / valid_to when using .or. by osyo-manga · Pull Request #99](https://github.com/kufu/activerecord-bitemporal/pull/99)
19
+ - [Fix typo in README.md by Naoya9922 · Pull Request #101](https://github.com/kufu/activerecord-bitemporal/pull/101)
20
+
3
21
  ## 1.1.0
4
22
 
5
23
  ### Added
data/README.md CHANGED
@@ -195,7 +195,7 @@ end
195
195
  | `valid_from` | `datetime` | 有効時間の開始時刻 |
196
196
  | `valid_to` | `datetime` | 有効時間の終了時刻 |
197
197
  | `transaction_from` | `datetime` | システム時間の開始時刻 |
198
- | `transaction_to` | `datetime` | システム時間の終了終了 |
198
+ | `transaction_to` | `datetime` | システム時間の終了時刻 |
199
199
 
200
200
  また、モデルクラスでは `ActiveRecord::Bitemporal` を `include` をする必要があります。
201
201
 
@@ -47,7 +47,7 @@ module ActiveRecord::Bitemporal
47
47
  end
48
48
  }
49
49
 
50
- def each_operatable_node(nodes = predicates, &block)
50
+ def each_operatable_node_6_0(nodes = predicates, &block)
51
51
  if block
52
52
  each_operatable_node(nodes).each(&block)
53
53
  else
@@ -56,11 +56,6 @@ module ActiveRecord::Bitemporal
56
56
  case node
57
57
  when Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
58
58
  y << node if node && node.left.respond_to?(:relation)
59
- when Arel::Nodes::Or, Arel::Nodes::And
60
- if Gem::Version.new("6.1.0") <= ActiveRecord.version
61
- each_operatable_node(node.left) { |node| y << node }
62
- each_operatable_node(node.right) { |node| y << node }
63
- end
64
59
  when Arel::Nodes::Grouping
65
60
  each_operatable_node(node.expr) { |node| y << node }
66
61
  end
@@ -69,6 +64,36 @@ module ActiveRecord::Bitemporal
69
64
  end
70
65
  end
71
66
 
67
+ def each_operatable_node_6_1(nodes = predicates, &block)
68
+ if block
69
+ each_operatable_node_6_1(nodes).each(&block)
70
+ else
71
+ Enumerator.new { |y|
72
+ Array(nodes).each { |node|
73
+ case node
74
+ when Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
75
+ y << node if node && node.left.respond_to?(:relation)
76
+ when Arel::Nodes::And
77
+ each_operatable_node_6_1(node.children) { |node| y << node }
78
+ when Arel::Nodes::Binary
79
+ each_operatable_node_6_1(node.left) { |node| y << node }
80
+ each_operatable_node_6_1(node.right) { |node| y << node }
81
+ when Arel::Nodes::Unary
82
+ each_operatable_node_6_1(node.expr) { |node| y << node }
83
+ end
84
+ }
85
+ }
86
+ end
87
+ end
88
+
89
+ def each_operatable_node(nodes = predicates, &block)
90
+ if Gem::Version.new("6.1.0") <= ActiveRecord.version
91
+ each_operatable_node_6_1(nodes, &block)
92
+ else
93
+ each_operatable_node_6_0(nodes, &block)
94
+ end
95
+ end
96
+
72
97
  def bitemporal_query_hash(*names)
73
98
  each_operatable_node
74
99
  .select { |node| names.include? node.left.name.to_s }
@@ -466,15 +491,53 @@ module ActiveRecord::Bitemporal
466
491
  where(bitemporal_id: id)
467
492
  }
468
493
 
469
- scope :valid_in, -> (from: nil, to: nil) {
470
- ignore_valid_datetime
471
- .tap { |relation| break relation.bitemporal_where_bind("valid_to", :gteq, from.in_time_zone.to_datetime) if from }
472
- .tap { |relation| break relation.bitemporal_where_bind("valid_from", :lteq, to.in_time_zone.to_datetime) if to }
494
+ # from < valid_to AND valid_from < to
495
+ scope :valid_in, -> (range = nil, from: nil, to: nil) {
496
+ return valid_in(from...to) if range.nil?
497
+
498
+ relation = ignore_valid_datetime
499
+ begin_, end_ = range.begin, range.end
500
+
501
+ # beginless range
502
+ if begin_
503
+ # from < valid_to
504
+ relation = relation.bitemporal_where_bind("valid_to", :gt, begin_.in_time_zone.to_datetime)
505
+ end
506
+
507
+ # endless range
508
+ if end_
509
+ if range.exclude_end?
510
+ # valid_from < to
511
+ relation = relation.bitemporal_where_bind("valid_from", :lt, end_.in_time_zone.to_datetime)
512
+ else
513
+ # valid_from <= to
514
+ relation = relation.bitemporal_where_bind("valid_from", :lteq, end_.in_time_zone.to_datetime)
515
+ end
516
+ end
517
+
518
+ relation
473
519
  }
474
- scope :valid_allin, -> (from: nil, to: nil) {
475
- ignore_valid_datetime
476
- .tap { |relation| break relation.bitemporal_where_bind("valid_from", :gteq, from.in_time_zone.to_datetime) if from }
477
- .tap { |relation| break relation.bitemporal_where_bind("valid_to", :lteq, to.in_time_zone.to_datetime) if to }
520
+
521
+ # from <= valid_from AND valid_to <= to
522
+ scope :valid_allin, -> (range = nil, from: nil, to: nil) {
523
+ return valid_allin(from..to) if range.nil?
524
+
525
+ relation = ignore_valid_datetime
526
+ begin_, end_ = range.begin, range.end
527
+
528
+ if begin_
529
+ relation = relation.bitemporal_where_bind("valid_from", :gteq, begin_.in_time_zone.to_datetime)
530
+ end
531
+
532
+ if end_
533
+ if range.exclude_end?
534
+ raise 'Range with excluding end is not supported'
535
+ else
536
+ relation = relation.bitemporal_where_bind("valid_to", :lteq, end_.in_time_zone.to_datetime)
537
+ end
538
+ end
539
+
540
+ relation
478
541
  }
479
542
  end
480
543
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Bitemporal
5
- VERSION = "1.1.0"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bitemporal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SmartHR
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-14 00:00:00.000000000 Z
11
+ date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -188,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
188
  - !ruby/object:Gem::Version
189
189
  version: '0'
190
190
  requirements: []
191
- rubygems_version: 3.3.3
191
+ rubygems_version: 3.3.7
192
192
  signing_key:
193
193
  specification_version: 4
194
194
  summary: BiTemporal Data Model for ActiveRecord