Permalink
Browse files

Add support for normalized reputation Arel query

  • Loading branch information...
1 parent b8514b5 commit e111f5f346a64903b201612c7f6d22ff280b4f76 Katsuya Noguchi committed Aug 26, 2012
Showing with 83 additions and 11 deletions.
  1. +4 −0 CHANGELOG.md
  2. +14 −0 lib/reputation_system/query_methods.rb
  3. +65 −11 spec/reputation_system/query_methods_spec.rb
View
@@ -1,3 +1,7 @@
+* Add `with_normalized_reputation` and `with_normalized_reputation_only`
+
+* Add `with_reputation` and `with_reputation_only` methods.
+
## ActiveRecordReputationSystem 1.3.4 ##
* Fix name of a migration class again.
@@ -35,6 +35,20 @@ def with_reputation_only(*args)
self.select(select).joins(joins)
end
+ def with_normalized_reputation(*args)
+ reputation_name, srn = parse_arel_query_args(args)
+ select = build_select_statement(table_name, reputation_name, nil, srn, true)
+ joins = build_join_statement(table_name, name, srn)
+ self.select(select).joins(joins)
+ end
+
+ def with_normalized_reputation_only(*args)
+ reputation_name, srn = parse_arel_query_args(args)
+ select = build_select_statement_with_reputation_only(table_name, reputation_name, srn, true)
+ joins = build_join_statement(table_name, name, srn)
+ self.select(select).joins(joins)
+ end
+
protected
def parse_arel_query_args(args)
@@ -126,7 +126,7 @@
end
end
end
-=begin
+
describe "#with_normalized_reputation" do
context "Without Scopes" do
before :each do
@@ -136,30 +136,30 @@
it "should return result with given normalized reputation" do
@question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
@question2.add_evaluation(:total_votes, 6, @user)
- res = Question.with_normalized_reputation(:total_votes, :all, {})
+ res = Question.with_normalized_reputation(:total_votes)
res.should == [@question, @question2]
res[0].normalized_total_votes.should be_within(DELTA).of(0)
res[1].normalized_total_votes.should be_within(DELTA).of(1)
end
- it "should retain select option" do
- res = Question.with_normalized_reputation(:total_votes, :all, {:select => "questions.id"})
+ it "should not retain select option" do
+ res = Question.with_normalized_reputation(:total_votes).select("questions.id")
res.should == [@question]
res[0].id.should_not be_nil
- lambda {res[0].text}.should raise_error
+ lambda {res[0].text}.should_not raise_error
end
it "should retain conditions option" do
@question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
@question2.add_evaluation(:total_votes, 6, @user)
- res = Question.with_normalized_reputation(:total_votes, :all, {:conditions => "normalized_total_votes > 0.6"})
+ res = Question.with_normalized_reputation(:total_votes).where("normalized_total_votes > 0.6")
res.should == [@question2]
end
it "should retain joins option" do
- res = Question.with_normalized_reputation(:total_votes, :all, {
- :select => "questions.*, users.name AS user_name",
- :joins => "JOIN users ON questions.author_id = users.id"})
+ res = Question.with_normalized_reputation(:total_votes).
+ select("questions.*, users.name AS user_name").
+ joins("JOIN users ON questions.author_id = users.id")
res.should == [@question]
res[0].user_name.should == @user.name
end
@@ -174,11 +174,65 @@
end
it "should return result with given reputation" do
- res = Phrase.with_normalized_reputation(:maturity, :ja, :all, {})
+ res = Phrase.with_normalized_reputation(:maturity, :ja)
res.should == [@phrase]
res[0].normalized_maturity.should be_within(DELTA).of(0)
end
end
end
-=end
+
+ describe "#with_normalized_reputation_only" do
+ context "Without Scopes" do
+ before :each do
+ @question.add_evaluation(:total_votes, 3, @user)
+ end
+
+ it "should return result with given normalized reputation" do
+ @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
+ @question2.add_evaluation(:total_votes, 6, @user)
+ res = Question.with_normalized_reputation_only(:total_votes)
+ res.length.should == 2
+ res[0].normalized_total_votes.should be_within(DELTA).of(0)
+ res[1].normalized_total_votes.should be_within(DELTA).of(1)
+ end
+
+ it "should not retain select option" do
+ res = Question.with_normalized_reputation_only(:total_votes).select("questions.id")
+ res.length.should == 1
+ res[0].id.should_not be_nil
+ lambda {res[0].text}.should raise_error
+ end
+
+ it "should retain conditions option" do
+ @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
+ @question2.add_evaluation(:total_votes, 6, @user)
+ res = Question.with_normalized_reputation_only(:total_votes).where("normalized_total_votes > 0.6")
+ res.length.should == 1
+ res[0].normalized_total_votes.should > 0.6
+ end
+
+ it "should retain joins option" do
+ res = Question.with_normalized_reputation_only(:total_votes).
+ select("questions.*, users.name AS user_name").
+ joins("JOIN users ON questions.author_id = users.id")
+ res.length.should == 1
+ res[0].user_name.should == @user.name
+ end
+ end
+
+ context "With Scopes" do
+ before :each do
+ @trans_ja = Translation.create!(:text => "Ichi", :user => @user, :locale => "ja", :phrase => @phrase)
+ @trans_ja.add_evaluation(:votes, 3, @user)
+ @trans_fr = Translation.create!(:text => "Ichi", :user => @user, :locale => "fr", :phrase => @phrase)
+ @trans_fr.add_evaluation(:votes, 6, @user)
+ end
+
+ it "should return result with given reputation" do
+ res = Phrase.with_normalized_reputation_only(:maturity, :ja)
+ res.length.should == 1
+ res[0].normalized_maturity.should be_within(DELTA).of(0)
+ end
+ end
+ end
end

0 comments on commit e111f5f

Please sign in to comment.