Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def price_filter
- currency = self.currency || DEFAULTS[:currency]
- # body = {}.tap do |h|
- # h.merge!(gte: Money.new(price_min, currency).exchange_to('TRY').cents) if price_min
- # h.merge!(lte: Money.new(price_max, currency).exchange_to('TRY').cents) if price_max
- # end
- # index.filter(range: { price_cents_try: body }) if body.present?
- # TODO (Halil Özgür): Note: this method fails for currencies with subunit_to_unit != 100
- # rates = Money.default_bank.rates.values_at(*Model::CURRENCIES.map { |currency| "#{currency}_TO_TRY" })
- # Money.default_bank.rates
- eur_to_try = Money.default_bank.rates['EUR_TO_TRY'].to_f
- rates = {}
- Model::CURRENCIES.each do |currency|
- rates[currency] = eur_to_try / Money.default_bank.rates["EUR_TO_#{currency}"].to_f
- end
- script_rules = []
- params = {}
- if price_min
- script_rules << 'price_cents_try >= price_min'
- params[:price_min] = Money.new(price_min, currency).exchange_to('TRY').cents
- end
- if price_max
- script_rules << 'price_cents_try <= price_max'
- params[:price_max] = Money.new(price_max, currency).exchange_to('TRY').cents
- end
- if script_rules.present?
- index
- .filter.script_fields(
- price_cents_try: {
- params: { rates: rates },
- script: 'doc["price_cents"].value * rates[doc["price_currency"].value]'
- }
- )
- .filter { s(script_rules.join(' && '), params) }
- end
- endc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement