-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--readme README.md | ||
--title 'Temporality Documentation' | ||
--charset utf-8 | ||
--markup markdown | ||
--private | ||
'lib/**/*.rb' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,85 @@ | ||
module Temporality | ||
|
||
# | ||
# This module provides an override for the `build` method on an association | ||
# proxy in order to automatically set the temporal bounds on built children | ||
# unless these were explicitly provided. | ||
# | ||
module AssociationExtensions | ||
|
||
# | ||
# Override for `ActiveRecord::Relation#build` that provides automatic | ||
# values for the temporal bounds. | ||
# | ||
# @return [ActiveRecord::Base] The built model | ||
# | ||
def build(**kwargs, &block) | ||
with_default_bounds_scope(**kwargs) { super(**kwargs, &block) } | ||
end | ||
|
||
# | ||
# Override for `ActiveRecord::Relation#create` that provides automatic | ||
# values for the temporal bounds. | ||
# | ||
# @return [ActiveRecord::Base] The built model | ||
# | ||
def create(**kwargs, &block) | ||
with_default_bounds_scope(**kwargs) { super(**kwargs, &block) } | ||
end | ||
|
||
# | ||
# Override for `ActiveRecord::Relation#create!` that provides automatic | ||
# values for the temporal bounds. | ||
# | ||
# @return [ActiveRecord::Base] The built model | ||
# | ||
def create!(**kwargs, &block) | ||
with_default_bounds_scope(**kwargs) { super(**kwargs, &block) } | ||
end | ||
|
||
# | ||
# Yields to the given block with a scope defining default values for the | ||
# temporal bounds. | ||
# | ||
def with_default_bounds_scope(**kwargs) | ||
return unless block_given? | ||
|
||
owner = proxy_association.owner | ||
klass = proxy_association.klass | ||
|
||
starts_on = kwargs[:starts_on] | ||
ends_on = kwargs[:ends_on] | ||
|
||
if temporal_compatible_association?(owner, klass.new) | ||
last_child = scope.order('ends_on ASC').last | ||
|
||
ends_on ||= owner.ends_on | ||
|
||
if scope.count.zero? | ||
starts_on ||= owner.starts_on | ||
elsif last_child.ends_on < owner.ends_on | ||
starts_on ||= last_child.ends_on + 1 | ||
end | ||
end | ||
|
||
starts_on ||= Temporality::PAST_INFINITY | ||
ends_on ||= Temporality::FUTURE_INFINITY | ||
|
||
where(starts_on: starts_on).where(ends_on: ends_on).scoping { yield } | ||
end | ||
|
||
|
||
private | ||
|
||
# | ||
# Returns `true` if the association parent and children have temporal bounds | ||
# | ||
# @return [Boolean] | ||
# | ||
def temporal_compatible_association?(*models) | ||
models.all? { |m| m.respond_to?(:starts_on) && m.respond_to?(:ends_on) } | ||
end | ||
|
||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Temporality | ||
|
||
# Temporality gem version | ||
VERSION = '0.0.5' | ||
VERSION = '0.0.6' | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters