Module: ActiveRecord::Bitemporal::Relation

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

Defined Under Namespace

Modules: Finder, MergeWithExceptBitemporalDefaultScope Classes: WhereClauseWithCheckTable

Instance Method Summary collapse

Methods included from Finder

#find, #find_at_time, #find_at_time!

Instance Method Details

#bitemporal_optionObject



173
174
175
# File 'lib/activerecord-bitemporal/scope.rb', line 173

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

#bitemporal_option_merge!(other) ⇒ Object



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

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

#bitemporal_valueObject



181
182
183
# File 'lib/activerecord-bitemporal/scope.rb', line 181

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

#bitemporal_value=(value) ⇒ Object



185
186
187
# File 'lib/activerecord-bitemporal/scope.rb', line 185

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

#build_arelObject



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

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

#loadObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/activerecord-bitemporal/bitemporal.rb', line 145

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



171
172
173
# File 'lib/activerecord-bitemporal/bitemporal.rb', line 171

def primary_key
  bitemporal_id_key
end

#transaction_datetimeObject



169
170
171
# File 'lib/activerecord-bitemporal/scope.rb', line 169

def transaction_datetime
  bitemporal_clause[:transaction_datetime]&.in_time_zone
end

#valid_datetimeObject



165
166
167
# File 'lib/activerecord-bitemporal/scope.rb', line 165

def valid_datetime
  bitemporal_clause[:valid_datetime]&.in_time_zone
end

#where_clauseObject



149
150
151
# File 'lib/activerecord-bitemporal/scope.rb', line 149

def where_clause
  WhereClauseWithCheckTable.new(super.send(:predicates))
end