Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Persistence#bitemporal_at and ActiveRecord::Bitemporal.bitemporal_at #152

Merged
merged 2 commits into from
Jan 31, 2024

Conversation

wata727
Copy link
Contributor

@wata727 wata727 commented Jan 26, 2024

This PR introduces Persistence#bitemporal_at and ActiveRecord::Bitemporal.bitemporal_at, which help you set a valid time and a transaction time at the same time.

This is inspired by the already existing bitemporal_at scope and improves the consistency of the interface:

users = User.bitemporal_at("2024/01/01")
# => SELECT "users".*
#      FROM "users"
#     WHERE "users"."transaction_from" <= '2024-01-01 00:00:00'
#       AND "users"."transaction_to"   >  '2024-01-01 00:00:00'
#       AND "users"."valid_from"       <= '2024-01-01 00:00:00'
#       AND "users"."valid_to"         >  '2024-01-01 00:00:00'

user = users.first
user.bitemporal_at("2024/01/02") { _1.reload }
# => SELECT "users".*
#      FROM "users"
#     WHERE "users"."transaction_from" <= '2024-01-02 00:00:00'
#       AND "users"."transaction_to"   >  '2024-01-02 00:00:00'
#       AND "users"."valid_from"       <= '2024-01-02 00:00:00'
#       AND "users"."valid_to"         >  '2024-01-02 00:00:00'

ActiveRecord::Bitemporal.bitemporal_at("2024/01/03") { User.all }
# => SELECT "users".*
#      FROM "users"
#     WHERE "users"."transaction_from" <= '2024-01-03 00:00:00'
#       AND "users"."transaction_to"   >  '2024-01-03 00:00:00'
#       AND "users"."valid_from"       <= '2024-01-03 00:00:00'
#       AND "users"."valid_to"         >  '2024-01-03 00:00:00'

@wata727 wata727 marked this pull request as ready for review January 30, 2024 11:28
@auto-assign auto-assign bot requested review from lighty and QWYNG January 30, 2024 11:28
@wata727 wata727 requested review from krororo and Dooor and removed request for lighty and QWYNG January 30, 2024 11:28
Copy link
Contributor

@Dooor Dooor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

let!(:company) { Company.create(name: "Company") }
before do
emp = company.employees.create(name: "Jane")
emp.address = Address.create(name: "Address")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use create_address ?
In the current code, the address.employee_id is updated after the Address is created.

Suggested change
emp.address = Address.create(name: "Address")
emp.create_address(name: "Address")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 You're right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Co-authored-by: kitazawa <3143443+krororo@users.noreply.github.com>
Copy link
Contributor

@krororo krororo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@wata727 wata727 merged commit 497bb4f into kufu:master Jan 31, 2024
10 of 14 checks passed
@wata727 wata727 deleted the more_bitemporal_at branch January 31, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants