Module: ActiveRecord::Bitemporal::Relation

Extended by:
BitemporalIdAsPrimaryKey
Includes:
Finder
Defined in:
lib/activerecord-bitemporal/bitemporal.rb,
lib/activerecord-bitemporal/scope.rb

Defined Under Namespace

Modules: BitemporalIdAsPrimaryKey, Finder, MergeWithExceptBitemporalDefaultScope

Instance Method Summary collapse

Methods included from Finder

#find_at_time, #find_at_time!

Instance Method Details

#bitemporal_optionObject



133
134
135
# File 'lib/activerecord-bitemporal/scope.rb', line 133

def bitemporal_option
  ::ActiveRecord::Bitemporal.merge_by(bitemporal_value.merge bitemporal_clause)
end

#bitemporal_option_merge!(other) ⇒ Object



137
138
139
# File 'lib/activerecord-bitemporal/scope.rb', line 137

def bitemporal_option_merge!(other)
  self.bitemporal_value = bitemporal_value.merge other
end

#bitemporal_valueObject



141
142
143
# File 'lib/activerecord-bitemporal/scope.rb', line 141

def bitemporal_value
  @values[:bitemporal_value] ||= {}
end

#bitemporal_value=(value) ⇒ Object



145
146
147
# File 'lib/activerecord-bitemporal/scope.rb', line 145

def bitemporal_value=(value)
  @values[:bitemporal_value] = value
end

#build_arelObject



177
178
179
180
181
# File 'lib/activerecord-bitemporal/bitemporal.rb', line 177

def build_arel(*)
  ActiveRecord::Bitemporal.with_bitemporal_option(**bitemporal_option) {
    super
  }
end

#loadObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/activerecord-bitemporal/bitemporal.rb', line 183

def load
  return super if loaded?

  # このタイミングで先読みしているアソシエーションが読み込まれるので時間を固定
  records = ActiveRecord::Bitemporal.with_bitemporal_option(**bitemporal_option) { super }

  return records if records.empty?

  valid_datetime_ = valid_datetime
  if ActiveRecord::Bitemporal.valid_datetime.nil? && (bitemporal_value[:with_valid_datetime].nil? || bitemporal_value[:with_valid_datetime] == :default_scope || valid_datetime_.nil?)
    valid_datetime_ = nil
  end

  transaction_datetime_ = transaction_datetime
  if ActiveRecord::Bitemporal.transaction_datetime.nil? && (bitemporal_value[:with_transaction_datetime].nil? || bitemporal_value[:with_transaction_datetime] == :default_scope || transaction_datetime_.nil?)
    transaction_datetime_ = nil
  end

  return records if valid_datetime_.nil? && transaction_datetime_.nil?

  records.each do |record|
    record.send(:bitemporal_option_storage)[:valid_datetime] = valid_datetime_ if valid_datetime_
    record.send(:bitemporal_option_storage)[:transaction_datetime] = transaction_datetime_ if transaction_datetime_
  end
end

#primary_keyObject



212
213
214
# File 'lib/activerecord-bitemporal/bitemporal.rb', line 212

def primary_key
  bitemporal_id_key
end

#transaction_datetimeObject



129
130
131
# File 'lib/activerecord-bitemporal/scope.rb', line 129

def transaction_datetime
  bitemporal_clause[:transaction_datetime]&.in_time_zone
end

#valid_dateObject



125
126
127
# File 'lib/activerecord-bitemporal/scope.rb', line 125

def valid_date
  valid_datetime&.to_date
end

#valid_datetimeObject



121
122
123
# File 'lib/activerecord-bitemporal/scope.rb', line 121

def valid_datetime
  bitemporal_clause[:valid_datetime]&.in_time_zone
end