Class: Gera::CurrencyRateHistoryInterval

Inherits:
ApplicationRecord show all
Includes:
HistoryIntervalConcern
Defined in:
app/models/gera/currency_rate_history_interval.rb

Constant Summary

Constants included from HistoryIntervalConcern

HistoryIntervalConcern::INTERVAL

Class Method Summary collapse

Class Method Details

.create_by_interval!(interval_from, interval_to = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/gera/currency_rate_history_interval.rb', line 6

def self.create_by_interval!(interval_from, interval_to = nil)
  interval_to ||= interval_from + INTERVAL
  CurrencyRate
    .where('created_at >= ? and created_at < ?', interval_from, interval_to)
    .group(:cur_from, :cur_to)
    .pluck(:cur_from, :cur_to, 'min(rate_value)', 'max(rate_value)')
    .each do |cur_from, cur_to, min_rate, max_rate|

    next if cur_from == cur_to

    create!(
      cur_from_id: Money::Currency.find(cur_from).local_id,
      cur_to_id: Money::Currency.find(cur_to).local_id,
      min_rate: min_rate, max_rate: max_rate,
      interval_from: interval_from, interval_to: interval_to
    )
  end
end