From: Tom Hughes Date: Tue, 14 Aug 2007 17:29:27 +0000 (+0000) Subject: Improve handling of user to user messages. X-Git-Tag: live~8195 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/b61e4f77e86fd2535a88b0d3e5aec97809c8ec5e?hp=e70d94c54a3c449bc12e99d0f3a01a3ef20fc0ab Improve handling of user to user messages. --- diff --git a/app/models/message.rb b/app/models/message.rb index 2c176a26d..039c8d30b 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,3 +1,4 @@ class Message < ActiveRecord::Base - belongs_to :user + belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id + belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id end diff --git a/app/models/user.rb b/app/models/user.rb index 63f6bd389..783a9bc3b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,7 @@ class User < ActiveRecord::Base has_many :traces has_many :diary_entries, :order => 'created_at DESC' has_many :messages, :foreign_key => :to_user_id + has_many :new_messages, :class_name => "Message", :foreign_key => :to_user_id, :conditions => "message_read = 0" has_many :friends validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password' @@ -76,24 +77,6 @@ class User < ActiveRecord::Base return OSM::GreatCircle.new(self.home_lat, self.home_lon).distance(nearby_user.home_lat, nearby_user.home_lon) end - def self.has_messages? - if Message.fdhjklsafind_by_to_user_id(self.id) - return true - else - return false - end - end - - def get_new_messages - messages = Message.find(:all, :conditions => "message_read = 0 and to_user_id = #{self.id}") - return messages - end - - def get_all_messages - messages = Message.find(:all, :conditions => "to_user_id = #{self.id}") - return messages - end - def is_friends_with?(new_friend) res = false @new_friend = new_friend diff --git a/app/views/layouts/site.rhtml b/app/views/layouts/site.rhtml index 41c3831c5..2a1c8ac0b 100644 --- a/app/views/layouts/site.rhtml +++ b/app/views/layouts/site.rhtml @@ -20,11 +20,11 @@ <% if @user and @user.id %> Welcome, <%= link_to @user.display_name, {:controller => 'user', :action => 'view', :display_name => @user.display_name}%> | - <% @inbox_weight = 'bold' if @user.get_new_messages.length > 0 %> + <% @inbox_weight = 'bold' if @user.new_messages.size > 0 %> <% if !@user.home_lon.nil? and !@user.home_lat.nil? %> <%= link_to 'home', {:controller => 'site', :action => 'index', :lon => @user.home_lon, :lat => @user.home_lat, :zoom => '10'} %> | <% end %> - <%= link_to "inbox (#{@user.get_new_messages.length})", {:controller => 'message', :action => 'inbox', :display_name => @user.display_name}, {:style => "font-weight: #{@inbox_weight};" } %> | + <%= link_to "inbox (#{@user.new_messages.size})", {:controller => 'message', :action => 'inbox', :display_name => @user.display_name}, {:style => "font-weight: #{@inbox_weight};" } %> | <%= link_to 'logout', {:controller => 'user', :action => 'logout', :referer => request.request_uri}, {:id => 'logoutanchor'}%> <% else %> <%= link_to 'log in', {:controller => 'user', :action => 'login', :referer => request.request_uri}, {:id => 'loginanchor'}%> | diff --git a/app/views/message/_message_summary.rhtml b/app/views/message/_message_summary.rhtml new file mode 100644 index 000000000..f572c8990 --- /dev/null +++ b/app/views/message/_message_summary.rhtml @@ -0,0 +1,11 @@ + + <%= link_to message_summary.sender.display_name , :controller => 'user', :action => message_summary.sender.display_name %> + <%= link_to message_summary.title , :controller => 'message', :action => 'read', :message_id => message_summary.id %> + <%= message_summary.sent_on %> + <% if message_summary.message_read? %> + Message read + <% else %> + <%= button_to 'Mark as read', :controller => 'message', :action => 'mark', :message_id => message_summary.id %> + <% end %> + <%= button_to 'Reply', :controller => 'message', :action => 'new', :user_id => message_summary.from_user_id %> + diff --git a/app/views/message/inbox.rhtml b/app/views/message/inbox.rhtml index daae9cd64..1c17ae18f 100644 --- a/app/views/message/inbox.rhtml +++ b/app/views/message/inbox.rhtml @@ -1,51 +1,30 @@

My inbox

-

You have <%=@user.get_new_messages.length %> new messages and <%=@user.get_all_messages.length - @user.get_new_messages.length %> old messages

+

You have <%= @user.new_messages.size %> new messages and <%= @user.messages.size - @user.new_messages.size %> old messages

-<% if (params[:all] and @user.get_all_messages.length > 0) or (@user.get_new_messages.length > 0) %> +<% if (params[:all] and @user.messages.size > 0) or (@user.new_messages.size > 0) %>
- - - - - - +
fromtitlereceived on
+ + + + + + + + <% if params[:all] %> + <%= render :partial => "message_summary", :collection => @user.messages %> + <% else %> + <%= render :partial => "message_summary", :collection => @user.new_messages %> + <% end %> +
FromSubjectDate
+
+<% end %> - <% if params[:all] %> - <% @user.get_all_messages.each do |message| %> - <%= link_to User.find(message.from_user_id).display_name , :controller => 'user', :action => User.find(message.from_user_id).display_name %> - <%= link_to message.title , :controller => 'message', :action => 'read', :message_id => message.id %> - <%= message.sent_on %> - <% if message.message_read = 0 %> - <%= link_to 'mark as read', :controller => 'message', :action => 'mark', :message_id => message.id %> - <%else%> - message read - <%end%> - <%= link_to 'reply', :controller => 'message', :action => 'new', :user_id => message.from_user_id %> - - <%end%> - - <%else%> - - <% @user.get_new_messages.each do |message| %> - <%= link_to User.find(message.from_user_id).display_name , :controller => 'user', :action => User.find(message.from_user_id).display_name %> - <%= link_to message.title , :controller => 'message', :action => 'read', :message_id => message.id %> - <%= message.sent_on %> - <% if message.message_read = 0 %> - <%= link_to 'mark as read', :controller => 'message', :action => 'mark', :message_id => message.id %> - <%else%> - message read - <%end%> - - <%end%> - <%end%> - - - -<%end%> +
<% if params[:all] %> - <%= link_to 'show new messages', :controller => 'message', :action => 'inbox', :display_name => @user.display_name %> + <%= link_to 'Show new messages', :controller => 'message', :action => 'inbox', :display_name => @user.display_name %> <% else %> - <%= link_to 'show all messages', :controller => 'message', :action => 'inbox', :display_name => @user.display_name, :all => true %> -<% end %> \ No newline at end of file + <%= link_to 'Show all messages', :controller => 'message', :action => 'inbox', :display_name => @user.display_name, :all => true %> +<% end %> diff --git a/app/views/message/read.rhtml b/app/views/message/read.rhtml index d20417e14..a8bd2e733 100644 --- a/app/views/message/read.rhtml +++ b/app/views/message/read.rhtml @@ -1,16 +1,29 @@

Reading your messages

- - - - - - - - + + + + + + + + + + + + + + + +
frommessagedatereply
<%= (User.find_by_id(@message.from_user_id)).display_name %><%= @message.body %><%= @message.sent_on %><%= link_to 'reply', :controller => 'message', :action => 'new', :user_id => @message.from_user_id %>
From<%= link_to @message.sender.display_name, :controller => 'user', :action => 'view', :display_name => @message.sender.display_name %>
Subject<%= @message.title %>
Date<%= @message.sent_on %>
Message<%= @message.body %>
-<% form_tag :controller => 'message', :action => 'mark', :message_id => @message.id do %> -<%= submit_tag 'Mark as read' %> -<% end %> +
+ + + + + + +
<%= button_to 'Reply', :controller => 'message', :action => 'new', :user_id => @message.from_user_id %><%= button_to 'Mark as read', :controller => 'message', :action => 'mark', :message_id => @message.id %>