From b0412b48c247539b3890a54ec450a81f007ea702 Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Thu, 6 Jul 2006 06:31:57 +0000
Subject: [PATCH] Sort recordings on receive time.

Properly store when a recording was received.  Sort the recordings on
that column.

Fix the _list.rhtml template so that it is more nice.  Add ID3 tags to
the MP3 files.
---
 app/controllers/recordings_controller.rb | 16 +++++++---
 app/models/recording.rb                  |  5 ++++
 app/views/recordings/_list.rhtml         | 37 +++++++++++++++++++-----
 db/migrate/001_initial.rb                |  1 +
 4 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/app/controllers/recordings_controller.rb b/app/controllers/recordings_controller.rb
index fc29e3d..4c9879a 100644
--- a/app/controllers/recordings_controller.rb
+++ b/app/controllers/recordings_controller.rb
@@ -26,7 +26,7 @@ class RecordingsController < ApplicationController
     @recording_pages, @recordings = paginate :recordings, 
 	:per_page => 10,
 	:conditions => [ "(user_id = ? OR user_id IS NULL)", user_id],
-	:order => "updated_on"
+	:order => "`when` desc"
   end
 
   def show
@@ -87,13 +87,20 @@ class RecordingsController < ApplicationController
  	timestamp = $2
 	cid = $3
 	mp3file = dir + "/mp3/v-" + pid + "-" + timestamp + "-" + cid + ".mp3"
+	record_time = Time.at(timestamp.to_i)
+	id3title = "Nr " + cid + record_time.strftime(" at %Y-%m-%d %H:%M:%S.")
+	id3artist = cid
+	id3album = record_time.strftime("Answering machine %Y-%m-%d")
         system("rmdtopvf " + dir + "/incoming/" + 
 		filename + "| pvftowav " +
-		"| lame -h - " + mp3file)
-	record_time = Time.at(timestamp.to_i)
+		"| lame -h --add-id3v2" + 
+		" --tt '" + id3title + "'" + 
+		" --ta '" + id3artist + "'" + 
+		" --tl '" + id3album + "'" + 
+		" - " + mp3file)
 	mp3info = Mp3Info.new(mp3file)
 	recording = Recording.new(:cid => cid,
-		    		  :when => timestamp,
+		    		  :when => record_time,
 		      		  :file => mp3file,
 				  :length => mp3info.length,
 		      		  :seen => 0,
@@ -104,5 +111,6 @@ class RecordingsController < ApplicationController
 	# 	    dir + "/archived/" + filename)
       end
     end
+    @recordings.sort!
   end
 end
diff --git a/app/models/recording.rb b/app/models/recording.rb
index e0752cd..92e817a 100644
--- a/app/models/recording.rb
+++ b/app/models/recording.rb
@@ -1,3 +1,8 @@
 class Recording < ActiveRecord::Base
   belongs_to :user
+
+  def <=>(other)
+    # Reverse the order, to get the newest entry first.
+    other["when"] <=> self["when"]
+  end
 end
diff --git a/app/views/recordings/_list.rhtml b/app/views/recordings/_list.rhtml
index 4fcf3aa..e287304 100644
--- a/app/views/recordings/_list.rhtml
+++ b/app/views/recordings/_list.rhtml
@@ -1,17 +1,40 @@
-<table>
+<% special_columns = ["when", "length"] %>
+<% ignored_columns = ["file", "created_on", "updated_on"] %>
+<% first_columns = ["when", "cid", "from", "length", "user_id", "comment"] %>
+<table border="1">
   <tr>
     <th>&nbsp;</th>
-  <% for column in Recording.content_columns %>
-    <th><%= column.human_name %></th>
-  <% end %>
+    <% for column_name in first_columns %>
+      <th><%= Recording.columns_hash[column_name].human_name %></th>
+    <%end%>
+    <% for column in Recording.content_columns %>
+      <% if not (ignored_columns + first_columns).include?(column.name) %>
+        <th><%= column.human_name %></th>
+      <% end %>
+    <% end %>
   </tr>
   
 <% for recording in @recordings %>
   <tr>
     <td><%= link_to 'Play', :action => 'play', :id => recording %></td>
-  <% for column in Recording.content_columns %>
-    <td><%=h recording.send(column.name) %></td>
-  <% end %>
+    <% for column_name in first_columns %>
+      <td>
+      <% if special_columns.include?(column_name) %>
+        <% if column_name == "when" %>
+	  <%= recording["when"].strftime("%Y-%m-%d&nbsp;%H:%M:%S") %>
+	<% elsif column_name == "length" %>
+	  <%= (10*recording["length"].to_f).round / 10.0 %>
+	<% end %>
+      <% else %>
+        <%=h recording.send(column_name) %>
+      <%end%>
+      </td>
+    <%end%>
+    <% for column in Recording.content_columns %>
+      <% if not (ignored_columns + first_columns).include?(column.name) %>
+        <td><%=h recording.send(column.name) %></td>
+      <% end %>
+    <% end %>
     <td><%= link_to 'Show', :action => 'show', :id => recording %></td>
     <td><%= link_to 'Edit', :action => 'edit', :id => recording %></td>
     <td><%= link_to 'Destroy', { :action => 'destroy', :id => recording }, :confirm => 'Are you sure?', :post => true %></td>
diff --git a/db/migrate/001_initial.rb b/db/migrate/001_initial.rb
index 443af48..2682d60 100644
--- a/db/migrate/001_initial.rb
+++ b/db/migrate/001_initial.rb
@@ -12,6 +12,7 @@ class Initial < ActiveRecord::Migration
 		  :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8') do |t|
       t.column :cid,      :string
       t.column :from,     :string
+      t.column :comment,  :string
       t.column :when,     :datetime
       t.column :user_id,  :integer
       t.column :file,	  :string
-- 
GitLab