class Voting::Voting
Public Class Methods
update_voting(resource, scopeable)
click to toggle source
# File lib/voting/models/voting/voting.rb, line 35 def update_voting(resource, scopeable) record = find_or_initialize_by(resource: resource, scopeable: scopeable) values = values_data(resource, scopeable) record.estimate = estimate(values) record.negative = values.voting_negative record.positive = values.voting_positive record.save! end
values_data(resource, scopeable)
click to toggle source
# File lib/voting/models/voting/voting.rb, line 20 def values_data(resource, scopeable) sql = %( SELECT COALESCE(SUM(negative), 0) voting_negative, COALESCE(SUM(positive), 0) voting_positive FROM #{vote_table_name} WHERE resource_type = ? and resource_id = ? and #{scope_query(scopeable)} ).squish values = [sql, resource.class.name, resource.id] values += [scopeable.class.name, scopeable.id] unless scopeable.nil? execute_sql values end
Private Class Methods
estimate(values)
click to toggle source
# File lib/voting/models/voting/voting.rb, line 48 def estimate(values) sum = values.voting_negative + values.voting_positive return 0 if sum.zero? square = Math.sqrt((values.voting_negative * values.voting_positive) / sum + 0.9604) ((values.voting_positive + 1.9208) / sum - 1.96 * square / sum) / (1 + 3.8416 / sum) end
execute_sql(sql)
click to toggle source
# File lib/voting/models/voting/voting.rb, line 58 def execute_sql(sql) Vote.find_by_sql(sql).first end
scope_query(scopeable)
click to toggle source
# File lib/voting/models/voting/voting.rb, line 66 def scope_query(scopeable) return 'scopeable_type is NULL and scopeable_id is NULL' if scopeable.nil? 'scopeable_type = ? and scopeable_id = ?' end
vote_table_name()
click to toggle source
# File lib/voting/models/voting/voting.rb, line 62 def vote_table_name @vote_table_name ||= Vote.table_name end