weighted_average 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 2.0.2 / 2012-06-06
2
+
3
+ * Bug fixes
4
+
5
+ * Don't modify the Arel::SelectManager or ActiveRecord::Relation that #weighted_average is called on
6
+
1
7
  2.0.1 / 2012-06-06
2
8
 
3
9
  * Bug fixes
@@ -26,6 +26,10 @@ module WeightedAverage
26
26
  #
27
27
  # @return [Arel::SelectManager] A relation you can play around with.
28
28
  def weighted_average_relation(data_column_names, options = {})
29
+ unless options[:safe] == true
30
+ return clone.weighted_average_relation(data_column_names, options.merge(:safe => true))
31
+ end
32
+
29
33
  data_column_names = Array.wrap data_column_names
30
34
  left = self.source.left
31
35
 
@@ -48,7 +52,7 @@ module WeightedAverage
48
52
  left[data_column_name]
49
53
  end
50
54
 
51
- data_columns_sum = data_columns.inject(nil) do |memo, data_column|
55
+ data_columns_added_together = data_columns.inject(nil) do |memo, data_column|
52
56
  if memo
53
57
  Arel::Nodes::Addition.new(memo, data_column)
54
58
  else
@@ -57,13 +61,13 @@ module WeightedAverage
57
61
  end
58
62
 
59
63
  if data_column_names.many?
60
- data_columns_sum = Arel::Nodes::Grouping.new(data_columns_sum)
64
+ data_columns_added_together = Arel::Nodes::Grouping.new(data_columns_added_together)
61
65
  end
62
66
 
63
67
  if disaggregate_by_column
64
- self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_sum / disaggregate_by_column * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
68
+ self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_added_together / disaggregate_by_column * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
65
69
  else
66
- self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_sum * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
70
+ self.projections = [Arel::Nodes::Division.new(Arel::Nodes::Sum.new(weighted_by_column * data_columns_added_together * 1.0), Arel::Nodes::Sum.new([weighted_by_column]))]
67
71
  end
68
72
 
69
73
  data_columns_not_eq_nil = data_columns.inject(nil) do |memo, data_column|
@@ -1,3 +1,3 @@
1
1
  module WeightedAverage
2
- VERSION = '2.0.1'
2
+ VERSION = '2.0.2'
3
3
  end
@@ -44,6 +44,50 @@ describe WeightedAverage do
44
44
  # )
45
45
  # end
46
46
 
47
+ describe :weighted_average do
48
+ it "does not modify the Arel::SelectManager it is used on" do
49
+ s_t = Segment.arel_table
50
+ select_manager = s_t.where(s_t[:seats].eq(9))
51
+ original_constraints = select_manager.constraints
52
+ original_sql = select_manager.to_sql
53
+ select_manager.weighted_average(:seats, :weighted_by => :passengers)
54
+ select_manager.constraints.must_equal original_constraints
55
+ select_manager.to_sql.must_equal original_sql
56
+ end
57
+
58
+ it "does not modify the ActiveRecord::Relation it is used on" do
59
+ s_t = Segment.arel_table
60
+ relation = Segment.where(s_t[:seats].eq(9))
61
+ original_constraints = relation.constraints
62
+ original_sql = relation.to_sql
63
+ relation.weighted_average(:seats, :weighted_by => :passengers)
64
+ relation.constraints.must_equal original_constraints
65
+ relation.to_sql.must_equal original_sql
66
+ end
67
+ end
68
+
69
+ describe :weighted_average_relation do
70
+ it "does not modify the Arel::SelectManager it is used on" do
71
+ s_t = Segment.arel_table
72
+ select_manager = s_t.where(s_t[:seats].eq(9))
73
+ original_constraints = select_manager.constraints
74
+ original_sql = select_manager.to_sql
75
+ select_manager.weighted_average_relation(:seats, :weighted_by => :passengers)
76
+ select_manager.constraints.must_equal original_constraints
77
+ select_manager.to_sql.must_equal original_sql
78
+ end
79
+
80
+ it "does not modify the ActiveRecord::Relation it is used on" do
81
+ s_t = Segment.arel_table
82
+ relation = Segment.where(s_t[:seats].eq(9))
83
+ original_constraints = relation.constraints
84
+ original_sql = relation.to_sql
85
+ relation.weighted_average_relation(:seats, :weighted_by => :passengers)
86
+ relation.constraints.must_equal original_constraints
87
+ relation.to_sql.must_equal original_sql
88
+ end
89
+ end
90
+
47
91
  # plain
48
92
 
49
93
  it "does default weighting" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_average
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: