lib/active_median/model.rb in active_median-0.3.3 vs lib/active_median/model.rb in active_median-0.4.0

- old
+ new

@@ -9,11 +9,12 @@ end private def calculate_percentile(column, percentile, operation) - percentile = percentile.to_f + percentile = Float(percentile, exception: false) + raise ArgumentError, "invalid percentile" if percentile.nil? raise ArgumentError, "percentile is not between 0 and 1" if percentile < 0 || percentile > 1 # basic version of Active Record disallow_raw_sql! # symbol = column (safe), Arel node = SQL (safe), other = untrusted # matches table.column and column @@ -22,10 +23,16 @@ unless /\A\w+(\.\w+)?\z/i.match(column) raise ActiveRecord::UnknownAttributeReference, "Query method called with non-attribute argument(s): #{column.inspect}. Use Arel.sql() for known-safe values." end end - column_alias = relation.send(:column_alias_for, "#{operation} #{column.to_s.downcase}") + column_alias = + if relation.respond_to?(:column_alias_for, true) + relation.send(:column_alias_for, "#{operation} #{column.to_s.downcase}") + else + # Active Record 7.0.5+ + ActiveRecord::Calculations::ColumnAliasTracker.new(connection).alias_for("#{operation} #{column.to_s.downcase}") + end # safety check # could quote, but want to keep consistent with Active Record raise "Bad column alias: #{column_alias}. Please report a bug." unless column_alias =~ /\A[a-z0-9_]+\z/ # column resolution