From b1e17869b14e996e43c23a4b5de62592fa951410 Mon Sep 17 00:00:00 2001
From: Dan Buch
Date: Wed, 7 Mar 2012 09:32:33 -0500
Subject: [PATCH] Actually mashing maps now, but not handling form post-back or
making it pretty
---
rails/map-mash/.gitignore | 1 +
.../app/controllers/maps_controller.rb | 92 +++++++++++++
.../app/controllers/mashes_controller.rb | 10 +-
rails/map-mash/app/helpers/maps_helper.rb | 2 +
.../map-mash/app/views/layouts/maps.html.erb | 17 +++
rails/map-mash/app/views/maps/edit.html.erb | 16 +++
rails/map-mash/app/views/maps/index.html.erb | 20 +++
rails/map-mash/app/views/maps/new.html.erb | 15 +++
rails/map-mash/app/views/maps/show.html.erb | 8 ++
rails/map-mash/app/views/mashes/new.html.erb | 20 ++-
.../config/initializers/mime_types.rb | 2 +
rails/map-mash/config/routes.rb | 3 +
.../spec/controllers/maps_controller_spec.rb | 126 ++++++++++++++++++
rails/map-mash/spec/fixtures/maps.yml | 7 +
.../map-mash/spec/helpers/maps_helper_spec.rb | 11 ++
rails/map-mash/spec/integration/maps_spec.rb | 4 +
.../spec/routing/maps_routing_spec.rb | 33 +++++
.../spec/views/maps/edit.html.erb_spec.rb | 20 +++
.../spec/views/maps/index.html.erb_spec.rb | 21 +++
.../spec/views/maps/new.html.erb_spec.rb | 20 +++
.../spec/views/maps/show.html.erb_spec.rb | 15 +++
.../spec/views/mashes/new.html.erb_spec.rb | 26 ----
22 files changed, 447 insertions(+), 42 deletions(-)
create mode 100644 rails/map-mash/app/controllers/maps_controller.rb
create mode 100644 rails/map-mash/app/helpers/maps_helper.rb
create mode 100644 rails/map-mash/app/views/layouts/maps.html.erb
create mode 100644 rails/map-mash/app/views/maps/edit.html.erb
create mode 100644 rails/map-mash/app/views/maps/index.html.erb
create mode 100644 rails/map-mash/app/views/maps/new.html.erb
create mode 100644 rails/map-mash/app/views/maps/show.html.erb
create mode 100644 rails/map-mash/spec/controllers/maps_controller_spec.rb
create mode 100644 rails/map-mash/spec/fixtures/maps.yml
create mode 100644 rails/map-mash/spec/helpers/maps_helper_spec.rb
create mode 100644 rails/map-mash/spec/integration/maps_spec.rb
create mode 100644 rails/map-mash/spec/routing/maps_routing_spec.rb
create mode 100644 rails/map-mash/spec/views/maps/edit.html.erb_spec.rb
create mode 100644 rails/map-mash/spec/views/maps/index.html.erb_spec.rb
create mode 100644 rails/map-mash/spec/views/maps/new.html.erb_spec.rb
create mode 100644 rails/map-mash/spec/views/maps/show.html.erb_spec.rb
delete mode 100644 rails/map-mash/spec/views/mashes/new.html.erb_spec.rb
diff --git a/rails/map-mash/.gitignore b/rails/map-mash/.gitignore
index 6dc2924..375b6f4 100644
--- a/rails/map-mash/.gitignore
+++ b/rails/map-mash/.gitignore
@@ -3,3 +3,4 @@ tmp/*
db/*.sqlite3
config/redis.conf
db/*.sqlite3-journal
+public/maps/*.png
diff --git a/rails/map-mash/app/controllers/maps_controller.rb b/rails/map-mash/app/controllers/maps_controller.rb
new file mode 100644
index 0000000..87a266f
--- /dev/null
+++ b/rails/map-mash/app/controllers/maps_controller.rb
@@ -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
diff --git a/rails/map-mash/app/controllers/mashes_controller.rb b/rails/map-mash/app/controllers/mashes_controller.rb
index 6fce0a8..76a9dc6 100644
--- a/rails/map-mash/app/controllers/mashes_controller.rb
+++ b/rails/map-mash/app/controllers/mashes_controller.rb
@@ -19,13 +19,13 @@ class MashesController < ApplicationController
end
def new
- map_a, map_b = Map.rand(2)
+ @map_a, @map_b = Map.rand(2)
@mash = Mash.new(
- :requestor => request.remote_ip,
- :map_a => map_a.id,
- :map_b => map_b.id,
- :winner => 0
+ :requester => request.remote_ip,
+ :map_a => @map_a.id,
+ :map_b => @map_b.id,
+ :winner => 'a'
)
respond_to do |format|
diff --git a/rails/map-mash/app/helpers/maps_helper.rb b/rails/map-mash/app/helpers/maps_helper.rb
new file mode 100644
index 0000000..88ee3d5
--- /dev/null
+++ b/rails/map-mash/app/helpers/maps_helper.rb
@@ -0,0 +1,2 @@
+module MapsHelper
+end
diff --git a/rails/map-mash/app/views/layouts/maps.html.erb b/rails/map-mash/app/views/layouts/maps.html.erb
new file mode 100644
index 0000000..c96c456
--- /dev/null
+++ b/rails/map-mash/app/views/layouts/maps.html.erb
@@ -0,0 +1,17 @@
+
+
+
+
+
+ Maps: <%= controller.action_name %>
+ <%= stylesheet_link_tag 'scaffold' %>
+
+
+
+<%= flash[:notice] %>
+
+<%= yield %>
+
+
+
diff --git a/rails/map-mash/app/views/maps/edit.html.erb b/rails/map-mash/app/views/maps/edit.html.erb
new file mode 100644
index 0000000..2dc0b23
--- /dev/null
+++ b/rails/map-mash/app/views/maps/edit.html.erb
@@ -0,0 +1,16 @@
+Editing map
+
+<% form_for(@map) do |f| %>
+ <%= f.error_messages %>
+
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+
+ <%= f.submit 'Update' %>
+
+<% end %>
+
+<%= link_to 'Show', @map %> |
+<%= link_to 'Back', maps_path %>
\ No newline at end of file
diff --git a/rails/map-mash/app/views/maps/index.html.erb b/rails/map-mash/app/views/maps/index.html.erb
new file mode 100644
index 0000000..847a526
--- /dev/null
+++ b/rails/map-mash/app/views/maps/index.html.erb
@@ -0,0 +1,20 @@
+Listing maps
+
+
+
+ Name |
+
+
+<% @maps.each do |map| %>
+
+ <%=h map.name %> |
+ <%= link_to 'Show', map %> |
+ <%= link_to 'Edit', edit_map_path(map) %> |
+ <%= link_to 'Destroy', map, :confirm => 'Are you sure?', :method => :delete %> |
+
+<% end %>
+
+
+
+
+<%= link_to 'New map', new_map_path %>
\ No newline at end of file
diff --git a/rails/map-mash/app/views/maps/new.html.erb b/rails/map-mash/app/views/maps/new.html.erb
new file mode 100644
index 0000000..838745b
--- /dev/null
+++ b/rails/map-mash/app/views/maps/new.html.erb
@@ -0,0 +1,15 @@
+New map
+
+<% form_for(@map) do |f| %>
+ <%= f.error_messages %>
+
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+
+ <%= f.submit 'Create' %>
+
+<% end %>
+
+<%= link_to 'Back', maps_path %>
\ No newline at end of file
diff --git a/rails/map-mash/app/views/maps/show.html.erb b/rails/map-mash/app/views/maps/show.html.erb
new file mode 100644
index 0000000..4210382
--- /dev/null
+++ b/rails/map-mash/app/views/maps/show.html.erb
@@ -0,0 +1,8 @@
+
+ Name:
+ <%=h @map.name %>
+
+
+
+<%= link_to 'Edit', edit_map_path(@map) %> |
+<%= link_to 'Back', maps_path %>
\ No newline at end of file
diff --git a/rails/map-mash/app/views/mashes/new.html.erb b/rails/map-mash/app/views/mashes/new.html.erb
index 3269278..7dcddce 100644
--- a/rails/map-mash/app/views/mashes/new.html.erb
+++ b/rails/map-mash/app/views/mashes/new.html.erb
@@ -4,16 +4,14 @@
<%= f.error_messages %>
- <%= f.label :requester %>
- <%= f.text_field :requester %>
-
-
- <%= f.label :map_a %>
- <%= f.text_field :map_a %>
-
-
- <%= f.label :map_b %>
- <%= f.text_field :map_b %>
+
+ <%= f.label :map_a %>
+
+
+
+ <%= f.label :map_b %>
+
+
<%= f.label :winner %>
@@ -24,4 +22,4 @@
<% end %>
-<%= link_to 'Back', mashes_path %>
\ No newline at end of file
+<%= link_to 'Back', mashes_path %>
diff --git a/rails/map-mash/config/initializers/mime_types.rb b/rails/map-mash/config/initializers/mime_types.rb
index 72aca7e..71306b9 100644
--- a/rails/map-mash/config/initializers/mime_types.rb
+++ b/rails/map-mash/config/initializers/mime_types.rb
@@ -3,3 +3,5 @@
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
+
+Mime::Type.register 'image/png', :png
diff --git a/rails/map-mash/config/routes.rb b/rails/map-mash/config/routes.rb
index 395094d..f84762d 100644
--- a/rails/map-mash/config/routes.rb
+++ b/rails/map-mash/config/routes.rb
@@ -1,3 +1,6 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :maps
+
map.resources :mashes
+ map.root :controller => 'mashes', :action => 'index'
end
diff --git a/rails/map-mash/spec/controllers/maps_controller_spec.rb b/rails/map-mash/spec/controllers/maps_controller_spec.rb
new file mode 100644
index 0000000..55def04
--- /dev/null
+++ b/rails/map-mash/spec/controllers/maps_controller_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/fixtures/maps.yml b/rails/map-mash/spec/fixtures/maps.yml
new file mode 100644
index 0000000..157d747
--- /dev/null
+++ b/rails/map-mash/spec/fixtures/maps.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
diff --git a/rails/map-mash/spec/helpers/maps_helper_spec.rb b/rails/map-mash/spec/helpers/maps_helper_spec.rb
new file mode 100644
index 0000000..3920ac7
--- /dev/null
+++ b/rails/map-mash/spec/helpers/maps_helper_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/integration/maps_spec.rb b/rails/map-mash/spec/integration/maps_spec.rb
new file mode 100644
index 0000000..fb05cdd
--- /dev/null
+++ b/rails/map-mash/spec/integration/maps_spec.rb
@@ -0,0 +1,4 @@
+require 'spec_helper'
+
+describe "Maps" do
+end
diff --git a/rails/map-mash/spec/routing/maps_routing_spec.rb b/rails/map-mash/spec/routing/maps_routing_spec.rb
new file mode 100644
index 0000000..08f29dd
--- /dev/null
+++ b/rails/map-mash/spec/routing/maps_routing_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/views/maps/edit.html.erb_spec.rb b/rails/map-mash/spec/views/maps/edit.html.erb_spec.rb
new file mode 100644
index 0000000..a0c10d5
--- /dev/null
+++ b/rails/map-mash/spec/views/maps/edit.html.erb_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/views/maps/index.html.erb_spec.rb b/rails/map-mash/spec/views/maps/index.html.erb_spec.rb
new file mode 100644
index 0000000..e4257a5
--- /dev/null
+++ b/rails/map-mash/spec/views/maps/index.html.erb_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/views/maps/new.html.erb_spec.rb b/rails/map-mash/spec/views/maps/new.html.erb_spec.rb
new file mode 100644
index 0000000..60583ae
--- /dev/null
+++ b/rails/map-mash/spec/views/maps/new.html.erb_spec.rb
@@ -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
diff --git a/rails/map-mash/spec/views/maps/show.html.erb_spec.rb b/rails/map-mash/spec/views/maps/show.html.erb_spec.rb
new file mode 100644
index 0000000..da657ff
--- /dev/null
+++ b/rails/map-mash/spec/views/maps/show.html.erb_spec.rb
@@ -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 " do
+ render
+ response.should have_text(/value\ for\ name/)
+ end
+end
diff --git a/rails/map-mash/spec/views/mashes/new.html.erb_spec.rb b/rails/map-mash/spec/views/mashes/new.html.erb_spec.rb
deleted file mode 100644
index 8ce3be3..0000000
--- a/rails/map-mash/spec/views/mashes/new.html.erb_spec.rb
+++ /dev/null
@@ -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