Actually mashing maps now, but not handling form post-back or making it pretty

This commit is contained in:
Dan Buch 2012-03-07 09:32:33 -05:00
parent dd73309ed1
commit b1e17869b1
22 changed files with 447 additions and 42 deletions

View File

@ -3,3 +3,4 @@ tmp/*
db/*.sqlite3 db/*.sqlite3
config/redis.conf config/redis.conf
db/*.sqlite3-journal db/*.sqlite3-journal
public/maps/*.png

View File

@ -0,0 +1,92 @@
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 }
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)
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

View File

@ -19,13 +19,13 @@ class MashesController < ApplicationController
end end
def new def new
map_a, map_b = Map.rand(2) @map_a, @map_b = Map.rand(2)
@mash = Mash.new( @mash = Mash.new(
:requestor => request.remote_ip, :requester => request.remote_ip,
:map_a => map_a.id, :map_a => @map_a.id,
:map_b => map_b.id, :map_b => @map_b.id,
:winner => 0 :winner => 'a'
) )
respond_to do |format| respond_to do |format|

View File

@ -0,0 +1,2 @@
module MapsHelper
end

View 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>Maps: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>

View 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 %>

View 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 %>

View 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 %>

View 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 %>

View File

@ -4,16 +4,14 @@
<%= f.error_messages %> <%= f.error_messages %>
<p> <p>
<%= f.label :requester %><br /> <div id="map_a">
<%= f.text_field :requester %> <%= f.label :map_a %><br />
</p> <image src="<%= url_for @map_a %>.png" />
<p> </div>
<%= f.label :map_a %><br /> <div id="map_b">
<%= f.text_field :map_a %> <%= f.label :map_b %><br />
</p> <image src="<%= url_for @map_b %>.png" />
<p> </div>
<%= f.label :map_b %><br />
<%= f.text_field :map_b %>
</p> </p>
<p> <p>
<%= f.label :winner %><br /> <%= f.label :winner %><br />

View File

@ -3,3 +3,5 @@
# Add new mime types for use in respond_to blocks: # Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf # Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone # Mime::Type.register_alias "text/html", :iphone
Mime::Type.register 'image/png', :png

View File

@ -1,3 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :maps
map.resources :mashes map.resources :mashes
map.root :controller => 'mashes', :action => 'index'
end end

View File

@ -0,0 +1,126 @@
require 'spec_helper'
describe MapsController do
def mock_map(stubs={})
@mock_map ||= mock_model(Map, stubs)
end
describe "GET index" do
it "assigns all maps as @maps" do
Map.stub(:find).with(:all).and_return([mock_map])
get :index
assigns[:maps].should == [mock_map]
end
end
describe "GET show" do
end
describe "GET new" do
it "assigns a new map as @map" do
Map.stub(:new).and_return(mock_map)
get :new
assigns[:map].should equal(mock_map)
end
end
describe "GET edit" do
it "assigns the requested map as @map" do
Map.stub(:find).with("37").and_return(mock_map)
get :edit, :id => "37"
assigns[:map].should equal(mock_map)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created map as @map" do
Map.stub(:new).with({'these' => 'params'}).and_return(mock_map(:save => true))
post :create, :map => {:these => 'params'}
assigns[:map].should equal(mock_map)
end
it "redirects to the created map" do
Map.stub(:new).and_return(mock_map(:save => true))
post :create, :map => {}
response.should redirect_to(map_url(mock_map))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved map as @map" do
Map.stub(:new).with({'these' => 'params'}).and_return(mock_map(:save => false))
post :create, :map => {:these => 'params'}
assigns[:map].should equal(mock_map)
end
it "re-renders the 'new' template" do
Map.stub(:new).and_return(mock_map(:save => false))
post :create, :map => {}
response.should render_template('new')
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :map => {:these => 'params'}
end
it "assigns the requested map as @map" do
Map.stub(:find).and_return(mock_map(:update_attributes => true))
put :update, :id => "1"
assigns[:map].should equal(mock_map)
end
it "redirects to the map" do
Map.stub(:find).and_return(mock_map(:update_attributes => true))
put :update, :id => "1"
response.should redirect_to(map_url(mock_map))
end
end
describe "with invalid params" do
it "updates the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :map => {:these => 'params'}
end
it "assigns the map as @map" do
Map.stub(:find).and_return(mock_map(:update_attributes => false))
put :update, :id => "1"
assigns[:map].should equal(mock_map)
end
it "re-renders the 'edit' template" do
Map.stub(:find).and_return(mock_map(:update_attributes => false))
put :update, :id => "1"
response.should render_template('edit')
end
end
end
describe "DELETE destroy" do
it "destroys the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the maps list" do
Map.stub(:find).and_return(mock_map(:destroy => true))
delete :destroy, :id => "1"
response.should redirect_to(maps_url)
end
end
end

7
rails/map-mash/spec/fixtures/maps.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
name: MyString
two:
name: MyString

View File

@ -0,0 +1,11 @@
require 'spec_helper'
describe MapsHelper do
#Delete this example and add some real ones or delete this file
it "is included in the helper object" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(MapsHelper)
end
end

View File

@ -0,0 +1,4 @@
require 'spec_helper'
describe "Maps" do
end

View File

@ -0,0 +1,33 @@
require 'spec_helper'
describe MapsController do
describe "routing" do
it "recognizes and generates #index" do
{ :get => "/maps" }.should route_to(:controller => "maps", :action => "index")
end
it "recognizes and generates #new" do
{ :get => "/maps/new" }.should route_to(:controller => "maps", :action => "new")
end
it "recognizes and generates #show" do
{ :get => "/maps/1" }.should route_to(:controller => "maps", :action => "show", :id => "1")
end
it "recognizes and generates #edit" do
{ :get => "/maps/1/edit" }.should route_to(:controller => "maps", :action => "edit", :id => "1")
end
it "recognizes and generates #create" do
{ :post => "/maps" }.should route_to(:controller => "maps", :action => "create")
end
it "recognizes and generates #update" do
{ :put => "/maps/1" }.should route_to(:controller => "maps", :action => "update", :id => "1")
end
it "recognizes and generates #destroy" do
{ :delete => "/maps/1" }.should route_to(:controller => "maps", :action => "destroy", :id => "1")
end
end
end

View File

@ -0,0 +1,20 @@
require 'spec_helper'
describe "/maps/edit.html.erb" do
include MapsHelper
before(:each) do
assigns[:map] = @map = stub_model(Map,
:new_record? => false,
:name => "value for name"
)
end
it "renders the edit map form" do
render
response.should have_tag("form[action=#{map_path(@map)}][method=post]") do
with_tag('input#map_name[name=?]', "map[name]")
end
end
end

View File

@ -0,0 +1,21 @@
require 'spec_helper'
describe "/maps/index.html.erb" do
include MapsHelper
before(:each) do
assigns[:maps] = [
stub_model(Map,
:name => "value for name"
),
stub_model(Map,
:name => "value for name"
)
]
end
it "renders a list of maps" do
render
response.should have_tag("tr>td", "value for name".to_s, 2)
end
end

View File

@ -0,0 +1,20 @@
require 'spec_helper'
describe "/maps/new.html.erb" do
include MapsHelper
before(:each) do
assigns[:map] = stub_model(Map,
:new_record? => true,
:name => "value for name"
)
end
it "renders new map form" do
render
response.should have_tag("form[action=?][method=post]", maps_path) do
with_tag("input#map_name[name=?]", "map[name]")
end
end
end

View File

@ -0,0 +1,15 @@
require 'spec_helper'
describe "/maps/show.html.erb" do
include MapsHelper
before(:each) do
assigns[:map] = @map = stub_model(Map,
:name => "value for name"
)
end
it "renders attributes in <p>" do
render
response.should have_text(/value\ for\ name/)
end
end

View File

@ -1,26 +0,0 @@
require 'spec_helper'
describe "/mashes/new.html.erb" do
include MashesHelper
before(:each) do
assigns[:mash] = stub_model(Mash,
:new_record? => true,
:requester => "value for requester",
:map_a => 1,
:map_b => 1,
:winner => 1
)
end
it "renders new mash form" do
render
response.should have_tag("form[action=?][method=post]", mashes_path) do
with_tag("input#mash_requester[name=?]", "mash[requester]")
with_tag("input#mash_map_a[name=?]", "mash[map_a]")
with_tag("input#mash_map_b[name=?]", "mash[map_b]")
with_tag("input#mash_winner[name=?]", "mash[winner]")
end
end
end