[23786] users/jberry/mpwa

source_changes at macosforge.org source_changes at macosforge.org
Mon Apr 9 17:40:13 PDT 2007


Revision: 23786
          http://trac.macosforge.org/projects/macports/changeset/23786
Author:   jberry at macports.org
Date:     2007-04-09 17:40:12 -0700 (Mon, 09 Apr 2007)

Log Message:
-----------
mpwa: Add new table for file blobs to isolate from general file information in case we decide blobs should be stored externally.

Modified Paths:
--------------
    users/jberry/mpwa/app/models/port_pkg.rb
    users/jberry/mpwa/app/models/port_pkg_file.rb
    users/jberry/mpwa/app/views/port_pkg/_form.rhtml
    users/jberry/mpwa/doc/design.txt
    users/jberry/mpwa/doc/schema-thoughts.sql

Added Paths:
-----------
    users/jberry/mpwa/app/controllers/file_blob_controller.rb
    users/jberry/mpwa/app/helpers/file_blob_helper.rb
    users/jberry/mpwa/app/models/file_blob.rb
    users/jberry/mpwa/app/views/file_blob/
    users/jberry/mpwa/app/views/file_blob/_form.rhtml
    users/jberry/mpwa/app/views/file_blob/edit.rhtml
    users/jberry/mpwa/app/views/file_blob/list.rhtml
    users/jberry/mpwa/app/views/file_blob/new.rhtml
    users/jberry/mpwa/app/views/file_blob/show.rhtml
    users/jberry/mpwa/app/views/layouts/file_blob.rhtml
    users/jberry/mpwa/test/fixtures/file_blobs.yml
    users/jberry/mpwa/test/functional/file_blob_controller_test.rb
    users/jberry/mpwa/test/unit/file_blob_test.rb

Added: users/jberry/mpwa/app/controllers/file_blob_controller.rb
===================================================================
--- users/jberry/mpwa/app/controllers/file_blob_controller.rb	                        (rev 0)
+++ users/jberry/mpwa/app/controllers/file_blob_controller.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,51 @@
+class FileBlobController < ApplicationController
+  def index
+    list
+    render :action => 'list'
+  end
+
+  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
+  verify :method => :post, :only => [ :destroy, :create, :update ],
+         :redirect_to => { :action => :list }
+
+  def list
+    @file_blob_pages, @file_blobs = paginate :file_blobs, :per_page => 10
+  end
+
+  def show
+    @file_blob = FileBlob.find(params[:id])
+  end
+
+  def new
+    @file_blob = FileBlob.new
+  end
+
+  def create
+    @file_blob = FileBlob.new(params[:file_blob])
+    if @file_blob.save
+      flash[:notice] = 'FileBlob was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @file_blob = FileBlob.find(params[:id])
+  end
+
+  def update
+    @file_blob = FileBlob.find(params[:id])
+    if @file_blob.update_attributes(params[:file_blob])
+      flash[:notice] = 'FileBlob was successfully updated.'
+      redirect_to :action => 'show', :id => @file_blob
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    FileBlob.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end

Added: users/jberry/mpwa/app/helpers/file_blob_helper.rb
===================================================================
--- users/jberry/mpwa/app/helpers/file_blob_helper.rb	                        (rev 0)
+++ users/jberry/mpwa/app/helpers/file_blob_helper.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,2 @@
+module FileBlobHelper
+end

Added: users/jberry/mpwa/app/models/file_blob.rb
===================================================================
--- users/jberry/mpwa/app/models/file_blob.rb	                        (rev 0)
+++ users/jberry/mpwa/app/models/file_blob.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,7 @@
+class FileBlob < ActiveRecord::Base
+    belongs_to :port_pkg_file
+    
+    def read(file)
+        self.data = file.read
+    end
+end

Modified: users/jberry/mpwa/app/models/port_pkg.rb
===================================================================
--- users/jberry/mpwa/app/models/port_pkg.rb	2007-04-10 00:33:12 UTC (rev 23785)
+++ users/jberry/mpwa/app/models/port_pkg.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -70,19 +70,19 @@
         # TODO: Need to fix the submitter somehow
         port_pkg.submitter = Person.ensure_person_with_email("jberry at macports.org")
         port_pkg.submitted_at = Time.now
- 
         port_pkg.port = port
         port_pkg.epoch = info['epoch']
         port_pkg.version = info['version']
         port_pkg.revision = info['revision']
 
         # Save unpacked data into a file
-        portPkgFile = PortPkgFile.new
-        portPkgFile.name = 'portpkg.xar'
         file.rewind
-        portPkgFile.data = file.read
+        portPkgFile = PortPkgFile.from_file(file, 'portpkg.xar')        
         port_pkg.files << portPkgFile
         
+        # Save files for from the expanded package
+        
+        
         # Save the pkg (maybe we shouldn't?)
         port_pkg.save
         

Modified: users/jberry/mpwa/app/models/port_pkg_file.rb
===================================================================
--- users/jberry/mpwa/app/models/port_pkg_file.rb	2007-04-10 00:33:12 UTC (rev 23785)
+++ users/jberry/mpwa/app/models/port_pkg_file.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -1,3 +1,34 @@
 class PortPkgFile < ActiveRecord::Base
     belongs_to :port_pkg
+    has_one :file_blob
+    
+    def PortPkgFile.from_path(path)
+        port_pkg_file = PortPkgFile.new
+        return port_pkg_file.read_from_path(path)
+    end
+    
+    def PortPkgFile.from_file(file, path)
+        port_pkg_file = PortPkgFile.new
+        return port_pkg_file.read_from_file(file, path)
+    end
+
+    def read_from_path(path)
+        File.open(path, "r") { |f| read_from_file(f, path) }
+        return self
+    end
+    
+    def read_from_file(file, path)
+        # Create a new blob
+        blob = FileBlob.new
+        blob.read(file)
+        self.file_blob = blob
+        
+        # Save other file information
+        self.file_path = path
+        self.length = blob.data.length
+        self.mime_type = 'text/plain'       # for now: could map file extensions and/or magic numbers, plus test for whether it looks like text
+        self.sha256 = Digest::SHA256.hexdigest(blob.data)
+        
+        return self  
+    end
 end

Added: users/jberry/mpwa/app/views/file_blob/_form.rhtml
===================================================================
--- users/jberry/mpwa/app/views/file_blob/_form.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/file_blob/_form.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,7 @@
+<%= error_messages_for 'file_blob' %>
+
+<!--[form:file_blob]-->
+<p><label for="file_blob_data">Data</label><br/>
+</p>
+<!--[eoform:file_blob]-->
+

Added: users/jberry/mpwa/app/views/file_blob/edit.rhtml
===================================================================
--- users/jberry/mpwa/app/views/file_blob/edit.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/file_blob/edit.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,9 @@
+<h1>Editing file_blob</h1>
+
+<% form_tag :action => 'update', :id => @file_blob do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @file_blob %> |
+<%= link_to 'Back', :action => 'list' %>

Added: users/jberry/mpwa/app/views/file_blob/list.rhtml
===================================================================
--- users/jberry/mpwa/app/views/file_blob/list.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/file_blob/list.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,27 @@
+<h1>Listing file_blobs</h1>
+
+<table>
+  <tr>
+  <% for column in FileBlob.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for file_blob in @file_blobs %>
+  <tr>
+  <% for column in FileBlob.content_columns %>
+    <td><%=h file_blob.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => file_blob %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => file_blob %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => file_blob }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @file_blob_pages.current.previous } if @file_blob_pages.current.previous %>
+<%= link_to 'Next page', { :page => @file_blob_pages.current.next } if @file_blob_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New file_blob', :action => 'new' %>

Added: users/jberry/mpwa/app/views/file_blob/new.rhtml
===================================================================
--- users/jberry/mpwa/app/views/file_blob/new.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/file_blob/new.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,8 @@
+<h1>New file_blob</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>

Added: users/jberry/mpwa/app/views/file_blob/show.rhtml
===================================================================
--- users/jberry/mpwa/app/views/file_blob/show.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/file_blob/show.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,8 @@
+<% for column in FileBlob.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @file_blob.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @file_blob %> |
+<%= link_to 'Back', :action => 'list' %>

Added: users/jberry/mpwa/app/views/layouts/file_blob.rhtml
===================================================================
--- users/jberry/mpwa/app/views/layouts/file_blob.rhtml	                        (rev 0)
+++ users/jberry/mpwa/app/views/layouts/file_blob.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+  <title>FileBlob: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>

Modified: users/jberry/mpwa/app/views/port_pkg/_form.rhtml
===================================================================
--- users/jberry/mpwa/app/views/port_pkg/_form.rhtml	2007-04-10 00:33:12 UTC (rev 23785)
+++ users/jberry/mpwa/app/views/port_pkg/_form.rhtml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -13,7 +13,5 @@
 <p><label for="port_pkg_revision">Revision</label><br/>
 <%= text_field 'port_pkg', 'revision'  %></p>
 
-<p><label for="port_pkg_url">Url</label><br/>
-<%= text_field 'port_pkg', 'url'  %></p>
 <!--[eoform:port_pkg]-->
 

Modified: users/jberry/mpwa/doc/design.txt
===================================================================
--- users/jberry/mpwa/doc/design.txt	2007-04-10 00:33:12 UTC (rev 23785)
+++ users/jberry/mpwa/doc/design.txt	2007-04-10 00:40:12 UTC (rev 23786)
@@ -88,8 +88,8 @@
 		/common (revisioned/shared between instances of a port)
 			/first-letter-of-portname
 				/portname/<directory tree> (the unbundled/uncompressed portpkg)
-		/5-low-bits-pkgid-2char-hex
-			/5-next-bits-pkgid-2char-hex
+		/low-6-bits-pkgid-2char-hex
+			/next-6-bits-pkgid-2char-hex
 				/pkgid-zero-padded-dec
 					/portpkg.tgz (the bundled/compressed portpkg)
 					/unpacked/<directory tree> (the unbundled/uncompressed portpkg)

Modified: users/jberry/mpwa/doc/schema-thoughts.sql
===================================================================
--- users/jberry/mpwa/doc/schema-thoughts.sql	2007-04-10 00:33:12 UTC (rev 23785)
+++ users/jberry/mpwa/doc/schema-thoughts.sql	2007-04-10 00:40:12 UTC (rev 23786)
@@ -58,19 +58,21 @@
 
 drop table if exists Port_Pkg_Files;
 create table Port_Pkg_Files (
-    id              bigint not null primary key auto_increment,
-    port_pkg_id     bigint not null,
-    name            varchar(1023),
-    mimetype        varchar(63)
+    id                  bigint not null primary key auto_increment,
+    port_pkg_id         bigint not null,
+    file_path           varchar(2047),
+    length              bigint not null,
+    mime_type           varchar(63),
+    sha256              varchar(64)
 );
-    
-drop table if exists File_Blobs
+
+drop table if exists File_Blobs;
 create table File_Blobs (
     id                  bigint not null primary key auto_increment,
     port_pkg_file_id    bigint not null,
     data                blob
 );
-    
+
 -- A tag which may be attached to various items through Ports_Tags, Port_Pkgs_Tags
 drop table if exists Tags;
 create table Tags (

Added: users/jberry/mpwa/test/fixtures/file_blobs.yml
===================================================================
--- users/jberry/mpwa/test/fixtures/file_blobs.yml	                        (rev 0)
+++ users/jberry/mpwa/test/fixtures/file_blobs.yml	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,5 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+  id: 1
+two:
+  id: 2

Added: users/jberry/mpwa/test/functional/file_blob_controller_test.rb
===================================================================
--- users/jberry/mpwa/test/functional/file_blob_controller_test.rb	                        (rev 0)
+++ users/jberry/mpwa/test/functional/file_blob_controller_test.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'file_blob_controller'
+
+# Re-raise errors caught by the controller.
+class FileBlobController; def rescue_action(e) raise e end; end
+
+class FileBlobControllerTest < Test::Unit::TestCase
+  fixtures :file_blobs
+
+  def setup
+    @controller = FileBlobController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = file_blobs(:first).id
+  end
+
+  def test_index
+    get :index
+    assert_response :success
+    assert_template 'list'
+  end
+
+  def test_list
+    get :list
+
+    assert_response :success
+    assert_template 'list'
+
+    assert_not_nil assigns(:file_blobs)
+  end
+
+  def test_show
+    get :show, :id => @first_id
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:file_blob)
+    assert assigns(:file_blob).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:file_blob)
+  end
+
+  def test_create
+    num_file_blobs = FileBlob.count
+
+    post :create, :file_blob => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_file_blobs + 1, FileBlob.count
+  end
+
+  def test_edit
+    get :edit, :id => @first_id
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:file_blob)
+    assert assigns(:file_blob).valid?
+  end
+
+  def test_update
+    post :update, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'show', :id => @first_id
+  end
+
+  def test_destroy
+    assert_nothing_raised {
+      FileBlob.find(@first_id)
+    }
+
+    post :destroy, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      FileBlob.find(@first_id)
+    }
+  end
+end

Added: users/jberry/mpwa/test/unit/file_blob_test.rb
===================================================================
--- users/jberry/mpwa/test/unit/file_blob_test.rb	                        (rev 0)
+++ users/jberry/mpwa/test/unit/file_blob_test.rb	2007-04-10 00:40:12 UTC (rev 23786)
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class FileBlobTest < Test::Unit::TestCase
+  fixtures :file_blobs
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20070409/7368240c/attachment.html


More information about the macports-changes mailing list