Skip to content
This repository
Browse code

Add support for normalized reputation Arel query

  • Loading branch information...
commit e111f5f346a64903b201612c7f6d22ff280b4f76 1 parent b8514b5
Katsuya Noguchi authored
4 CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,7 @@
  1 +* Add `with_normalized_reputation` and `with_normalized_reputation_only`
  2 +
  3 +* Add `with_reputation` and `with_reputation_only` methods.
  4 +
1 5 ## ActiveRecordReputationSystem 1.3.4 ##
2 6
3 7 * Fix name of a migration class again.
14 lib/reputation_system/query_methods.rb
@@ -35,6 +35,20 @@ def with_reputation_only(*args)
35 35 self.select(select).joins(joins)
36 36 end
37 37
  38 + def with_normalized_reputation(*args)
  39 + reputation_name, srn = parse_arel_query_args(args)
  40 + select = build_select_statement(table_name, reputation_name, nil, srn, true)
  41 + joins = build_join_statement(table_name, name, srn)
  42 + self.select(select).joins(joins)
  43 + end
  44 +
  45 + def with_normalized_reputation_only(*args)
  46 + reputation_name, srn = parse_arel_query_args(args)
  47 + select = build_select_statement_with_reputation_only(table_name, reputation_name, srn, true)
  48 + joins = build_join_statement(table_name, name, srn)
  49 + self.select(select).joins(joins)
  50 + end
  51 +
38 52 protected
39 53
40 54 def parse_arel_query_args(args)
76 spec/reputation_system/query_methods_spec.rb
@@ -126,7 +126,7 @@
126 126 end
127 127 end
128 128 end
129   -=begin
  129 +
130 130 describe "#with_normalized_reputation" do
131 131 context "Without Scopes" do
132 132 before :each do
@@ -136,30 +136,30 @@
136 136 it "should return result with given normalized reputation" do
137 137 @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
138 138 @question2.add_evaluation(:total_votes, 6, @user)
139   - res = Question.with_normalized_reputation(:total_votes, :all, {})
  139 + res = Question.with_normalized_reputation(:total_votes)
140 140 res.should == [@question, @question2]
141 141 res[0].normalized_total_votes.should be_within(DELTA).of(0)
142 142 res[1].normalized_total_votes.should be_within(DELTA).of(1)
143 143 end
144 144
145   - it "should retain select option" do
146   - res = Question.with_normalized_reputation(:total_votes, :all, {:select => "questions.id"})
  145 + it "should not retain select option" do
  146 + res = Question.with_normalized_reputation(:total_votes).select("questions.id")
147 147 res.should == [@question]
148 148 res[0].id.should_not be_nil
149   - lambda {res[0].text}.should raise_error
  149 + lambda {res[0].text}.should_not raise_error
150 150 end
151 151
152 152 it "should retain conditions option" do
153 153 @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
154 154 @question2.add_evaluation(:total_votes, 6, @user)
155   - res = Question.with_normalized_reputation(:total_votes, :all, {:conditions => "normalized_total_votes > 0.6"})
  155 + res = Question.with_normalized_reputation(:total_votes).where("normalized_total_votes > 0.6")
156 156 res.should == [@question2]
157 157 end
158 158
159 159 it "should retain joins option" do
160   - res = Question.with_normalized_reputation(:total_votes, :all, {
161   - :select => "questions.*, users.name AS user_name",
162   - :joins => "JOIN users ON questions.author_id = users.id"})
  160 + res = Question.with_normalized_reputation(:total_votes).
  161 + select("questions.*, users.name AS user_name").
  162 + joins("JOIN users ON questions.author_id = users.id")
163 163 res.should == [@question]
164 164 res[0].user_name.should == @user.name
165 165 end
@@ -174,11 +174,65 @@
174 174 end
175 175
176 176 it "should return result with given reputation" do
177   - res = Phrase.with_normalized_reputation(:maturity, :ja, :all, {})
  177 + res = Phrase.with_normalized_reputation(:maturity, :ja)
178 178 res.should == [@phrase]
179 179 res[0].normalized_maturity.should be_within(DELTA).of(0)
180 180 end
181 181 end
182 182 end
183   -=end
  183 +
  184 + describe "#with_normalized_reputation_only" do
  185 + context "Without Scopes" do
  186 + before :each do
  187 + @question.add_evaluation(:total_votes, 3, @user)
  188 + end
  189 +
  190 + it "should return result with given normalized reputation" do
  191 + @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
  192 + @question2.add_evaluation(:total_votes, 6, @user)
  193 + res = Question.with_normalized_reputation_only(:total_votes)
  194 + res.length.should == 2
  195 + res[0].normalized_total_votes.should be_within(DELTA).of(0)
  196 + res[1].normalized_total_votes.should be_within(DELTA).of(1)
  197 + end
  198 +
  199 + it "should not retain select option" do
  200 + res = Question.with_normalized_reputation_only(:total_votes).select("questions.id")
  201 + res.length.should == 1
  202 + res[0].id.should_not be_nil
  203 + lambda {res[0].text}.should raise_error
  204 + end
  205 +
  206 + it "should retain conditions option" do
  207 + @question2 = Question.create!(:text => 'Does this work?', :author_id => @user.id)
  208 + @question2.add_evaluation(:total_votes, 6, @user)
  209 + res = Question.with_normalized_reputation_only(:total_votes).where("normalized_total_votes > 0.6")
  210 + res.length.should == 1
  211 + res[0].normalized_total_votes.should > 0.6
  212 + end
  213 +
  214 + it "should retain joins option" do
  215 + res = Question.with_normalized_reputation_only(:total_votes).
  216 + select("questions.*, users.name AS user_name").
  217 + joins("JOIN users ON questions.author_id = users.id")
  218 + res.length.should == 1
  219 + res[0].user_name.should == @user.name
  220 + end
  221 + end
  222 +
  223 + context "With Scopes" do
  224 + before :each do
  225 + @trans_ja = Translation.create!(:text => "Ichi", :user => @user, :locale => "ja", :phrase => @phrase)
  226 + @trans_ja.add_evaluation(:votes, 3, @user)
  227 + @trans_fr = Translation.create!(:text => "Ichi", :user => @user, :locale => "fr", :phrase => @phrase)
  228 + @trans_fr.add_evaluation(:votes, 6, @user)
  229 + end
  230 +
  231 + it "should return result with given reputation" do
  232 + res = Phrase.with_normalized_reputation_only(:maturity, :ja)
  233 + res.length.should == 1
  234 + res[0].normalized_maturity.should be_within(DELTA).of(0)
  235 + end
  236 + end
  237 + end
184 238 end

0 comments on commit e111f5f

Please sign in to comment.
Something went wrong with that request. Please try again.