public
Description: A Location-Based social networking app
Homepage: http://github.com/gcnovus/pinball
Clone URL: git://github.com/gcnovus/pinball.git
filled in FavoriteLocationsController wireframes; added tests
gcnovus (author)
Thu Jun 26 09:09:52 -0700 2008
commit  736c9ece8ddf4d6488d8754f7bc2e925ce6daf1e
tree    a278aa41cf42248acc6dfa357e869f8530f7b824
parent  b95d253253f6d964dcc7bbad6c8a739290b6bf61
...
1
2
3
 
4
5
6
...
12
13
14
 
 
15
16
17
...
21
22
23
 
 
24
25
26
27
28
 
 
 
 
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
...
1
2
3
4
5
6
7
...
13
14
15
16
17
18
19
20
...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -1,6 +1,7 @@
0
 class FavoriteLocationsController < ApplicationController
0
 
0
   requires_is_self
0
+ append_before_filter :require_favorite_location!, :only => [ :edit, :destroy, :update, :set_current ]
0
   
0
   # GET only
0
   def index
0
@@ -12,6 +13,8 @@ class FavoriteLocationsController < ApplicationController
0
   
0
   # POST only
0
   def create
0
+ @favorite_location = requested_profile.favorite_locations.build(favorite_location_params)
0
+ @favorite_location.save!
0
     redirect_to :action => 'index'
0
   end
0
 
0
@@ -21,12 +24,39 @@ class FavoriteLocationsController < ApplicationController
0
 
0
   # DELETE only
0
   def destroy
0
+ requested_favorite_location.destroy
0
+ requested_profile.favorite_locations(true)
0
     redirect_to :action => 'index'
0
   end
0
 
0
   # PUT only
0
   def update
0
+ @favorite_location = requested_profile.favorite_locations.find_by_id(params[:favorite_location_id])
0
+ @favorite_location.update_attributes!(favorite_location_params)
0
+ @favorite_location.save!
0
+ flash[:notice] = "Favorite location #{@favorite_location.name} updated"
0
     redirect_to :action => 'index'
0
   end
0
+
0
+ def set_current
0
+ requested_profile.location = requested_favorite_location
0
+ requested_profile.save!
0
+ current_user.reload
0
+ redirect_to dashboard_path
0
+ end
0
+
0
+ private
0
+
0
+ def favorite_location_params
0
+ (params[:favorite_location] || {}).pass(:name, :location)
0
+ end
0
+
0
+ def requested_favorite_location(reload = false)
0
+ safe_load :@favorite_location, Location::Favorite, :favorite_location_id, reload
0
+ end
0
+
0
+ def require_favorite_location!
0
+ require_exists! requested_favorite_location, Location::Favorite, :favorite_location_id
0
+ end
0
 
0
 end
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
27
28
29
 
 
 
 
 
 
 
 
30
31
32
0
@@ -27,14 +27,6 @@ module Utilities
0
           require_exists! requested_profile, Profile, :profile_id
0
         end
0
     
0
- def requested_favorite_location(reload = false)
0
- @favorite_location = nil if reload
0
- @favorite_location ||= params[:favorite_location_id]
0
- end
0
- def require_favorite_location!
0
- end
0
-
0
-
0
 
0
         def safe_load(variable_name, klass, parameter_name, reload)
0
           begin
...
18
19
20
21
 
 
22
23
24
...
18
19
20
 
21
22
23
24
25
0
@@ -18,7 +18,8 @@ module Location::Locatable
0
   
0
   module InstanceMethods
0
     
0
- def initialize_with_parse_location(options = {})
0
+ def initialize_with_parse_location(options = nil)
0
+ options ||= {}
0
       initialize_without_parse_location(options.block(:location))
0
       self.location = options[:location]
0
     end
...
8
9
10
 
 
11
12
13
...
8
9
10
11
12
13
14
15
0
@@ -8,6 +8,8 @@ class Profile < ActiveRecord::Base
0
   
0
   belongs_to :user
0
   
0
+ has_many :favorite_locations, :class_name => 'Location::Favorite'
0
+
0
   validates_presence_of :email
0
   validates_email :message => 'is not a valid email address', :allow_blank => true
0
   validates_length_of :email, :within => 3..100, :allow_blank => true#, :message => 'is not a valid email address'
...
1
2
 
 
 
 
 
 
 
3
...
 
1
2
3
4
5
6
7
8
9
0
@@ -1 +1,7 @@
0
-<% title 'Edit Favorite Location' -%>
0
\ No newline at end of file
0
+<% title 'Edit Favorite Location' -%>
0
+
0
+<%= render :partial => '/favorite_locations/form',
0
+ :locals => {
0
+ :url => formatted_favorite_location_path(:formatted_location_id => requested_favorite_location),
0
+ :submit_text => 'Update',
0
+ :method => :put } -%>
0
\ No newline at end of file
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
...
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -1 +1,22 @@
0
-<% title 'Your Favorite Locations' -%>
0
\ No newline at end of file
0
+<% title 'Your Favorite Locations' -%>
0
+
0
+<p class='section'><%= link_to 'Add', formatted_new_favorite_location_path -%></p>
0
+
0
+<table class='section'>
0
+ <thead>
0
+ <tr><th>Name</th><th>Location</th><th>&nbsp;</th></tr>
0
+ </thead>
0
+ <tbody>
0
+ <% requested_profile.favorite_locations.each do |f| -%>
0
+ <tr>
0
+ <td><%= f.name -%></td>
0
+ <td><%= f.location -%></td>
0
+ <td>
0
+ <%= link_to 'Edit', formatted_edit_favorite_location_path(:favorite_location_id => f) -%>
0
+ --
0
+ <%= link_to 'There now', formatted_set_current_favorite_location_path(:favorite_location_id => f), :method => :post -%>
0
+ </td>
0
+ </tr>
0
+ <% end -%>
0
+ </tbody>
0
+</table>
0
\ No newline at end of file
...
1
2
 
 
 
 
 
 
3
...
 
1
2
3
4
5
6
7
8
0
@@ -1 +1,6 @@
0
-<% title 'Create a New Favorite Location' -%>
0
\ No newline at end of file
0
+<% title 'Create a New Favorite Location' -%>
0
+
0
+<%= render :partial => '/favorite_locations/form',
0
+ :locals => {
0
+ :url => formatted_favorite_locations_path, :submit_text => 'Add it!',
0
+ :method => :post } -%>
0
\ No newline at end of file
...
2
3
4
 
5
6
...
2
3
4
5
6
7
0
@@ -2,4 +2,5 @@
0
 <ul>
0
   <li><%= link_to 'Privacy', edit_privacy_settings_path(:profile_id => current_user.profile) -%></li>
0
   <li><%= link_to 'Notifications', edit_notification_settings_path(:profile_id => current_user.profile) -%></li>
0
+ <li><%= link_to 'Favorite Locations', formatted_favorite_locations_path(:profile_id => current_user.profile) -%></li>
0
 </ul>
0
\ No newline at end of file
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 <p class='section'>
0
 <% if requested_profile.location.kind_of?(Location::Airport) -%>
0
- There is nobody else in <%= requested_profile.location -%> airport.
0
+ There is nobody else in <%= requested_profile.location -%>.
0
 <% else -%>
0
   There is nobody near <%= requested_profile.location -%>.
0
 <% end -%>
...
8
9
10
11
 
12
13
...
8
9
10
 
11
12
13
0
@@ -8,5 +8,5 @@
0
   <li><%= link_to 'Set your privacy preferences', edit_privacy_settings_path -%></li>
0
   <li><%= link_to 'Edit your profile', edit_profile_path -%></li>
0
   <li><%= link_to 'Set your location', edit_location_path -%></li>
0
- <li><%= link_to 'Add some favorite locations', new_favorite_location_path -%></li>
0
+ <li><%= link_to 'Add some favorite locations', formatted_new_favorite_location_path -%></li>
0
 </ul>
0
\ No newline at end of file
...
81
82
83
84
 
 
 
 
85
86
87
...
81
82
83
 
84
85
86
87
88
89
90
0
@@ -81,7 +81,10 @@ ActionController::Routing::Routes.draw do |map|
0
     end
0
   end
0
   
0
- map.resources :favorite_locations, :path_prefix => '/people/:profile_id/location', :as => 'favorites'
0
+ map.resources :favorite_locations,
0
+ :path_prefix => '/people/:profile_id/location',
0
+ :as => 'favorites',
0
+ :member => { :set_current => :post }
0
   
0
   map.with_options(:controller => 'settings') do |m|
0
     m.get do |n|
...
40
41
42
43
 
44
45
46
47
 
48
49
50
51
52
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
56
 
57
58
59
60
 
 
 
61
 
62
63
64
65
66
 
 
 
 
67
68
69
...
40
41
42
 
43
44
45
46
 
47
48
49
50
 
 
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
67
68
69
70
 
71
72
73
74
75
76
77
78
 
 
79
80
81
82
83
84
85
0
@@ -40,30 +40,46 @@ class FavoriteLocationsControllerTest < ActionController::TestCase
0
       
0
       
0
       should_be_allowed 'to see her own favorite locations' do
0
- get :index, :profile_id => current_user
0
+ get :index, :profile_id => current_profile
0
       end
0
       
0
       should_be_allowed 'to view the form to add a new favorite location' do
0
- get :new, :profile_id => current_user
0
+ get :new, :profile_id => current_profile
0
       end
0
       
0
       should 'be able to create a new favorite location' do
0
- post :create, :profile_id => current_user
0
- assert_redirected_to :action => 'index'
0
+ somewhere = Location::Base.find(:first)
0
+ assert_difference('current_profile.favorite_locations.size', 1) do
0
+ post :create, :profile_id => current_profile, :favorite_location => { :location => somewhere.address, :name => 'anything' }
0
+ assert_redirected_to :action => 'index'
0
+ end
0
+ end
0
+
0
+ should 'not be able to create a new favorite location for somebody else by hacking the parameters' do
0
+ other = somebody_other_than(current_user)
0
+ somewhere = Location::Base.find(:first)
0
+ assert_difference('other.profile.favorite_locations.size', 0) do
0
+ post :create, :profile_id => current_profile, :favorite_location => { :location => somewhere, :profile => other, :name => 'anything' }
0
+ end
0
       end
0
       
0
       should_be_allowed 'to view the form to edit an existing favorite location' do
0
- get :edit, :profile_id => current_user, :favorite_location_id => 1234
0
+ get :edit, :profile_id => current_profile, :favorite_location_id => current_profile.favorite_locations.first
0
       end
0
       
0
       should 'be able to update an existing favorite location' do
0
- put :update, :profile_id => current_user, :favorite_location_id => 1324
0
+ fave = current_profile.favorite_locations.first
0
+ assert_not_equal 'foo', fave.name
0
+ put :update, :profile_id => current_profile, :favorite_location_id => fave, :favorite_location => { :name => 'foo' }
0
         assert_redirected_to :action => 'index'
0
+ assert_equal 'foo', fave.reload.name
0
       end
0
 
0
       should 'be able to delete an existing favorite location' do
0
- delete :destroy, :profile_id => current_user, :favorite_location_id => 1324
0
- assert_redirected_to :action => 'index'
0
+ assert_difference('current_profile.favorite_locations.size', -1) do
0
+ delete :destroy, :profile_id => current_profile, :favorite_location_id => current_profile.favorite_locations.first
0
+ assert_redirected_to :action => 'index'
0
+ end
0
       end
0
       
0
     end

Comments

    No one has commented yet.