소스 검색

Move set positions logic to Election/Poll models. #15. Fix #30

tags/last-sinatra-version
Adrian Short 8 년 전
부모
커밋
ee8ac9a17b
1개의 변경된 파일27개의 추가작업 그리고 1개의 파일을 삭제
  1. +27
    -1
      models.rb

+ 27
- 1
models.rb 파일 보기

@@ -35,6 +35,29 @@ class Poll
def successful_candidacies # Candidacies where the candidate was elected
Candidacy.all(:election => @election, :district => @district, :order => [:position], :limit => @seats)
end
# Set candidacy.position for every candidacy in this poll
# Returns array of candidacies, or false if we don't have results for this poll
def set_positions
# Check that every candidacy for this poll has its votes recorded (ie that the election results are known)
if Candidacy.count(:conditions => { :district_id => @district_id, :election_id => @election_id, :votes => nil }) == 0
return false
end

# Get the candidacies for this poll
ccys = Candidacy.all(:conditions => { :district_id => @district_id, :election_id => @election_id }, :order => [:votes.desc])

puts "Found no candidacies for this poll" if ccys.size == 0

position = 1
ccys.each do |ccy|
position <= @seats ? ccy.seats = 1 : ccy.seats = 0
ccy.position = position
ccy.save
position += 1
end
ccys
end

belongs_to :election
belongs_to :district
@@ -200,6 +223,10 @@ class Election
def ballot_papers_issued
Poll.sum(:ballot_papers_issued, :election => self)
end
def set_positions
polls.each { |p| p.set_positions }
end
end

class District
@@ -209,7 +236,6 @@ class District
property :body_id, Integer, :required => true
property :name, String, :length => 255, :required => true
property :slug, String
property :seats, Integer
property :ons_district_code, String

belongs_to :body


불러오는 중...
취소
저장