Permalink
Browse files

Add evaluations associations for all evaluation targets

  • Loading branch information...
1 parent 05e7639 commit a41233fbe398d42d47dc4f8abea73647fffadc30 Katsuya Noguchi committed Oct 3, 2012
Showing with 57 additions and 1 deletion.
  1. +2 −0 CHANGELOG.md
  2. +11 −1 lib/reputation_system/base.rb
  3. +44 −0 spec/reputation_system/base_spec.rb
View
@@ -1,3 +1,5 @@
+* Add `evaluations` association for all evaluation targets.
+
* Set `:sum` as default for `aggregated_by` option.
* Rename models - RSReputation to ReputationSystem::Reputation, RSEvaluation to ReputationSystem::Evaluation and RSReputationMessage to ReputationSystem::ReputationMessage
@@ -49,7 +49,17 @@ def has_reputation(reputation_name, options)
# If it is first time to be called
unless ancestors.include?(ReputationSystem::ReputationMethods)
- has_many :reputations, :as => :target, :class_name => "ReputationSystem::Reputation", :dependent => :destroy
+ has_many :reputations, :as => :target, :class_name => "ReputationSystem::Reputation", :dependent => :destroy do
+ def for(reputation_name)
+ self.where(:reputation_name => reputation_name)
+ end
+ end
+ has_many :evaluations, :as => :target, :class_name => "ReputationSystem::Evaluation", :dependent => :destroy do
+ def for(reputation_name)
+ self.where(:reputation_name => reputation_name)
+ end
+ end
+
include ReputationSystem::QueryBuilder
include ReputationSystem::QueryMethods
include ReputationSystem::FinderMethods
@@ -65,4 +65,48 @@
end
end
end
+
+ context "Association" do
+ describe "#reputations" do
+ it "should define reputations association" do
+ @question.respond_to?(:reputations).should == true
+ end
+ it "should return all reputations for the target" do
+ @question.add_evaluation(:total_votes, 2, @user)
+ @question.add_evaluation(:difficulty, 2, @user)
+ @question.reputations.count.should == 2
+ end
+ describe "#for" do
+ it "should return empty array if there is no reputation for the target" do
+ @question.reputations.for(:total_votes).should == []
+ end
+ it "should return all reputations of the given type for the target" do
+ @question.add_evaluation(:total_votes, 2, @user)
+ @question.add_evaluation(:difficulty, 2, @user)
+ @question.reputations.for(:total_votes).count.should == 1
+ end
+ end
+ end
+
+ describe "#evaluations" do
+ it "should define evaluations association" do
+ @question.respond_to?(:evaluations).should == true
+ end
+ it "should return all evaluations for the target" do
+ @question.add_evaluation(:total_votes, 2, @user)
+ @question.add_evaluation(:difficulty, 2, @user)
+ @question.evaluations.count.should == 2
+ end
+ describe "#for" do
+ it "should return empty array if there is no evaluation for the target" do
+ @question.evaluations.for(:total_votes).should == []
+ end
+ it "should return all evaluations of the given type for the target" do
+ @question.add_evaluation(:total_votes, 2, @user)
+ @question.add_evaluation(:difficulty, 2, @user)
+ @question.evaluations.for(:total_votes).count.should == 1
+ end
+ end
+ end
+ end
end

0 comments on commit a41233f

Please sign in to comment.