Module: ChronoModel::TimeMachine::HistoryModel::ClassMethods
Overview
Methods that make up the history interface of the companion History model, automatically built for each Model that includes TimeMachine
Instance Method Summary collapse
-
#as_of(time) ⇒ Object
Fetches as of
time
records. -
#at(time) ⇒ Object
Fetches history record at the given time.
- #find ⇒ Object
-
#history? ⇒ Boolean
To identify this class as the History subclass.
-
#of(object) ⇒ Object
Fetches the given
object
history, sorted by history record time by default. - #past ⇒ Object
- #relation ⇒ Object
-
#sorted ⇒ Object
Returns the history sorted by recorded_at.
-
#time_query(match, time, options = {}) ⇒ Object
In the History context, pre-fill the :on options with the validity interval.
- #virtual_table_at(time, table_name: nil) ⇒ Object
-
#with_hid_pkey ⇒ Object
HACK.
Methods included from Timeline
#has_timeline, #quoted_history_fields, #timeline, #timeline_associations, #timeline_associations_from
Instance Method Details
#as_of(time) ⇒ Object
Fetches as of time
records.
69 70 71 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 69 def as_of(time) superclass.from(virtual_table_at(time)).as_of_time!(time) end |
#at(time) ⇒ Object
Fetches history record at the given time
86 87 88 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 86 def at(time) time_query(:at, time).from(quoted_table_name).as_of_time!(time) end |
#find ⇒ Object
43 44 45 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 43 def find(*) with_hid_pkey { super } end |
#history? ⇒ Boolean
To identify this class as the History subclass
59 60 61 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 59 def history? true end |
#of(object) ⇒ Object
Fetches the given object
history, sorted by history record time by default. Always includes an “as_of_time” column that is either the upper bound of the validity range or now() if history validity is maximum.
101 102 103 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 101 def of(object) where(id: object) end |
#past ⇒ Object
54 55 56 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 54 def past time_query(:before, :now).where("NOT upper_inf(#{quoted_table_name}.validity)") end |
#relation ⇒ Object
63 64 65 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 63 def relation super.as_of_time!(Time.now) end |
#sorted ⇒ Object
Returns the history sorted by recorded_at
92 93 94 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 92 def sorted all.order(Arel.sql(%( #{quoted_table_name}."recorded_at" ASC, #{quoted_table_name}."hid" ASC ))) end |
#time_query(match, time, options = {}) ⇒ Object
In the History context, pre-fill the :on options with the validity interval.
49 50 51 52 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 49 def time_query(match, time, = {}) [:on] ||= :validity super end |
#virtual_table_at(time, table_name: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 73 def virtual_table_at(time, table_name: nil) virtual_name = if table_name connection.quote_table_name(table_name) else superclass.quoted_table_name end "(#{at(time).to_sql}) #{virtual_name}" end |
#with_hid_pkey ⇒ Object
HACK. find() and save() require the real history ID. So we are setting it now and ensuring to reset it to the original one after execution completes. FIXME
34 35 36 37 38 39 40 41 |
# File 'lib/chrono_model/time_machine/history_model.rb', line 34 def with_hid_pkey old = primary_key self.primary_key = :hid yield ensure self.primary_key = old end |