Module: WeightedAverage::ActiveRecordRelationInstanceMethods
- Defined in:
- lib/weighted_average/active_record_relation_instance_methods.rb
Instance Method Summary collapse
-
#weighted_average(data_column_names, options = {}) ⇒ Float?
Get the weighted average of column(s).
-
#weighted_average_relation(data_column_names, options = {}) ⇒ Arel::SelectManager
Same as WeightedAverage::ArelSelectManagerInstanceMethods#weighted_average, except it can interpret associations.
Instance Method Details
#weighted_average(data_column_names, options = {}) ⇒ Float?
Get the weighted average of column(s).
In addition to the options available on WeightedAverage::ArelSelectManagerInstanceMethods#weighted_average, this ActiveRecord-specific method understands associations.
23 24 25 26 |
# File 'lib/weighted_average/active_record_relation_instance_methods.rb', line 23 def weighted_average(data_column_names, = {}) weighted_average = connection.select_value weighted_average_relation(data_column_names, ).to_sql weighted_average.nil? ? nil : weighted_average.to_f end |
#weighted_average_relation(data_column_names, options = {}) ⇒ Arel::SelectManager
Same as WeightedAverage::ArelSelectManagerInstanceMethods#weighted_average, except it can interpret associations.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/weighted_average/active_record_relation_instance_methods.rb', line 33 def weighted_average_relation(data_column_names, = {}) if weighted_by_option = [:weighted_by] case weighted_by_option when Array # :weighted_by specifies a custom column on an association table (least common) unless association = reflect_on_association(weighted_by_option.first) raise ArgumentError, "#{name} does not have association #{weighted_by_option.first.inspect}" end weighted_by_column = association.klass.arel_table[weighted_by_option.last] when Symbol, String if association = reflect_on_association(weighted_by_option) # :weighted_by specifies an association table with a column named "weighting" weighted_by_column = association.klass.arel_table[DEFAULT_WEIGHTED_BY_COLUMN_NAME] else # :weighted_by specifies a custom column on the same table weighted_by_column = arel_table[weighted_by_option] end end if association joins(association.name).arel.weighted_average_relation data_column_names, .merge(:weighted_by => weighted_by_column) else arel.weighted_average_relation data_column_names, .merge(:weighted_by => weighted_by_column) end else arel.weighted_average_relation data_column_names, end end |