]> git.openstreetmap.org Git - rails.git/commitdiff
resolved user.rb conflict
authorNick Black <nickb@svn.openstreetmap.org>
Sat, 5 May 2007 14:24:08 +0000 (14:24 +0000)
committerNick Black <nickb@svn.openstreetmap.org>
Sat, 5 May 2007 14:24:08 +0000 (14:24 +0000)
app/controllers/user_controller.rb
app/models/user.rb
config/routes.rb
db/migrate.sql

index 6895d0f9cfe7588b4e52d818a8ad2c2c581ae6d4..60b83cc89f4d4d824fca7efd295bc2f0d585b7e5 100644 (file)
@@ -2,8 +2,8 @@ class UserController < ApplicationController
   layout 'site'
 
   before_filter :authorize, :only => [:preferences, :api_details, :api_gpx_files]
-  before_filter :authorize_web, :only => [:edit, :account, :go_public, :view, :diary]
-  before_filter :require_user, :only => [:edit, :set_home, :account, :go_public]
+  before_filter :authorize_web, :only => [:edit, :account, :go_public, :view, :diary, :make_friend]
+  before_filter :require_user, :only => [:edit, :set_home, :account, :go_public, :make_friend]
 
   def save
     @user = User.new(params[:user])
@@ -164,7 +164,22 @@ class UserController < ApplicationController
   end
 
   def make_friend
-    if params[:display_name]      
+
+    if params[:display_name]     
+      name = params[:display_name]
+      friend = Friend.new
+      friend.user_id = @user.id
+      friend.friend_user_id = User.find_by_display_name(name).id 
+      unless @user.is_friends_with?(friend)
+        if friend.save
+          flash[:notice] = "#{name} is now your friend"
+        else
+          friend.add_error("adding a friend failed")
+        end
+      else
+        flash[:notice] = "Your are already friends"  
+      end
+        redirect_to :controller => 'user', :action => 'view'
     end
   end
 
index 887dcb388ea7eced6506608adb356cff9c3f7b78..f7a4122a3afa9c3b56ec897ff7d4ac71eaf0d3b1 100644 (file)
@@ -5,7 +5,8 @@ class User < ActiveRecord::Base
   has_many :traces
   has_many :diary_entries
   has_many :messages, :foreign_key => :to_user_id
+  has_many :friends
+
   validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password'
   validates_uniqueness_of :display_name, :allow_nil => true
   validates_uniqueness_of :email
@@ -18,7 +19,7 @@ class User < ActiveRecord::Base
     self.timeout = Time.now
     self.token = User.make_token()
   end
-  
+
   def pass_crypt=(str) 
     write_attribute("pass_crypt", Digest::MD5.hexdigest(str)) 
   end
@@ -26,7 +27,7 @@ class User < ActiveRecord::Base
   def pass_crypt_confirmation=(str) 
     write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str)) 
   end
-  
+
   def self.authenticate(email, passwd) \r
     find(:first, :conditions => [ "email = ? AND pass_crypt = ?", email, Digest::MD5.hexdigest(passwd)])\r
   end 
@@ -34,7 +35,7 @@ class User < ActiveRecord::Base
   def self.authenticate_token(token) 
     find(:first, :conditions => [ "token = ? ", token])
   end 
-  
+
   def self.make_token(length=30)
     chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
     confirmstring = ''
@@ -58,9 +59,8 @@ class User < ActiveRecord::Base
     el1['account_created'] = self.creation_time.xmlschema
     return el1
   end
-  
+
   def nearby(lat_range=1, lon_range=1)
-     
       if self.home_lon and self.home_lat 
           nearby = User.find(:all,  :conditions => "#{self.home_lon} > home_lon - #{lon_range} and #{self.home_lon} < home_lon + #{lon_range} and  #{self.home_lon} > home_lon - #{lon_range} and #{self.home_lon} < home_lon + #{lon_range} and data_public = 1") 
       else
@@ -81,11 +81,21 @@ class User < ActiveRecord::Base
     messages = Message.find(:all, :conditions => "message_read = 0")
     return messages
   end
-  
+
   def get_all_messages
     messages = Message.find(:all, :conditions => "message_read = 0")
     return messages
   end
 
-  
+  def is_friends_with?(new_friend)
+    res = false
+    @new_friend = new_friend
+    self.friends.each do |friend|
+      if friend.user_id == @new_friend.user_id
+        return true
+      end
+    end
+    return false
+  end
+
 end
index bca61fd89a75d57cf8ed327bdeee946612906085..dd08c78152aaf19bfbe5e73fc29175eae467d107 100644 (file)
@@ -63,6 +63,7 @@ ActionController::Routing::Routes.draw do |map|
   map.connect '/traces/tag/:tag/page/:page', :controller => 'trace', :action => 'list', :id => nil
 
   # user pages
+  map.connect '/user/:display_name/make_friend', :controller => 'user', :action => 'make_friend'
   map.connect '/user/:display_name', :controller => 'user', :action => 'view'
   map.connect '/user/:display_name/diary', :controller => 'user', :action => 'diary'
   map.connect '/user/:display_name/diary/newpost', :controller => 'diary_entry', :action => 'new'
index 72c71232b169df557029232703b34428cab22ba2..302ad2a6ccfef0b5cb8cbd1184b6e0ab05e1a16d 100644 (file)
@@ -46,3 +46,6 @@ alter table users add column within_lon double default 2;
 alter table users add column within_lat double default 2;
 
 create table messages (id bigint not null auto_increment, user_id bigint(20) not null,  from_user_id bigint(20) not null, title varchar(255), body text, sent_on datetime, message_read boolean default 0, primary key(id));
+
+create table friends (id bigint not null auto_increment, user_id bigint(20) not null, friend_user_id(20) not null, primary key(id));
+create index user_id_idx on friends(friend_user_id);