Class Rating
In: app/models/rating.rb
Parent: ActiveRecord::Base

Ratings are ratings given by Users on Logis.

Ratings are always related to a PeerGroup (this can be the default PeerGroup). Ratings are given by Users within a PeerGroup and with the voting-weight they have within that PeerGroup.

Ratings are never destroyed before their logi or peer_group.

Always only use vote for setting scores and powers…

Methods

Included Modules

Comparable

Public Class methods

Devaluates the weights of the ratings

[Source]

    # File app/models/rating.rb, line 59
59:   def self.devaluate_weights(fraction)
60:     PeerGroup.find_each do |peer_group|
61:       peer_group.ratings.find_each do |rating|
62:         rating.weight *= fraction
63:         rating.save
64:       end
65:     end
66:   end

Public Instance methods

Compares ratings by score.

Is also capable of comparing with nil (in concert with the NilClass extension in config/initializers/nil.rb)

[Source]

     # File app/models/rating.rb, line 116
116:   def <=>(other)
117:     if other == nil
118:       if self.score >= 0
119:         return 1
120:       else
121:         return -1
122:       end
123:     else
124:       return self.score <=> other.score
125:     end
126:   end

Calculates the amount of power this rating generates for the user.

[Source]

    # File app/models/rating.rb, line 81
81:   def power_income
82:     if self.score > 0
83:       return (self.score ** 2) * 0.05
84:     else
85:       return 0.0
86:     end
87:   end

Returns true if this rating has any power…

[Source]

    # File app/models/rating.rb, line 74
74:   def powered?
75:     return (self.weight != 0)
76:   end

Adds a vote to the rating with the given score & users power.

[Source]

     # File app/models/rating.rb, line 132
132:   def vote(vote)
133:     # checks
134:     if !vote.new_record?
135:       raise t('m.rating.error_already_voted')
136:     end
137:     if !vote.user.anonymous? and vote.user == self.logi.creator
138:       raise t('m.rating.error_vote_for_own')
139:     end
140:     if !vote.powered?
141:       raise t('m.rating.error_no_power')
142:     end
143:     # calculation
144:     if self.powered?
145:       old_power_income = self.power_income
146:     else
147:       old_power_income = 0
148:     end
149:     self.score = ((self.score * self.weight) + (vote.score * vote.weight)) / (self.weight + vote.weight)
150:     self.weight += vote.weight
151:     # power distribution to the logi's owner
152:     if !self.logi.creator.anonymous?
153:       membership = self.peer_group.membership_for(self.logi.creator)
154:       membership.power += (self.power_income - old_power_income)
155:       membership.save
156:     end
157:     # saving
158:     self.save
159:     vote.save
160:   end

Returns a new vote.

[Source]

     # File app/models/rating.rb, line 91
 91:   def vote_for(user, ip_address)
 92:     vote = nil
 93:     conditions = 'created_at > "' + 1.week.ago.to_s(:db) + '"'
 94:     if !user.anonymous?
 95:       vote = self.votes.find_by_user_id(user.id,
 96:           :conditions => conditions)
 97:     elsif ip_address
 98:       vote = self.votes.find_by_ip_address(ip_address,
 99:           :conditions => conditions)
100:     end
101:     if vote.nil?
102:       weight = self.peer_group.membership_for(user).power
103:       vote = self.votes.build(:user => user, :weight => weight,
104:           :ip_address => ip_address)
105:     end
106:     return vote
107:   end

Protected Instance methods

Logs positively voted to peergroup channel on log.logilogi.org

[Source]

     # File app/models/rating.rb, line 168
168:   def log_positive_to_log_log
169:     if GlobalConfig.use_log_log
170:       if self.score > 0 and (self.new_record? or self.score_was < 0)
171:         m = LogLogMessage.new(
172:             :kind => "Logi",
173:             :channel_name =>
174:                 'for_' + self.peer_group.unix_name,
175:             :title => self.logi.current_title,
176:             :author => self.logi.creator.name,
177:             :text => self.logi.current_snippet,
178:             :url => GlobalConfig.site_url + self.logi.link.to_s,
179:             :unique_id => self.logi.perma_id)
180:         m.save
181:       end
182:     end
183:   end

Revokes the voting-power the user received via this rating.

[Source]

     # File app/models/rating.rb, line 187
187:   def revoke_voting_power
188:     if !self.logi.creator.anonymous?
189:       membership = self.peer_group.membership_for(self.logi.creator)
190:       membership.power -= self.power_income
191:       membership.save
192:     end
193:     return true
194:   end

[Validate]