Created
November 19, 2013 23:15
-
-
Save waneka/7554327 to your computer and use it in GitHub Desktop.
A selection of Ruby snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The following are snippets from the fantasy politics web app that we made. Unfortunately, | |
it's not currently online for your viewing. :( | |
This is a snippet from the view for the team page. | |
<h3><%= @queried_team.name %> <br>Total Score: <%= @queried_team.team_score(1) %></h3> | |
<p>A team can consist of up to four senators and four congressmen</p> | |
<div id="team_members"> | |
<h4>Team Members:</h4> | |
<ul class="sortable"> | |
<% unless @queried_team.politicians.nil? %> | |
<% @queried_team.politicians.each_with_index do |sleazeball, index| %> | |
<li class="name pol-<%= index %>" rel="<%= sleazeball.id %>"><img src="/images/mugshots_sm/<%= sleazeball.bioguide_id %>.jpg" class="pol"> <%= sleazeball.info %></li> | |
<% end %> | |
<% end %> | |
</ul> | |
</div> | |
<% if @access %> | |
<input value="Update My Team!" type="submit" id="update" class="button"> | |
<div id="availablePoliticians"> | |
<input type="text" id="filterInput" placeholder="Filter Politicians here"> | |
<ul class="sortable"> | |
</ul> | |
</div> | |
<div id="hidden"> | |
<ul> | |
<% @politicians.each_with_index do |sleazeball, index| %> | |
<li class="pol-<%= index %>" rel="<%= sleazeball.id %>"> | |
<span class="name"><img src="/images/mugshots_sm/<%= sleazeball.bioguide_id %>.jpg" class="pol"> <%= sleazeball.info %></span> | |
</li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
And here is the model for the politicians: | |
class Politician < ActiveRecord::Base | |
has_many :politician_teams | |
has_many :teams, through: :politician_teams | |
has_many :scores | |
def info | |
"#{self.title}. #{self.first_name} #{self.last_name}, #{self.state} [#{self.party}]" | |
end | |
def self.getAllTwitterHandles | |
all.delete_if{ |politician| politician.in_office == false || politician.twitter_id.nil? }.map{ |politician| politician.twitter_id }.map!{|politician| "@#{politician}"} | |
end | |
def twitter_score(week) | |
pol_score = scores.where(game_number: week)[0] | |
if pol_score && pol_score.twitter_mentions | |
pol_score.twitter_mentions.to_f / 20 | |
else | |
0 | |
end | |
end | |
def sunlight_score(week) | |
pol_score = scores.where(game_number: week)[0] | |
if pol_score && pol_score.vote_score | |
pol_score.vote_score * 5 | |
else | |
0 | |
end | |
end | |
def total_score(week) | |
(twitter_score(week) + sunlight_score(week)).round(2) | |
end | |
def self.top_scorers(week, limit = 10) | |
politicos = Politician.all | |
politicos.sort_by {|pol| pol.total_score(week)}.reverse | |
politicos[0...limit] | |
end | |
def self.sort_by_score(week) | |
politicos = Politician.all | |
politicos.sort_by {|pol| pol.total_score(week)}.reverse | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment