Module: TemporalTables::RelationExtensions
- Defined in:
- lib/temporal_tables/relation_extensions.rb
Overview
Stores the time from the “at” field into each of the resulting objects so that it can be carried forward in subsequent queries.
Class Method Summary collapse
Instance Method Summary collapse
- #at(*args) ⇒ Object
- #at!(value) ⇒ Object
- #at_value ⇒ Object
- #at_value=(value) ⇒ Object
-
#default_value_for(name) ⇒ Object
Only needed for Rails 5.1.x.
- #exec_queries ⇒ Object
- #historical? ⇒ Boolean
- #limited_ids_for(*args) ⇒ Object
- #threadify_at ⇒ Object
- #to_sql(*args) ⇒ Object
Class Method Details
.included(base) ⇒ Object
7 8 9 10 11 |
# File 'lib/temporal_tables/relation_extensions.rb', line 7 def self.included(base) base.class_eval do ActiveRecord::Relation::SINGLE_VALUE_METHODS << :at end end |
Instance Method Details
#at(*args) ⇒ Object
21 22 23 |
# File 'lib/temporal_tables/relation_extensions.rb', line 21 def at(*args) spawn.at!(*args) end |
#at!(value) ⇒ Object
25 26 27 28 |
# File 'lib/temporal_tables/relation_extensions.rb', line 25 def at!(value) self.at_value = value where!(klass.build_temporal_constraint(value)) end |
#at_value ⇒ Object
13 14 15 |
# File 'lib/temporal_tables/relation_extensions.rb', line 13 def at_value @values.fetch(:at, nil) || Thread.current[:at_time] end |
#at_value=(value) ⇒ Object
17 18 19 |
# File 'lib/temporal_tables/relation_extensions.rb', line 17 def at_value=(value) @values[:at] = value end |
#default_value_for(name) ⇒ Object
Only needed for Rails 5.1.x
73 74 75 76 77 78 79 |
# File 'lib/temporal_tables/relation_extensions.rb', line 73 def default_value_for(name) if name == :at nil else super(name) end end |
#exec_queries ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/temporal_tables/relation_extensions.rb', line 51 def exec_queries # Note that record preloading, like when you specify # MyClass.includes(:associations) # happens within this exec_queries call. That's why we needed to # store the at_time in the thread above. records = threadify_at { super } if historical? # Store the at value on each record returned records.each do |r| r.at_value = at_value end end @records = records records end |
#historical? ⇒ Boolean
68 69 70 |
# File 'lib/temporal_tables/relation_extensions.rb', line 68 def historical? table_name =~ /_h$/i && at_value end |
#limited_ids_for(*args) ⇒ Object
47 48 49 |
# File 'lib/temporal_tables/relation_extensions.rb', line 47 def limited_ids_for(*args) threadify_at { super(*args) } end |
#threadify_at ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/temporal_tables/relation_extensions.rb', line 34 def threadify_at if at_value && !Thread.current[:at_time] begin Thread.current[:at_time] = at_value yield ensure Thread.current[:at_time] = nil end else yield end end |
#to_sql(*args) ⇒ Object
30 31 32 |
# File 'lib/temporal_tables/relation_extensions.rb', line 30 def to_sql(*args) threadify_at { super(*args) } end |