Skip to content

Instantly share code, notes, and snippets.

@sgonyea
Created November 30, 2010 01:58
Show Gist options
  • Select an option

  • Save sgonyea/721001 to your computer and use it in GitHub Desktop.

Select an option

Save sgonyea/721001 to your computer and use it in GitHub Desktop.
class Motion < ActiveRecord::Base
belongs_to :member
has_many :events
validates_inclusion_of :state, :in =>
%w(waitingsecond waitingexpedited waitingobjection
objected voting passed failed approved)
def seconds
events.where(:type => "second").count
end
def ayes
events.where(:type => "vote", :value => true).count
end
def nays
events.where(:type => "vote", :value => false).count
end
def required_votes
possible_votes / 2 + 1
end
def seconds_for_expediting
possible_votes / 3
end
def second(member)
# TODO: Members cannot second their own motions
events.create(:member => member, :type => "second")
second_count = seconds
if state == "waitingsecond" && second_count >= 2
waitingobjection!
elsif state == "waitingexpedited" && second_count >= seconds_for_expediting
voting!
end
end
def vote(member, value)
events.create(:member => member, :type => "vote", :value => value)
passed! if ayes > required_votes
end
belongs_to :member
has_many :seconds, :class_name => :events,
:select => "type = second"
has_many :votes, :class_name => :events,
:conditions => "type = vote" do
# @return [ActiveRecord::Relation] An Array-like structure, of all aye-votes cast
def yeas
where :value => true
end
# @return [ActiveRecord::Relation] An Array-like structure, of all nay-votes cast
def nays
where :value => false
end
end
# @return [Fixnum] The current count of yea votes
def yeas
votes.yeas.count
end
alias :ayes :yeas
# @return [Fixnum] The current count of nay votes
def nays
votes.nays.count
end
belongs_to :member
has_many :seconds
has_many :votes do
# @return [ActiveRecord::Relation] An Array-like structure, of all aye-votes cast
def yeas
where :value => true
end
# @return [ActiveRecord::Relation] An Array-like structure, of all nay-votes cast
def nays
where :value => false
end
end
# @return [Fixnum] The current count of yea votes
def yeas
votes.yeas.count
end
alias :ayes :yeas
# @return [Fixnum] The current count of nay votes
def nays
votes.nays.count
end
# @return [Fixnum] The number of votes required to pass this Motion
def required_votes
possible_votes / 2 + 1
end
# @return [true, false] Whether or not Motion has met its requirement for passage
def has_met_requirement?
yeas >= required_votes
end
# @return [Fixnum] The numbers of seconds required to expedite
def seconds_for_expedition
possible_votes / 3
end
alias :seconds_for_expediting :seconds_for_expedition
# Second this Motion
# @param [Member] member The member who is seconding this motion
# @return [true, false] Whether or not the second was accepted
# @TODO @return
def second(member)
seconds.create(:member => member)
second_count = seconds
if state == "waitingsecond" && second_count >= 2
waitingobjection!
elsif state == "waitingexpedited" && second_count >= seconds_for_expediting
voting!
end
end
# Cast a Member's Vote
# @param [Member] member An active member
# @param [true, false] value An aye or nay vote
# @return [true, false] Whether or not the vote was accepted
# @TODO @return
def vote(member, value)
votes.create(:member => member, :value => value)
passed! if ayes > required_votes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment