Browse files

Applying current_user everywhere.

  • Loading branch information...
1 parent 366f51a commit 3989a019795edb17ee9fa30d32116f7137253b1f @phillipoertel phillipoertel committed Oct 12, 2011
View
14 app/controllers/favorite_places_controller.rb
@@ -2,7 +2,7 @@ class FavoritePlacesController < ApplicationController
# GET /favorite_places
# GET /favorite_places.json
def index
- @favorite_places = FavoritePlace.all
+ @favorite_places = current_user.favorite_places
respond_to do |format|
format.html # index.html.erb
@@ -13,7 +13,7 @@ def index
# GET /favorite_places/1
# GET /favorite_places/1.json
def show
- @favorite_place = FavoritePlace.find(params[:id])
+ @favorite_place = current_user.favorite_places.find(params[:id])
respond_to do |format|
format.html # show.html.erb
@@ -24,7 +24,7 @@ def show
# GET /favorite_places/new
# GET /favorite_places/new.json
def new
- @favorite_place = FavoritePlace.new
+ @favorite_place = current_user.favorite_places.new
respond_to do |format|
format.html # new.html.erb
@@ -34,13 +34,13 @@ def new
# GET /favorite_places/1/edit
def edit
- @favorite_place = FavoritePlace.find(params[:id])
+ @favorite_place = current_user.favorite_places.find(params[:id])
end
# POST /favorite_places
# POST /favorite_places.json
def create
- @favorite_place = FavoritePlace.new(params[:favorite_place])
+ @favorite_place = current_user.favorite_places.new(params[:favorite_place])
respond_to do |format|
if @favorite_place.save
@@ -56,7 +56,7 @@ def create
# PUT /favorite_places/1
# PUT /favorite_places/1.json
def update
- @favorite_place = FavoritePlace.find(params[:id])
+ @favorite_place = current_user.favorite_places.find(params[:id])
respond_to do |format|
if @favorite_place.update_attributes(params[:favorite_place])
@@ -72,7 +72,7 @@ def update
# DELETE /favorite_places/1
# DELETE /favorite_places/1.json
def destroy
- @favorite_place = FavoritePlace.find(params[:id])
+ @favorite_place = current_user.favorite_places.find(params[:id])
@favorite_place.destroy
respond_to do |format|
View
2 app/controllers/visits_controller.rb
@@ -1,6 +1,6 @@
class VisitsController < ApplicationController
def show
- @flats = Flat.have_visits
+ @flats = current_user.flats.have_visits
end
end
View
2 app/views/flats/_one_flat_map.html.erb
@@ -11,7 +11,7 @@
var geocoder = new google.maps.Geocoder();
centerMap(map, geocoder);
- placeMarkers(<%=raw FavoritePlace.all.to_json %>, 'green');
+ placeMarkers(<%=raw current_user.favorite_places.to_json %>, 'green');
placeMarkers([<%=raw flat.model.to_json %>], 'red');
}
</script>
View
2 app/views/flats/_overview_map.html.erb
@@ -17,7 +17,7 @@ function initialize() {
centerMap(map, geocoder);
- placeMarkers(<%=raw FavoritePlace.all.to_json %>, 'green');
+ placeMarkers(<%=raw current_user.favorite_places.to_json %>, 'green');
G_markers = placeMarkers(G_addresses, 'red');
View
38 spec/controllers/favorite_places_controller_spec.rb
@@ -20,26 +20,35 @@
describe FavoritePlacesController do
- before(:each) { sign_in Factory.create(:user) }
+ before(:all) { @user = Factory.create(:user) }
+ before(:each) { sign_in @user }
# This should return the minimal set of attributes required to create a valid
# FavoritePlace. As you add validations to FavoritePlace, be sure to
# update the return value of this method accordingly.
- def valid_attributes
- Factory.attributes_for(:favorite_place)
+ def valid_attributes(opts = {:with_user => false})
+ attributes = Factory.attributes_for(:favorite_place)
+ end
+
+ # valid_attributes is used for two things:
+ # 1) submitting params
+ # 2) creating a new record inside the test
+ # only supply the user for 2), otherwise tests doing 1) may work although the code doesn't
+ def valid_attributes_with_user_id
+ valid_attributes.merge(:user_id => @user.id)
end
describe "GET index" do
it "assigns all favorite_places as @favorite_places" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
get :index
assigns(:favorite_places).should eq([favorite_place])
end
end
describe "GET show" do
it "assigns the requested favorite_place as @favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
get :show, :id => favorite_place.id.to_s
assigns(:favorite_place).should eq(favorite_place)
end
@@ -54,7 +63,7 @@ def valid_attributes
describe "GET edit" do
it "assigns the requested favorite_place as @favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
get :edit, :id => favorite_place.id.to_s
assigns(:favorite_place).should eq(favorite_place)
end
@@ -65,7 +74,7 @@ def valid_attributes
it "creates a new FavoritePlace" do
expect {
post :create, :favorite_place => valid_attributes
- }.to change(FavoritePlace, :count).by(1)
+ }.to change(@user.favorite_places, :count).by(1)
end
it "assigns a newly created favorite_place as @favorite_place" do
@@ -100,7 +109,7 @@ def valid_attributes
describe "PUT update" do
describe "with valid params" do
it "updates the requested favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
# Assuming there are no other favorite_places in the database, this
# specifies that the FavoritePlace created on the previous line
# receives the :update_attributes message with whatever params are
@@ -110,29 +119,30 @@ def valid_attributes
end
it "assigns the requested favorite_place as @favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
put :update, :id => favorite_place.id, :favorite_place => valid_attributes
assigns(:favorite_place).should eq(favorite_place)
end
it "redirects to the favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ puts valid_attributes[:user].to_yaml
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
put :update, :id => favorite_place.id, :favorite_place => valid_attributes
response.should redirect_to(favorite_place)
end
end
describe "with invalid params" do
it "assigns the favorite_place as @favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
# Trigger the behavior that occurs when invalid params are submitted
FavoritePlace.any_instance.stub(:save).and_return(false)
put :update, :id => favorite_place.id.to_s, :favorite_place => {}
assigns(:favorite_place).should eq(favorite_place)
end
it "re-renders the 'edit' template" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
# Trigger the behavior that occurs when invalid params are submitted
FavoritePlace.any_instance.stub(:save).and_return(false)
put :update, :id => favorite_place.id.to_s, :favorite_place => {}
@@ -143,14 +153,14 @@ def valid_attributes
describe "DELETE destroy" do
it "destroys the requested favorite_place" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
expect {
delete :destroy, :id => favorite_place.id.to_s
}.to change(FavoritePlace, :count).by(-1)
end
it "redirects to the favorite_places list" do
- favorite_place = FavoritePlace.create! valid_attributes
+ favorite_place = FavoritePlace.create! valid_attributes_with_user_id
delete :destroy, :id => favorite_place.id.to_s
response.should redirect_to(favorite_places_url)
end
View
8 spec/requests/visits/view_visits_spec.rb → spec/requests/visits/show_visits_spec.rb
@@ -1,14 +1,14 @@
require 'spec_helper'
feature "Visits" do
-
+
background do
- @flat_passed = Factory.create(:flat, :visit_at => Time.now - 2.days)
- @flat_future = Factory.create(:flat, :visit_at => Time.now + 2.days)
+ user = sign_in_with_form
+ @flat_passed = Factory.create(:flat, :visit_at => Time.now - 2.days, :user => user)
+ @flat_future = Factory.create(:flat, :visit_at => Time.now + 2.days, :user => user)
end
scenario "It formats future/past visits correctly", :type => :smoke do
- sign_in_with_form
visit(visits_path)
page.should have_css('li.passed', :text => @flat_passed.street)
page.should have_css('li', :text => @flat_future.street)

0 comments on commit 3989a01

Please sign in to comment.