Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. def price_filter
  2. currency = self.currency || DEFAULTS[:currency]
  3.  
  4. # body = {}.tap do |h|
  5. # h.merge!(gte: Money.new(price_min, currency).exchange_to('TRY').cents) if price_min
  6. # h.merge!(lte: Money.new(price_max, currency).exchange_to('TRY').cents) if price_max
  7. # end
  8. # index.filter(range: { price_cents_try: body }) if body.present?
  9.  
  10. # TODO (Halil Özgür): Note: this method fails for currencies with subunit_to_unit != 100
  11. # rates = Money.default_bank.rates.values_at(*Model::CURRENCIES.map { |currency| "#{currency}_TO_TRY" })
  12. # Money.default_bank.rates
  13. eur_to_try = Money.default_bank.rates['EUR_TO_TRY'].to_f
  14. rates = {}
  15. Model::CURRENCIES.each do |currency|
  16. rates[currency] = eur_to_try / Money.default_bank.rates["EUR_TO_#{currency}"].to_f
  17. end
  18. script_rules = []
  19. params = {}
  20. if price_min
  21. script_rules << 'price_cents_try >= price_min'
  22. params[:price_min] = Money.new(price_min, currency).exchange_to('TRY').cents
  23. end
  24. if price_max
  25. script_rules << 'price_cents_try <= price_max'
  26. params[:price_max] = Money.new(price_max, currency).exchange_to('TRY').cents
  27. end
  28. if script_rules.present?
  29. index
  30. .filter.script_fields(
  31. price_cents_try: {
  32. params: { rates: rates },
  33. script: 'doc["price_cents"].value * rates[doc["price_currency"].value]'
  34. }
  35. )
  36. .filter { s(script_rules.join(' && '), params) }
  37. end
  38. endc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement