theoooo-tally 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,7 @@ class <%= migration_name %> < ActiveRecord::Migration
15
15
 
16
16
  <% tallyables.each do |t| -%>
17
17
  add_column :<%= t.underscore.camelize.tableize %>, :tally_sheets_count, :string
18
+ add_column :<%= t.underscore.camelize.tableize %>, :tally_score, :string
18
19
  <% end -%>
19
20
  end
20
21
 
@@ -6,11 +6,9 @@ module Tally
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- def has_tally
10
- #require 'statistics2'
11
-
9
+ def has_tally
12
10
  include InstanceMethods
13
- has_many :votes, :as => :tallyable, :class_name => "TallySheet"
11
+ has_many :votes, :as => :tallyable, :class_name => "TallySheet", :after_add => :update_tally_score!, :after_remove => :update_tally_score!
14
12
  has_many :votes_for, :as => :tallyable, :class_name => "TallySheet", :conditions => {:for => true}
15
13
  has_many :votes_against, :as => :tallyable, :class_name => "TallySheet", :conditions => {:for => false}
16
14
  end
@@ -22,20 +20,19 @@ module Tally
22
20
  # TODO: Figure out why I can't do :voter => voter
23
21
  !!votes.find(:first, :conditions => {:voter_id => voter, :voter_type => voter.class.to_s})
24
22
  end
25
-
26
- def vote_score
27
- vote_ci_lower_bound
23
+
24
+ def update_tally_score!
25
+ update_attribute(:tally_score, tally_ci_lower_bound) # Skips validation
28
26
  end
29
27
 
30
28
  # From http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
31
- def vote_ci_lower_bound(pos = votes_for.count, n = votes.count, power = 0.10)
32
- 1
33
- # if n == 0
34
- # return 0
35
- # end
36
- # z = Statistics2.pnormaldist(1-power/2)
37
- # phat = 1.0*pos/n
38
- # (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
29
+ def tally_ci_lower_bound(pos = votes_for.count, n = votes.count, power = 0.10)
30
+ if n == 0
31
+ return 0
32
+ end
33
+ z = Rubystats::NormalDistribution.new.icdf(1-power/2)
34
+ phat = 1.0*pos/n
35
+ (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
39
36
  end
40
37
 
41
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theoooo-tally
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Cushion
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-01 00:00:00 -07:00
12
+ date: 2009-05-05 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubystats
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description:
17
26
  email: theo@jivatechnology.com
18
27
  executables: []