Permalink
Browse files

filled in FavoriteLocationsController wireframes; added tests

  • Loading branch information...
1 parent b95d253 commit 736c9ece8ddf4d6488d8754f7bc2e925ce6daf1e @jamesarosen jamesarosen committed Jun 26, 2008
@@ -1,6 +1,7 @@
class FavoriteLocationsController < ApplicationController
requires_is_self
+ append_before_filter :require_favorite_location!, :only => [ :edit, :destroy, :update, :set_current ]
# GET only
def index
@@ -12,6 +13,8 @@ def new
# POST only
def create
+ @favorite_location = requested_profile.favorite_locations.build(favorite_location_params)
+ @favorite_location.save!
redirect_to :action => 'index'
end
@@ -21,12 +24,39 @@ def edit
# DELETE only
def destroy
+ requested_favorite_location.destroy
+ requested_profile.favorite_locations(true)
redirect_to :action => 'index'
end
# PUT only
def update
+ @favorite_location = requested_profile.favorite_locations.find_by_id(params[:favorite_location_id])
+ @favorite_location.update_attributes!(favorite_location_params)
+ @favorite_location.save!
+ flash[:notice] = "Favorite location #{@favorite_location.name} updated"
redirect_to :action => 'index'
end
+
+ def set_current
+ requested_profile.location = requested_favorite_location
+ requested_profile.save!
+ current_user.reload
+ redirect_to dashboard_path
+ end
+
+ private
+
+ def favorite_location_params
+ (params[:favorite_location] || {}).pass(:name, :location)
+ end
+
+ def requested_favorite_location(reload = false)
+ safe_load :@favorite_location, Location::Favorite, :favorite_location_id, reload
+ end
+
+ def require_favorite_location!
+ require_exists! requested_favorite_location, Location::Favorite, :favorite_location_id
+ end
end
@@ -27,14 +27,6 @@ def require_profile!
require_exists! requested_profile, Profile, :profile_id
end
- def requested_favorite_location(reload = false)
- @favorite_location = nil if reload
- @favorite_location ||= params[:favorite_location_id]
- end
- def require_favorite_location!
- end
-
-
def safe_load(variable_name, klass, parameter_name, reload)
begin
@@ -18,7 +18,8 @@ def self.included(base)
module InstanceMethods
- def initialize_with_parse_location(options = {})
+ def initialize_with_parse_location(options = nil)
+ options ||= {}
initialize_without_parse_location(options.block(:location))
self.location = options[:location]
end
View
@@ -8,6 +8,8 @@ class Profile < ActiveRecord::Base
belongs_to :user
+ has_many :favorite_locations, :class_name => 'Location::Favorite'
+
validates_presence_of :email
validates_email :message => 'is not a valid email address', :allow_blank => true
validates_length_of :email, :within => 3..100, :allow_blank => true#, :message => 'is not a valid email address'
@@ -0,0 +1,8 @@
+<% form_for :favorite_location, @favorite_location, :url => url, :html => { :method => method, :class => 'horizontal section' } do |f| %>
+
+ <%= f.text_field :name, :class => 'text focus' %>
+ <%= f.text_field :location, :class => 'text' %>
+
+ <%= submit_and_cancel_tags :submit => submit_text %>
+
+<% end %>
@@ -1 +1,7 @@
-<% title 'Edit Favorite Location' -%>
+<% title 'Edit Favorite Location' -%>
+
+<%= render :partial => '/favorite_locations/form',
+ :locals => {
+ :url => formatted_favorite_location_path(:formatted_location_id => requested_favorite_location),
+ :submit_text => 'Update',
+ :method => :put } -%>
@@ -1 +1,22 @@
-<% title 'Your Favorite Locations' -%>
+<% title 'Your Favorite Locations' -%>
+
+<p class='section'><%= link_to 'Add', formatted_new_favorite_location_path -%></p>
+
+<table class='section'>
+ <thead>
+ <tr><th>Name</th><th>Location</th><th>&nbsp;</th></tr>
+ </thead>
+ <tbody>
+ <% requested_profile.favorite_locations.each do |f| -%>
+ <tr>
+ <td><%= f.name -%></td>
+ <td><%= f.location -%></td>
+ <td>
+ <%= link_to 'Edit', formatted_edit_favorite_location_path(:favorite_location_id => f) -%>
+ --
+ <%= link_to 'There now', formatted_set_current_favorite_location_path(:favorite_location_id => f), :method => :post -%>
+ </td>
+ </tr>
+ <% end -%>
+ </tbody>
+</table>
@@ -1 +1,6 @@
-<% title 'Create a New Favorite Location' -%>
+<% title 'Create a New Favorite Location' -%>
+
+<%= render :partial => '/favorite_locations/form',
+ :locals => {
+ :url => formatted_favorite_locations_path, :submit_text => 'Add it!',
+ :method => :post } -%>
@@ -2,4 +2,5 @@
<ul>
<li><%= link_to 'Privacy', edit_privacy_settings_path(:profile_id => current_user.profile) -%></li>
<li><%= link_to 'Notifications', edit_notification_settings_path(:profile_id => current_user.profile) -%></li>
+ <li><%= link_to 'Favorite Locations', formatted_favorite_locations_path(:profile_id => current_user.profile) -%></li>
</ul>
@@ -1,6 +1,6 @@
<p class='section'>
<% if requested_profile.location.kind_of?(Location::Airport) -%>
- There is nobody else in <%= requested_profile.location -%> airport.
+ There is nobody else in <%= requested_profile.location -%>.
<% else -%>
There is nobody near <%= requested_profile.location -%>.
<% end -%>
@@ -8,5 +8,5 @@
<li><%= link_to 'Set your privacy preferences', edit_privacy_settings_path -%></li>
<li><%= link_to 'Edit your profile', edit_profile_path -%></li>
<li><%= link_to 'Set your location', edit_location_path -%></li>
- <li><%= link_to 'Add some favorite locations', new_favorite_location_path -%></li>
+ <li><%= link_to 'Add some favorite locations', formatted_new_favorite_location_path -%></li>
</ul>
View
@@ -81,7 +81,10 @@
end
end
- map.resources :favorite_locations, :path_prefix => '/people/:profile_id/location', :as => 'favorites'
+ map.resources :favorite_locations,
+ :path_prefix => '/people/:profile_id/location',
+ :as => 'favorites',
+ :member => { :set_current => :post }
map.with_options(:controller => 'settings') do |m|
m.get do |n|
@@ -40,30 +40,46 @@ class FavoriteLocationsControllerTest < ActionController::TestCase
should_be_allowed 'to see her own favorite locations' do
- get :index, :profile_id => current_user
+ get :index, :profile_id => current_profile
end
should_be_allowed 'to view the form to add a new favorite location' do
- get :new, :profile_id => current_user
+ get :new, :profile_id => current_profile
end
should 'be able to create a new favorite location' do
- post :create, :profile_id => current_user
- assert_redirected_to :action => 'index'
+ somewhere = Location::Base.find(:first)
+ assert_difference('current_profile.favorite_locations.size', 1) do
+ post :create, :profile_id => current_profile, :favorite_location => { :location => somewhere.address, :name => 'anything' }
+ assert_redirected_to :action => 'index'
+ end
+ end
+
+ should 'not be able to create a new favorite location for somebody else by hacking the parameters' do
+ other = somebody_other_than(current_user)
+ somewhere = Location::Base.find(:first)
+ assert_difference('other.profile.favorite_locations.size', 0) do
+ post :create, :profile_id => current_profile, :favorite_location => { :location => somewhere, :profile => other, :name => 'anything' }
+ end
end
should_be_allowed 'to view the form to edit an existing favorite location' do
- get :edit, :profile_id => current_user, :favorite_location_id => 1234
+ get :edit, :profile_id => current_profile, :favorite_location_id => current_profile.favorite_locations.first
end
should 'be able to update an existing favorite location' do
- put :update, :profile_id => current_user, :favorite_location_id => 1324
+ fave = current_profile.favorite_locations.first
+ assert_not_equal 'foo', fave.name
+ put :update, :profile_id => current_profile, :favorite_location_id => fave, :favorite_location => { :name => 'foo' }
assert_redirected_to :action => 'index'
+ assert_equal 'foo', fave.reload.name
end
should 'be able to delete an existing favorite location' do
- delete :destroy, :profile_id => current_user, :favorite_location_id => 1324
- assert_redirected_to :action => 'index'
+ assert_difference('current_profile.favorite_locations.size', -1) do
+ delete :destroy, :profile_id => current_profile, :favorite_location_id => current_profile.favorite_locations.first
+ assert_redirected_to :action => 'index'
+ end
end
end

0 comments on commit 736c9ec

Please sign in to comment.