lib/sequel/extensions/currency_rates.rb in umbrellio-sequel-plugins-0.3.0.62 vs lib/sequel/extensions/currency_rates.rb in umbrellio-sequel-plugins-0.3.0.67

- old
+ new

@@ -5,12 +5,13 @@ module CurrencyRates # Join a rates table # # @param aliaz [Symbol] alias to be used for joined table # @param table [Symbol] table name to join to - # @param currency_column [Symbol] currency column by which table is joined - # @param time_column [Symbol] time column by which table is joined + # @param currency_column [Symbol or Sequel::SQL::Expression] currency column by which + # table is joined + # @param time_column [Symbol or Sequel::SQL::Expression] time column by which table is joined # # @example # Order::Model.with_rates.select(Sequel[:amount].in_usd) # @return [Sequel::Dataset] dataset def with_rates( @@ -20,19 +21,28 @@ currency_column: :currency, time_column: :created_at ) table = Sequel[table] rates = Sequel[aliaz] - join_expr = table[currency_column] =~ rates[:currency] - join_expr &= rates[:period].pg_range.contains(table[time_column]) + + currency_expr = wrap_if_symbol(currency_column, table) + time_expr = wrap_if_symbol(time_column, table) + + join_expr = (rates[:currency] =~ currency_expr) & rates[:period].pg_range.contains(time_expr) left_join(rates_table.as(aliaz), join_expr) end # Returns a table name # # @return [Symbol] table name def table_name respond_to?(:first_source_alias) ? first_source_alias : super + end + + private + + def wrap_if_symbol(column_name, table) + column_name.is_a?(Symbol) ? table[column_name] : column_name end end module CurrencyRateExchange # Exchange column value to a specific currency