This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
rs_evaluation.rb
69 lines (59 loc) · 3.02 KB
/
rs_evaluation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
##
# 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.
##
class RSEvaluation < ActiveRecord::Base
belongs_to :source, :polymorphic => true
belongs_to :target, :polymorphic => true
has_one :sent_messages, :as => :sender, :class_name => 'RSReputationMessage', :dependent => :destroy
attr_accessible :reputation_name, :value, :source, :source_id, :source_type, :target, :target_id, :target_type
# Sets an appropriate source type in case of Single Table Inheritance.
before_validation :set_source_type_for_sti
# the same source cannot evaluate the same target more than once.
validates_uniqueness_of :source_id, :scope => [:reputation_name, :source_type, :target_id, :target_type]
validate :source_must_be_defined_for_reputation_in_network
def self.find_by_reputation_name_and_source_and_target(reputation_name, source, target)
source_type = get_source_type_for_sti(source, target.class.name, reputation_name)
RSEvaluation.find(:first,
:conditions => {:reputation_name => reputation_name.to_s,
:source_id => source.id,
:source_type => source_type,
:target_id => target.id,
:target_type => target.class.name
})
end
def self.create_evaluation(reputation_name, value, source, target)
RSEvaluation.create!(:reputation_name => reputation_name.to_s, :value => value,
:source_id => source.id, :source_type => source.class.name,
:target_id => target.id, :target_type => target.class.name)
end
protected
def self.get_source_type_for_sti(source, target_type, reputation_name)
valid_source_type = ReputationSystem::Network.get_reputation_def(target_type, reputation_name)[:source].to_s.camelize
temp = source.class
while temp && valid_source_type != temp.name && temp.name != "ActiveRecord::Base"
temp = temp.superclass
end
temp ? temp.name : nil
end
def set_source_type_for_sti
temp = self.class.get_source_type_for_sti(source, target_type, reputation_name)
self.source_type = temp if temp
end
def source_must_be_defined_for_reputation_in_network
unless source_type == ReputationSystem::Network.get_reputation_def(target_type, reputation_name)[:source].to_s.camelize
errors.add(:source_type, "#{source_type} is not source of #{reputation_name} reputation")
end
end
end