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 class MapsController < ApplicationController
# GET /maps
# GET /maps.xml
def index def index
@maps = Map.all @maps = Map.all
respond_to do |format| respond_to do |format|
format.html # index.html.erb format.html
format.xml { render :xml => @maps } format.xml { render :xml => @maps }
end end
end end
@ -27,66 +25,4 @@ class MapsController < ApplicationController
end end
end 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 end

View File

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

View File

@ -2,42 +2,22 @@ require 'fastercsv'
class Map < ActiveRecord::Base 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) def self.from_city_name(city_name)
self.find_or_initialize_by_name(city_name).save! self.find_or_initialize_by_name(city_name).save!
end end
def self.pair def self.pair
if Mash.count == 0 if Mash.count == 0
self.all(:order => 'RANDOM()', :limit => count) self.all(:order => 'RANDOM()', :limit => 2)
else else
first_conditions = <<-SQL self.lowest_scoring.all(:order => 'RANDOM()', :limit => 2)
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]
end end
end end
@ -54,23 +34,7 @@ class Map < ActiveRecord::Base
end end
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 def rounds
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}]) Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end end
def points
Mash.count(:conditions => ['winner = ?', self.id])
end
end end

View File

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

View File

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

View File

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

View File

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