re-namespacing a touch
This commit is contained in:
10
map-mash/app/controllers/application_controller.rb
Normal file
10
map-mash/app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
# Filters added to this controller apply to all controllers in the application.
|
||||
# Likewise, all the methods added will be available for all controllers.
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
helper :all # include all helpers, all the time
|
||||
protect_from_forgery # See ActionController::RequestForgeryProtection for details
|
||||
|
||||
# Scrub sensitive parameters from your log
|
||||
# filter_parameter_logging :password
|
||||
end
|
28
map-mash/app/controllers/maps_controller.rb
Normal file
28
map-mash/app/controllers/maps_controller.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
class MapsController < ApplicationController
|
||||
def index
|
||||
@maps = Map.all
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.xml { render :xml => @maps }
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
@map = Map.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
format.png do
|
||||
GoogleMapLocationFetcher.new.fetch([@map.name]) do |loc,image|
|
||||
dest = Rails.root.join("public/maps/#{@map.id}.png")
|
||||
FileUtils.mkdir_p(File.dirname(dest))
|
||||
|
||||
File.open(dest, 'w') do |f|
|
||||
f.write(image)
|
||||
end
|
||||
send_file(dest, :type => 'image/png')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
76
map-mash/app/controllers/mash_tournaments_controller.rb
Normal file
76
map-mash/app/controllers/mash_tournaments_controller.rb
Normal file
@@ -0,0 +1,76 @@
|
||||
class MashTournamentsController < ApplicationController
|
||||
def index
|
||||
@mash_tournaments = MashTournament.all
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
begin
|
||||
@mash_tournament = MashTournament.find(params[:id])
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
redirect_to :action => 'new' and return
|
||||
end
|
||||
|
||||
if not @mash_tournament.done?
|
||||
redirect_to new_mash_path
|
||||
flash[:notice] = "You're not quite done there."
|
||||
return
|
||||
end
|
||||
|
||||
logger.info("Mash tournament #{@mash_tournament.id} is done!")
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def new
|
||||
requester = Requester.find_or_initialize_by_ip(request.remote_ip)
|
||||
if requester.current_tournament && !requester.current_tournament.done?
|
||||
flash[:notice] = "But you haven't finished this one yet :-("
|
||||
redirect_to new_mash_path and return
|
||||
end
|
||||
|
||||
@mash_tournament = MashTournament.new(:requester => requester)
|
||||
requester.save
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
if not (requester = Requester.find_by_ip(request.remote_ip))
|
||||
flash[:notice] = "Not so fast..."
|
||||
redirect_to new_mash_tournament_path and return
|
||||
end
|
||||
if requester.current_tournament && !requester.current_tournament.done?
|
||||
flash[:notice] = "Seriously, you haven't finished this one yet! :-P"
|
||||
redirect_to new_mash_path and return
|
||||
end
|
||||
|
||||
@mash_tournament = MashTournament.new(:requester => requester)
|
||||
rounds = params.fetch(:mash_tournament, {}).fetch(:total_rounds, 3).to_i
|
||||
|
||||
respond_to do |format|
|
||||
action_new = lambda { format.html { render :action => 'new' } }
|
||||
|
||||
if @mash_tournament.save
|
||||
builder = @mash_tournament.builder
|
||||
if builder.create_rounds_for_contenders(2 ** rounds) == rounds and
|
||||
builder.fill_in_next_round.length == (2 ** rounds) / 2
|
||||
flash[:notice] = "Let's start mashing!"
|
||||
format.html do
|
||||
redirect_to :controller => 'mashes', :action => 'new'
|
||||
end
|
||||
else
|
||||
action_new.call
|
||||
end
|
||||
else
|
||||
action_new.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
54
map-mash/app/controllers/mashes_controller.rb
Normal file
54
map-mash/app/controllers/mashes_controller.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
class MashesController < ApplicationController
|
||||
def redirect_to_new
|
||||
flash[:notice] = 'Sneaky pete.'
|
||||
redirect_to(:action => 'new')
|
||||
end
|
||||
|
||||
alias_method :index, :redirect_to_new
|
||||
alias_method :show, :redirect_to_new
|
||||
|
||||
def new
|
||||
return if not already_registered?(request.remote_ip)
|
||||
|
||||
requester = Requester.find_by_ip(request.remote_ip)
|
||||
@mash = requester.current_tournament.next_unplayed_mash
|
||||
|
||||
if not @mash
|
||||
flash[:notice] = "You're done!"
|
||||
redirect_to requester.current_tournament, :action => 'show'
|
||||
return
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
return if not already_registered?(request.remote_ip)
|
||||
logger.info("Got params: #{params.inspect}")
|
||||
|
||||
@mash = Mash.find(params[:id])
|
||||
@mash.winner = Map.find((params[:mash][:winner_id] || -1).to_i)
|
||||
|
||||
logger.info("About to save #{@mash.inspect}")
|
||||
|
||||
respond_to do |format|
|
||||
if @mash.save
|
||||
flash[:notice] = 'Mashed!'
|
||||
format.html { redirect_to(:action => 'new') }
|
||||
else
|
||||
format.html { render :action => 'new' }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def already_registered?(ip)
|
||||
if not Requester.find_by_ip(ip)
|
||||
redirect_to :controller => :mash_tournaments, :action => 'new'
|
||||
return false
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
end
|
3
map-mash/app/helpers/application_helper.rb
Normal file
3
map-mash/app/helpers/application_helper.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
# Methods added to this helper will be available to all templates in the application.
|
||||
module ApplicationHelper
|
||||
end
|
2
map-mash/app/helpers/maps_helper.rb
Normal file
2
map-mash/app/helpers/maps_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module MapsHelper
|
||||
end
|
9
map-mash/app/helpers/mash_tournaments_helper.rb
Normal file
9
map-mash/app/helpers/mash_tournaments_helper.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module MashTournamentsHelper
|
||||
def total_rounds_options_for_select
|
||||
options = []
|
||||
@mash_tournament.total_rounds_options.each do |n|
|
||||
options << [n, n]
|
||||
end
|
||||
options
|
||||
end
|
||||
end
|
2
map-mash/app/helpers/mashes_helper.rb
Normal file
2
map-mash/app/helpers/mashes_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module MashesHelper
|
||||
end
|
67
map-mash/app/models/map.rb
Normal file
67
map-mash/app/models/map.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
require 'fastercsv'
|
||||
|
||||
|
||||
class Map < ActiveRecord::Base
|
||||
def self.from_city_name(city_name)
|
||||
self.find_or_initialize_by_name(city_name).save!
|
||||
end
|
||||
|
||||
def self.pair
|
||||
second = nil
|
||||
max_tries = self.count
|
||||
tries = 0
|
||||
|
||||
while second.nil? && tries < max_tries
|
||||
first = self.all(:order => 'RANDOM()', :limit => 1).first
|
||||
second = self.all(:order => 'RANDOM()', :limit => 1,
|
||||
:conditions => [%{
|
||||
(
|
||||
points >= :first_points
|
||||
AND id <> :first_id
|
||||
) OR (
|
||||
id NOT IN (
|
||||
SELECT map_a_id
|
||||
FROM mashes
|
||||
WHERE map_b_id = :first_id
|
||||
)
|
||||
AND id NOT IN (
|
||||
SELECT map_b_id
|
||||
FROM mashes
|
||||
WHERE map_a_id = :first_id
|
||||
)
|
||||
)}, {
|
||||
:first_id => first.id,
|
||||
:first_points => first.points
|
||||
}
|
||||
]
|
||||
).first
|
||||
tries += 1
|
||||
end
|
||||
|
||||
if second.nil?
|
||||
logger.error('OH CRAP thar be no more pairings for ye olde mashes')
|
||||
[first, first]
|
||||
else
|
||||
[first, second]
|
||||
end
|
||||
end
|
||||
|
||||
def self.import(csv_filename)
|
||||
FasterCSV.parse(open(csv_filename), :headers => true,
|
||||
:header_converters => [:downcase, :symbol]).each do |row|
|
||||
map = self.find_or_initialize_by_name("#{row[:city]}, #{row[:region]}")
|
||||
map.save
|
||||
if block_given?
|
||||
yield map
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.mashes
|
||||
Mash.all(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
||||
end
|
||||
|
||||
def rounds
|
||||
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
||||
end
|
||||
end
|
13
map-mash/app/models/mash.rb
Normal file
13
map-mash/app/models/mash.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Mash < ActiveRecord::Base
|
||||
belongs_to :map_a, :class_name => 'Map'
|
||||
belongs_to :map_b, :class_name => 'Map'
|
||||
belongs_to :winner, :class_name => 'Map'
|
||||
has_one :tournament, :class_name => 'MashTournament'
|
||||
has_one :round, :class_name => 'MashTournamentRound'
|
||||
|
||||
named_scope :unplayed, :conditions => [%{
|
||||
winner_id IS NULL
|
||||
AND map_a_id IS NOT NULL
|
||||
AND map_b_id IS NOT NULL
|
||||
}]
|
||||
end
|
39
map-mash/app/models/mash_tournament.rb
Normal file
39
map-mash/app/models/mash_tournament.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
class MashTournament < ActiveRecord::Base
|
||||
belongs_to :requester
|
||||
has_many :mashes
|
||||
has_many :rounds, :class_name => 'MashTournamentRound'
|
||||
|
||||
def next_unplayed_mash
|
||||
mash = self.mashes.unplayed.first
|
||||
if not mash
|
||||
filled = self.builder.fill_in_next_round
|
||||
if filled
|
||||
return self.mashes.unplayed.first
|
||||
else
|
||||
return nil
|
||||
end
|
||||
else
|
||||
return mash
|
||||
end
|
||||
end
|
||||
|
||||
def total_rounds_options
|
||||
@total_rounds_options ||= [3, 4, 5, 6, 7]
|
||||
end
|
||||
|
||||
def done?
|
||||
self.rounds.collect(&:done?).uniq == [true]
|
||||
end
|
||||
|
||||
def round(number = 0)
|
||||
MashTournamentRound.find_by_mash_tournament_id(self.id,
|
||||
:conditions => {:number => number}
|
||||
)
|
||||
end
|
||||
|
||||
def builder
|
||||
MashTournamentBuilder.new(
|
||||
self, MashTournament, MashTournamentRound, Map, Mash
|
||||
)
|
||||
end
|
||||
end
|
16
map-mash/app/models/mash_tournament_round.rb
Normal file
16
map-mash/app/models/mash_tournament_round.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class MashTournamentRound < ActiveRecord::Base
|
||||
belongs_to :tournament, :class_name => 'MashTournament'
|
||||
has_many :mashes
|
||||
|
||||
def self.for_round(tournament, round_number)
|
||||
self.find_by_mash_tournament_id_and_number(tournament, round_number)
|
||||
end
|
||||
|
||||
def done?
|
||||
winners = []
|
||||
self.mashes.collect do |mash|
|
||||
winners << mash.winner_id
|
||||
end
|
||||
!winners.include?(nil)
|
||||
end
|
||||
end
|
7
map-mash/app/models/requester.rb
Normal file
7
map-mash/app/models/requester.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class Requester < ActiveRecord::Base
|
||||
has_many :mash_tournaments
|
||||
|
||||
def current_tournament
|
||||
self.mash_tournaments.last
|
||||
end
|
||||
end
|
17
map-mash/app/views/layouts/mash_tournaments.html.erb
Normal file
17
map-mash/app/views/layouts/mash_tournaments.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||
<title>MashTournaments: <%= controller.action_name %></title>
|
||||
<%= stylesheet_link_tag 'scaffold' %>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p style="color: green"><%= flash[:notice] %></p>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
20
map-mash/app/views/layouts/mashes.html.erb
Normal file
20
map-mash/app/views/layouts/mashes.html.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||
<title>Map Mash</title>
|
||||
<%= stylesheet_link_tag 'styles' %>
|
||||
<%= stylesheet_link_tag 'scaffold' %>
|
||||
<%= javascript_include_tag 'jquery.min' %>
|
||||
<%= yield :head_js %>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p style="color: green"><%= flash[:notice] %></p>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
16
map-mash/app/views/maps/edit.html.erb
Normal file
16
map-mash/app/views/maps/edit.html.erb
Normal file
@@ -0,0 +1,16 @@
|
||||
<h1>Editing map</h1>
|
||||
|
||||
<% form_for(@map) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p>
|
||||
<%= f.label :name %><br />
|
||||
<%= f.text_field :name %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.submit 'Update' %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Show', @map %> |
|
||||
<%= link_to 'Back', maps_path %>
|
20
map-mash/app/views/maps/index.html.erb
Normal file
20
map-mash/app/views/maps/index.html.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<h1>Listing maps</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
</tr>
|
||||
|
||||
<% @maps.each do |map| %>
|
||||
<tr>
|
||||
<td><%=h map.name %></td>
|
||||
<td><%= link_to 'Show', map %></td>
|
||||
<td><%= link_to 'Edit', edit_map_path(map) %></td>
|
||||
<td><%= link_to 'Destroy', map, :confirm => 'Are you sure?', :method => :delete %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New map', new_map_path %>
|
15
map-mash/app/views/maps/new.html.erb
Normal file
15
map-mash/app/views/maps/new.html.erb
Normal file
@@ -0,0 +1,15 @@
|
||||
<h1>New map</h1>
|
||||
|
||||
<% form_for(@map) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p>
|
||||
<%= f.label :name %><br />
|
||||
<%= f.text_field :name %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.submit 'Create' %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Back', maps_path %>
|
8
map-mash/app/views/maps/show.html.erb
Normal file
8
map-mash/app/views/maps/show.html.erb
Normal file
@@ -0,0 +1,8 @@
|
||||
<p>
|
||||
<b>Name:</b>
|
||||
<%=h @map.name %>
|
||||
</p>
|
||||
|
||||
|
||||
<%= link_to 'Edit', edit_map_path(@map) %> |
|
||||
<%= link_to 'Back', maps_path %>
|
12
map-mash/app/views/mash_tournaments/edit.html.erb
Normal file
12
map-mash/app/views/mash_tournaments/edit.html.erb
Normal file
@@ -0,0 +1,12 @@
|
||||
<h1>Editing mash_tournament</h1>
|
||||
|
||||
<% form_for(@mash_tournament) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p>
|
||||
<%= f.submit 'Update' %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Show', @mash_tournament %> |
|
||||
<%= link_to 'Back', mash_tournaments_path %>
|
18
map-mash/app/views/mash_tournaments/index.html.erb
Normal file
18
map-mash/app/views/mash_tournaments/index.html.erb
Normal file
@@ -0,0 +1,18 @@
|
||||
<h1>Listing mash_tournaments</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
</tr>
|
||||
|
||||
<% @mash_tournaments.each do |mash_tournament| %>
|
||||
<tr>
|
||||
<td><%= link_to 'Show', mash_tournament %></td>
|
||||
<td><%= link_to 'Edit', edit_mash_tournament_path(mash_tournament) %></td>
|
||||
<td><%= link_to 'Destroy', mash_tournament, :confirm => 'Are you sure?', :method => :delete %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New mash_tournament', new_mash_tournament_path %>
|
9
map-mash/app/views/mash_tournaments/new.html.haml
Normal file
9
map-mash/app/views/mash_tournaments/new.html.haml
Normal file
@@ -0,0 +1,9 @@
|
||||
%h1 New Map Mash tournament!
|
||||
|
||||
- form_for(@mash_tournament) do |f|
|
||||
= f.error_messages
|
||||
|
||||
= f.label(:total_rounds)
|
||||
= f.select(:total_rounds, total_rounds_options_for_select)
|
||||
%p
|
||||
= f.submit 'Start'
|
15
map-mash/app/views/mash_tournaments/show.html.haml
Normal file
15
map-mash/app/views/mash_tournaments/show.html.haml
Normal file
@@ -0,0 +1,15 @@
|
||||
%h1
|
||||
Tournament #{@mash_tournament.id} Complete! You chose
|
||||
%em #{@mash_tournament.rounds.reverse.first.mashes.first.winner.name}!
|
||||
|
||||
- @mash_tournament.rounds.reverse.each do |round|
|
||||
%h2 Round #{round.number}
|
||||
- round.mashes.each do |mash|
|
||||
%ul
|
||||
- if mash.map_a.present? && mash.map_b.present? && mash.winner.present?
|
||||
%li
|
||||
#{mash.map_a.name} vs. #{mash.map_b.name}, winner =
|
||||
%strong #{mash.winner.name}
|
||||
|
||||
|
||||
= link_to 'New!', :controller => 'mash_tournaments', :action => 'new'
|
22
map-mash/app/views/mashes/new.html.haml
Normal file
22
map-mash/app/views/mashes/new.html.haml
Normal file
@@ -0,0 +1,22 @@
|
||||
- content_for :head_js do
|
||||
:javascript
|
||||
$(function() {
|
||||
$('.mash-image').click(function(elem) {
|
||||
$('#mash_winner_id').val($(elem.target).attr('data-map-id'));
|
||||
$('form[id^="edit_mash_"]').submit();
|
||||
});
|
||||
});
|
||||
|
||||
%h1 Mash the Maps!
|
||||
%p Choose your fave.
|
||||
|
||||
- form_for(@mash) do |f|
|
||||
= f.error_messages
|
||||
|
||||
#maps
|
||||
#map_a.mash-image
|
||||
%image{:src => "#{url_for @mash.map_a}.png", :'data-map-id' => @mash.map_a.id}
|
||||
#map_b.mash-image
|
||||
%image{:src => "#{url_for @mash.map_b}.png", :'data-map-id' => @mash.map_b.id}
|
||||
|
||||
= f.hidden_field :winner_id, :value => '?'
|
Reference in New Issue
Block a user