A bit cleaner way of selecting map pairs, bunch of other cleanup crap generally characterized as removing generated Rails code

This commit is contained in:
Dan Buch 2012-03-07 23:43:32 -05:00
parent b31926f16f
commit d7f0f6c964
7 changed files with 54 additions and 147 deletions

View File

@ -1,12 +1,10 @@
class MapsController < ApplicationController
# GET /maps
# GET /maps.xml
def index
@maps = Map.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @maps }
format.html
format.xml { render :xml => @maps }
end
end
@ -27,66 +25,4 @@ class MapsController < ApplicationController
end
end
end
# GET /maps/new
# GET /maps/new.xml
def new
@map = Map.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @map }
end
end
# GET /maps/1/edit
def edit
@map = Map.find(params[:id])
end
# POST /maps
# POST /maps.xml
def create
@map = Map.new(params[:map])
respond_to do |format|
if @map.save
flash[:notice] = 'Map was successfully created.'
format.html { redirect_to(@map) }
format.xml { render :xml => @map, :status => :created, :location => @map }
else
format.html { render :action => "new" }
format.xml { render :xml => @map.errors, :status => :unprocessable_entity }
end
end
end
# PUT /maps/1
# PUT /maps/1.xml
def update
@map = Map.find(params[:id])
respond_to do |format|
if @map.update_attributes(params[:map])
flash[:notice] = 'Map was successfully updated.'
format.html { redirect_to(@map) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @map.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /maps/1
# DELETE /maps/1.xml
def destroy
@map = Map.find(params[:id])
@map.destroy
respond_to do |format|
format.html { redirect_to(maps_url) }
format.xml { head :ok }
end
end
end

View File

@ -19,7 +19,6 @@ class MashesController < ApplicationController
respond_to do |format|
format.html
format.xml { render :xml => @mash }
end
end
@ -28,15 +27,15 @@ class MashesController < ApplicationController
mash_params[:requester] = request.remote_ip
logger.info("Got mash params: #{mash_params.inspect}")
@mash = Mash.new(mash_params)
@winner = Map.find(@mash.winner)
@winner.points += 1
respond_to do |format|
if @mash.save
if @mash.save && @winner.save
flash[:notice] = 'Mash was successfully created.'
format.html { redirect_to(:action => 'new') }
format.xml { render :xml => @mash, :status => :created, :location => @mash }
else
format.html { render :action => 'new' }
format.xml { render :xml => @mash.errors, :status => :unprocessable_entity }
end
end
end

View File

@ -2,42 +2,22 @@ require 'fastercsv'
class Map < ActiveRecord::Base
named_scope :lowest_scoring, :conditions => [
%{`maps`.`points` = (
SELECT `points` FROM `maps`
ORDER BY `points` ASC
LIMIT 1
)}]
def self.from_city_name(city_name)
self.find_or_initialize_by_name(city_name).save!
end
def self.pair
if Mash.count == 0
self.all(:order => 'RANDOM()', :limit => count)
self.all(:order => 'RANDOM()', :limit => 2)
else
first_conditions = <<-SQL
NOT EXISTS (
SELECT * FROM `mashes`
WHERE `winner` = `maps`.`id`
)
SQL
second_conditions = <<-SQL
`maps`.`id` != :first_id
AND (
SELECT COUNT(*) FROM `mashes`
WHERE `winner` = :first_id
) = (
SELECT COUNT(*) FROM `mashes`
WHERE `winner` = `maps`.`id`
)
SQL
first = self.all(
:order => 'RANDOM()',
:limit => 1,
:conditions => [first_conditions]
).first
second = self.all(
:order => 'RANDOM()',
:limit => 1,
:conditions => [second_conditions, {:first_id => first.id}]
).first
[first, second]
self.lowest_scoring.all(:order => 'RANDOM()', :limit => 2)
end
end
@ -54,23 +34,7 @@ class Map < ActiveRecord::Base
end
end
def >(other)
self.points > other.points
end
def <(other)
self.points < other.points
end
def ==(other)
self.points == other.points
end
def rounds
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end
def points
Mash.count(:conditions => ['winner = ?', self.id])
end
end

View File

@ -2,15 +2,18 @@ class CreateMaps < ActiveRecord::Migration
def self.up
create_table :maps do |t|
t.string :name, :null => false
t.integer :points, :default => 0
t.timestamps
end
add_index :maps, :name
add_index :maps, [:name]
add_index :maps, [:points]
end
def self.down
remove_index :maps, [:name]
remove_index :maps, [:points]
drop_table :maps
remove_index :maps, :name
end
end

View File

@ -9,11 +9,13 @@ class CreateMashes < ActiveRecord::Migration
t.timestamps
end
add_index :mashes, :winner
add_index :mashes, [:winner]
add_index :mashes, [:requester]
end
def self.down
remove_index :mashes, [:winner]
remove_index :mashes, [:requester]
drop_table :mashes
remove_index :mashes, :winner
end
end

View File

@ -12,12 +12,14 @@
ActiveRecord::Schema.define(:version => 20120304164625) do
create_table "maps", :force => true do |t|
t.string "name", :null => false
t.string "name", :null => false
t.integer "points", :default => 0
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "maps", ["name"], :name => "index_maps_on_name"
add_index "maps", ["points"], :name => "index_maps_on_points"
create_table "mashes", :force => true do |t|
t.string "requester", :null => false
@ -28,6 +30,7 @@ ActiveRecord::Schema.define(:version => 20120304164625) do
t.datetime "updated_at"
end
add_index "mashes", ["requester"], :name => "index_mashes_on_requester"
add_index "mashes", ["winner"], :name => "index_mashes_on_winner"
end

View File

@ -1,15 +1,15 @@
body { background-color: #fff; color: #333; }
body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
background-color: #eee;
padding: 10px;
font-size: 11px;
}
a { color: #000; }
@ -17,38 +17,38 @@ a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
.fieldWithErrors {
padding: 2px;
background-color: red;
display: table;
padding: 2px;
background-color: red;
display: table;
}
#errorExplanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}
#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}
#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
color: #333;
margin-bottom: 0;
padding: 5px;
}
#errorExplanation ul li {
font-size: 12px;
list-style: square;
font-size: 12px;
list-style: square;
}