Module: ChronoModel::TimeMachine::HistoryModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/chrono_model/time_machine/history_model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#current_versionObject

Returns this history entry’s current record



193
194
195
# File 'lib/chrono_model/time_machine/history_model.rb', line 193

def current_version
  self.class.superclass.find(rid)
end

#firstObject

Returns the first history entry



181
182
183
# File 'lib/chrono_model/time_machine/history_model.rb', line 181

def first
  self.class.where(id: rid).chronological.first
end

#historical?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/chrono_model/time_machine/history_model.rb', line 148

def historical?
  true
end

#idObject

The history id is hid, but this cannot set as primary key or temporal assocations will break. Solutions are welcome.



126
127
128
# File 'lib/chrono_model/time_machine/history_model.rb', line 126

def id
  hid
end

#lastObject

Returns the last history entry



187
188
189
# File 'lib/chrono_model/time_machine/history_model.rb', line 187

def last
  self.class.where(id: rid).chronological.last
end

#predObject

Returns the previous history entry, or nil if this is the first one.



155
156
157
158
159
160
161
162
163
# File 'lib/chrono_model/time_machine/history_model.rb', line 155

def pred
  return if self.valid_from.nil?

  if self.class.timeline_associations.empty?
    self.class.where('id = ? AND upper(validity) = ?', rid, valid_from).first
  else
    super(id: rid, before: valid_from, table: self.class.superclass.quoted_table_name)
  end
end

#recordObject

:nodoc:



197
198
199
200
# File 'lib/chrono_model/time_machine/history_model.rb', line 197

def record #:nodoc:
  ActiveSupport::Deprecation.warn '.record is deprecated in favour of .current_version'
  self.current_version
end

#recorded_atObject



211
212
213
# File 'lib/chrono_model/time_machine/history_model.rb', line 211

def recorded_at
  ChronoModel::Conversions.string_to_utc_time attributes_before_type_cast['recorded_at']
end

#ridObject

Referenced record ID.



132
133
134
# File 'lib/chrono_model/time_machine/history_model.rb', line 132

def rid
  attributes[self.class.primary_key]
end

#saveObject



136
137
138
# File 'lib/chrono_model/time_machine/history_model.rb', line 136

def save(*)
  self.class.with_hid_pkey { super }
end

#save!Object



140
141
142
# File 'lib/chrono_model/time_machine/history_model.rb', line 140

def save!(*)
  self.class.with_hid_pkey { super }
end

#succObject Also known as: next

Returns the next history entry, or nil if this is the last one.



168
169
170
171
172
173
174
175
176
# File 'lib/chrono_model/time_machine/history_model.rb', line 168

def succ
  return if self.valid_to.nil?

  if self.class.timeline_associations.empty?
    self.class.where('id = ? AND lower(validity) = ?', rid, valid_to).first
  else
    super(id: rid, after: valid_to, table: self.class.superclass.quoted_table_name)
  end
end

#update_columnsObject



144
145
146
# File 'lib/chrono_model/time_machine/history_model.rb', line 144

def update_columns(*)
  self.class.with_hid_pkey { super }
end

#valid_fromObject



202
203
204
# File 'lib/chrono_model/time_machine/history_model.rb', line 202

def valid_from
  validity.first
end

#valid_toObject Also known as: as_of_time



206
207
208
# File 'lib/chrono_model/time_machine/history_model.rb', line 206

def valid_to
  validity.last
end