Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move calculated records method to ranking, allow them to share method…

…s with the existing ranking scopes
  • Loading branch information...
commit c5727f9cb8922be7207c3df8380e74747a6065ec 1 parent e36c9bb
@mjankowski mjankowski authored
View
22 lib/pacecar/associations.rb
@@ -6,17 +6,6 @@ def self.included(base)
module ClassMethods
- def has_calculated_records(*names)
- opts = names.extract_options!
- names.each do |association_name|
- *columns = opts[:on] || []
- columns.flatten.each do |column|
- define_calculated_scope association_name, column, :average, :avg
- define_calculated_scope association_name, column, :total, :sum
- end
- end
- end
-
def has_recent_records(*names)
names.each do |name|
scope "recent_#{name}_since".to_sym, lambda { |since|
@@ -45,17 +34,6 @@ def conditions_for_name(name)
"((select count(*) from #{connection.quote_table_name(name)} where #{connection.quote_table_name(name)}.#{connection.quote_column_name reflections[name].primary_key_name} = #{quoted_table_name}.#{connection.quote_column_name primary_key} and #{connection.quote_table_name(name)}.#{connection.quote_column_name("created_at")} > :since_time) > 0)"
end
- def define_calculated_scope(association_name, column, function_name, function_method)
- { 'highest' => 'desc', 'lowest' => 'asc' }.each do |direction_name, direction|
- scope "by_#{association_name}_#{direction_name}_#{column}_#{function_name}".to_sym, {
- :select => "#{quoted_table_name}.*, #{function_method}(#{connection.quote_table_name(association_name)}.#{connection.quote_column_name column}) as #{association_name}_#{column}_#{function_name}",
- :joins => "inner join #{connection.quote_table_name(association_name)} on #{connection.quote_table_name(association_name)}.#{connection.quote_column_name reflections[association_name].primary_key_name} = #{quoted_table_name}.#{connection.quote_column_name primary_key}",
- :group => safe_column_names.collect { |column_name| "#{quoted_table_name}.#{connection.quote_column_name(column_name)}" }.join(', '),
- :order => "#{association_name}_#{column}_#{function_name} #{direction}"
- }
- end
- end
-
end
end
end
View
39 lib/pacecar/ranking.rb
@@ -11,14 +11,41 @@ def has_ranking(association)
define_ranking_scope association, :minimum, :asc
end
+ def has_calculated_records(*names)
+ opts = names.extract_options!
+ names.each do |association_name|
+ *columns = opts[:on] || []
+ columns.flatten.each do |column|
+ define_calculated_scope association_name, column, :average, :avg
+ define_calculated_scope association_name, column, :total, :sum
+ define_calculated_scope association_name, column, :count, :count
+ end
+ end
+ end
+
protected
- def define_ranking_scope(association, name, direction)
- scope "#{name}_#{association}",
- :select => "#{quoted_table_name}.*, count(#{reflections[association].quoted_table_name}.#{connection.quote_column_name reflections[association].primary_key_name}) as #{association}_count",
- :joins => "inner join #{association} on #{association}.#{reflections[association].primary_key_name} = #{quoted_table_name}.#{connection.quote_column_name primary_key}",
- :group => safe_column_names.collect { |column_name| "#{quoted_table_name}.#{connection.quote_column_name(column_name)}" }.join(', '),
- :order => "#{association}_count #{direction}"
+ def define_ranking_scope(association, direction_name, direction)
+ scope "#{direction_name}_#{association}", {
+ :select => "#{quoted_table_name}.*, count(#{reflections[association].quoted_table_name}.#{connection.quote_column_name reflections[association].primary_key_name}) as #{association}_count",
+ :order => "#{association}_count #{direction}"
+ }.merge(association_group_and_join(association))
+ end
+
+ def define_calculated_scope(association_name, column, function_name, function_method)
+ { 'highest' => 'desc', 'lowest' => 'asc' }.each do |direction_name, direction|
+ scope "by_#{association_name}_#{direction_name}_#{column}_#{function_name}".to_sym, {
+ :select => "#{quoted_table_name}.*, #{function_method}(#{connection.quote_table_name(association_name)}.#{connection.quote_column_name column}) as #{association_name}_#{column}_#{function_name}",
+ :order => "#{association_name}_#{column}_#{function_name} #{direction}"
+ }.merge(association_group_and_join(association_name))
+ end
+ end
+
+ def association_group_and_join(association_name)
+ {
+ :joins => "inner join #{connection.quote_table_name(association_name)} on #{connection.quote_table_name(association_name)}.#{connection.quote_column_name reflections[association_name].primary_key_name} = #{quoted_table_name}.#{connection.quote_column_name primary_key}",
+ :group => safe_column_names.collect { |column_name| "#{quoted_table_name}.#{connection.quote_column_name(column_name)}" }.join(', ')
+ }
end
end
View
36 spec/associations_spec.rb
@@ -30,39 +30,3 @@
end
end
-
-describe 'Associations', 'has calculated records' do
- before do
- @user_one = Factory :user
- @user_two = Factory :user
- Factory :comment, :user => @user_one, :rating => 8
- Factory :comment, :user => @user_one, :rating => 6
- Factory :comment, :user => @user_two, :rating => 4
- Factory :comment, :user => @user_two, :rating => 3
- Factory :comment, :user => @user_two, :rating => 2
- end
- it "should order records based on association column highest average" do
- output = User.by_comments_highest_rating_average
- output.should == [@user_one, @user_two]
- output.first.comments_rating_average.to_i.should == 7
- output.last.comments_rating_average.to_i.should == 3
- end
- it "should order records based on association column lowest average" do
- output = User.by_comments_lowest_rating_average
- output.should == [@user_two, @user_one]
- output.first.comments_rating_average.to_i.should == 3
- output.last.comments_rating_average.to_i.should == 7
- end
- it "should order records based on association column highest total" do
- output = User.by_comments_highest_rating_total
- output.should == [@user_one, @user_two]
- output.first.comments_rating_total.to_i.should == 14
- output.last.comments_rating_total.to_i.should == 9
- end
- it "should order records based on association column lowest total" do
- output = User.by_comments_lowest_rating_total
- output.should == [@user_two, @user_one]
- output.first.comments_rating_total.to_i.should == 9
- output.last.comments_rating_total.to_i.should == 14
- end
-end
View
38 spec/ranking_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe 'Ranking' do
+describe 'Ranking', 'has ranking' do
before do
@many = Factory :user
@@ -25,3 +25,39 @@
end
end
+
+describe 'Ranking', 'has calculated records' do
+ before do
+ @user_one = Factory :user
+ @user_two = Factory :user
+ Factory :comment, :user => @user_one, :rating => 8
+ Factory :comment, :user => @user_one, :rating => 6
+ Factory :comment, :user => @user_two, :rating => 4
+ Factory :comment, :user => @user_two, :rating => 3
+ Factory :comment, :user => @user_two, :rating => 2
+ end
+ it "should order records based on association column highest average" do
+ output = User.by_comments_highest_rating_average
+ output.should == [@user_one, @user_two]
+ output.first.comments_rating_average.to_i.should == 7
+ output.last.comments_rating_average.to_i.should == 3
+ end
+ it "should order records based on association column lowest average" do
+ output = User.by_comments_lowest_rating_average
+ output.should == [@user_two, @user_one]
+ output.first.comments_rating_average.to_i.should == 3
+ output.last.comments_rating_average.to_i.should == 7
+ end
+ it "should order records based on association column highest total" do
+ output = User.by_comments_highest_rating_total
+ output.should == [@user_one, @user_two]
+ output.first.comments_rating_total.to_i.should == 14
+ output.last.comments_rating_total.to_i.should == 9
+ end
+ it "should order records based on association column lowest total" do
+ output = User.by_comments_lowest_rating_total
+ output.should == [@user_two, @user_one]
+ output.first.comments_rating_total.to_i.should == 9
+ output.last.comments_rating_total.to_i.should == 14
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.