Permalink
Browse files

Merge branch 'master' of https://github.com/twitter/activerecord-repu…

  • Loading branch information...
2 parents 37eb2b9 + 34e919c commit d5d4ce2e0073b2da4d64d9732be0e2addccdaea2 Katsuya Noguchi committed Aug 9, 2012
View
@@ -1,2 +1,4 @@
Gemfile.lock
pkg
+*.swp
+*.DS_Store
View
@@ -1,4 +1,8 @@
- * Make evaluation methods return true on success.
+== 1.3.0
+
+* Add evaluated_by method.
+
+* Make evaluation methods return true on success.
== 1.2.1
View
@@ -165,12 +165,12 @@ reputations_activated?(reputation_name)
# Includes the specified reputation value for the given name via a normal Active Record find query.
ActiveRecord::Base.find_with_reputation(reputation_name, find_scope, options)
# For example:
-ActiveRecord::Base.find_with_reputation(:maturity, :all, {:select => "id", :conditions => ["maturity > ?", 3], :order => "maturity"})
+User.find_with_reputation(:karma, :all, {:select => "id", :conditions => ["karma > ?", 3], :order => "karma"})
# Includes the specified normalized reputation value for the given name via a normal Active Record find query.
ActiveRecord::Base.find_with_normalized_reputation(reputation_name, find_options)
# For example:
-ActiveRecord::Base.find_with_normalized_reputation(:maturity, :all, {:select => "id", :conditions => ["maturity > ?", 3], :order => "maturity"})
+User.find_with_normalized_reputation(:karma, :all, {:select => "id", :conditions => ["karma > ?", 0.5], :order => "karma"})
# Includes the specified reputation value for the given name via a normal Active Record count query.
ActiveRecord::Base.count_with_reputation(reputation_name, find_options)
@@ -179,6 +179,14 @@ ActiveRecord::Base.count_with_reputation(reputation_name, find_options)
ActiveRecord::Base.find_with_reputation_sql(reputation_name, find_options)
```
+## Querying for Evaluated ActiveRecord
+```ruby
+# Returns all active record instances evaluated by a given source for a given reputation name.
+ActiveRecord::Base.evaluated_by(reputation_name, source)
+# For example:
+Question.evaluated_by(:votes, @user)
+```
+
## Advanced Topics
### Scope
Reputations can have different scopes to provide additional context.
@@ -213,6 +221,10 @@ To execute an Active Record query using a scoped reputation, try this:
```ruby
ActiveRecord::Base.find_with_reputation(:reputation_name, :scope, :find_options)
```
+To find active records evaluated by a given source for a scoped reputation, try this:
+```ruby
+ActiveRecord::Base.evaluated_by(:reputation_name, source, :scope)
+```
There are a few more helper methods available for scopes:
```ruby
# Allows you to add a scope dynamically.
Binary file not shown.
Binary file not shown.
@@ -16,6 +16,23 @@
module ReputationSystem
module Evaluation
+ module ClassMethods
+ def evaluated_by(reputation_name, source, *args)
+ scope = args.first
+ srn = ReputationSystem::Network.get_scoped_reputation_name(self.name, reputation_name, scope)
+ source_type = source.class.name
+ options = {}
+ options[:select] ||= sanitize_sql_array(["%s.*", self.table_name])
+ options[:joins] = sanitize_sql_array(["JOIN rs_evaluations ON %s.id = rs_evaluations.target_id AND rs_evaluations.target_type = ? AND rs_evaluations.reputation_name = ? AND rs_evaluations.source_id = ? AND rs_evaluations.source_type = ?", self.name, srn.to_s, source.id, source_type])
+ options[:joins] = sanitize_sql_array([options[:joins], self.table_name])
+ find(:all, options)
+ end
+ end
+
+ def self.included(klass)
+ klass.extend ClassMethods
+ end
+
def add_evaluation(reputation_name, value, source, *args)
scope = args.first
srn = ReputationSystem::Network.get_scoped_reputation_name(self.class.name, reputation_name, scope)
@@ -15,5 +15,5 @@
##
module ReputationSystem
- VERSION = "1.2.1"
+ VERSION = "1.3.0"
end
@@ -26,6 +26,52 @@
end
context "Primary Reputation" do
+ describe "#evaluated_by" do
+ it "should return an empty array if it is not evaluaated by a given source" do
+ Question.evaluated_by(:total_votes, @user).should == []
+ end
+
+ it "should return an array of targets evaluated by a given source" do
+ user2 = User.create!(:name => 'katsuya')
+ question2 = Question.create!(:text => 'Question 2', :author_id => @user.id)
+ question3 = Question.create!(:text => 'Question 3', :author_id => @user.id)
+ @question.add_evaluation(:total_votes, 1, @user).should be_true
+ question2.add_evaluation(:total_votes, 2, user2).should be_true
+ question3.add_evaluation(:total_votes, 3, @user).should be_true
+ Question.evaluated_by(:total_votes, @user).should == [@question, question3]
+ Question.evaluated_by(:total_votes, user2).should == [question2]
+ end
+
+ context "With Scopes" do
+ it "should return an array of targets evaluated by a given source on appropriate scope" do
+ user2 = User.create!(:name => 'katsuya')
+ phrase2 = Phrase.create!(:text => "Two")
+ @phrase.add_evaluation(:difficulty_with_scope, 1, @user, :s1).should be_true
+ @phrase.add_evaluation(:difficulty_with_scope, 2, @user, :s2).should be_true
+ @phrase.add_evaluation(:difficulty_with_scope, 3, user2, :s2).should be_true
+ @phrase.add_evaluation(:difficulty_with_scope, 4, user2, :s3).should be_true
+ phrase2.add_evaluation(:difficulty_with_scope, 1, user2, :s1).should be_true
+ phrase2.add_evaluation(:difficulty_with_scope, 2, user2, :s2).should be_true
+ phrase2.add_evaluation(:difficulty_with_scope, 3, @user, :s2).should be_true
+ phrase2.add_evaluation(:difficulty_with_scope, 4, @user, :s3).should be_true
+ Phrase.evaluated_by(:difficulty_with_scope, @user, :s1).should == [@phrase]
+ Phrase.evaluated_by(:difficulty_with_scope, user2, :s1).should == [phrase2]
+ Phrase.evaluated_by(:difficulty_with_scope, @user, :s2).should == [@phrase, phrase2]
+ Phrase.evaluated_by(:difficulty_with_scope, user2, :s2).should == [@phrase, phrase2]
+ Phrase.evaluated_by(:difficulty_with_scope, @user, :s3).should == [phrase2]
+ Phrase.evaluated_by(:difficulty_with_scope, user2, :s3).should == [@phrase]
+ end
+
+ it "should raise exception if invalid scope is given" do
+ lambda{@phrase.add_evaluation(:difficulty_with_scope, 1, :invalid_scope)}.should raise_error(ArgumentError)
+ end
+
+ it "should raise exception if scope is not given" do
+ lambda{@phrase.add_evaluation(:difficulty_with_scope, 1)}.should raise_error(ArgumentError)
+ end
+ end
+ end
+
describe "#add_evaluation" do
it "should create evaluation in case of valid input" do
@question.add_evaluation(:total_votes, 1, @user).should be_true
@@ -1,28 +0,0 @@
-##
-# Copyright 2012 Twitter, Inc
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-##
-
-require 'spec_helper'
-
-describe ActiveRecord::Base do
-
- before(:each) do
- @user = User.create!(:name => 'jack')
- @question = Question.create!(:text => 'Does this work?', :author_id => @user.id)
- @answer = Answer.create!(:text => 'Yes!', :author_id => @user.id, :question_id => @question.id)
- @phrase = Phrase.create!(:text => "One")
- end
-
-end

0 comments on commit d5d4ce2

Please sign in to comment.