Skip to content

Commit

Permalink
Adding users to documents and revisions.
Browse files Browse the repository at this point in the history
Every uploaded document or revision has a user assigned to it.  A user can view all the documents they've interacted with on their profile page.
  • Loading branch information
bamnet committed Jan 14, 2012
1 parent a6175d3 commit f62cf29
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/controllers/documents_controller.rb
Expand Up @@ -3,6 +3,7 @@ class DocumentsController < ApplicationController
# GET /documents/1.xml
def show
@document = Document.find(params[:id])
@current_revision = @document.revisions.current.first

respond_to do |format|
format.html # show.html.erb
Expand Down Expand Up @@ -35,6 +36,10 @@ def edit
# POST /documents.xml
def create
@document = Document.new(params[:document])
@document.user = current_user
@document.revisions.each do |r|
r.user = current_user
end

respond_to do |format|
if @document.save
Expand Down
1 change: 1 addition & 0 deletions app/controllers/revisions_controller.rb
Expand Up @@ -17,6 +17,7 @@ def new
def create
@revision = Revision.new(params[:revision])
@revision.document = @document
@revision.user = current_user

respond_to do |format|
if @revision.save
Expand Down
2 changes: 2 additions & 0 deletions app/models/document.rb
@@ -1,5 +1,6 @@
class Document < ActiveRecord::Base
belongs_to :category
belongs_to :user
has_many :revisions, :dependent => :destroy

accepts_nested_attributes_for :revisions
Expand All @@ -15,4 +16,5 @@ class Document < ActiveRecord::Base
def download_count
revisions.sum(:download_count)
end

end
1 change: 1 addition & 0 deletions app/models/revision.rb
@@ -1,5 +1,6 @@
class Revision < ActiveRecord::Base
belongs_to :document
belongs_to :user

# Handles the file attachment stuff
# git://github.com/bamnet/attachable.git
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
@@ -1,4 +1,7 @@
class User < ActiveRecord::Base
has_many :documents
has_many :revisions

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
Expand All @@ -7,4 +10,9 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name

def related_documents
(Document.joins(:revisions).where('revisions.user_id' => id) + documents).uniq
end


end
13 changes: 7 additions & 6 deletions app/views/documents/show.html.erb
Expand Up @@ -37,7 +37,7 @@
<div id="dl-padding">
<div id="dl_icon">
<%= link_to download_document_revision_path(@document, 'current') do %>
<%= image_tag("icons/icon_#{@document.revisions.current.first.short_type}.png", :alt => "Download File") %>
<%= image_tag("icons/icon_#{@current_revision.short_type}.png", :alt => "Download File") %>
<% end %>
</div>

Expand All @@ -46,7 +46,8 @@
<div id="dl-link-padding">
<%= link_to "Download File", download_document_revision_path(@document, 'current') %>
<span class="details">
from USER on <%= @document.revisions.current.first.created_at.strftime('%D') %>
from <%= @current_revision.user.nil? ? "Unknown" : @current_revision.user.name %>
on <%= @current_revision.created_at.strftime('%D') %>
</span>
&nbsp;&nbsp;<%= render :partial => 'clippy', :locals => {:document => @document} %>
</div>
Expand All @@ -60,7 +61,7 @@
<%= link_to download_document_revision_path(@document, revision) do %>
<%= image_tag("icons/icon_#{revision.short_type}.png", :height => "20", :alt => "Download File") %> Revision <%= i+1 %>
<% end %>
<span class="details">from USER on <%= revision.created_at.getlocal.strftime('%D @ %r') %></span>
<span class="details">from <%= revision.user.nil? ? "Unknown" : revision.user.name %> on <%= revision.created_at.getlocal.strftime('%D @ %r') %></span>
</div>
<% end %>
<% end %>
Expand Down Expand Up @@ -92,9 +93,9 @@
</div>
<% unless @document.revisions.empty? %>
<div id="admin_debug" class="mc-indent">
Filename: <%= @document.revisions.current.first.file_name %><br />
Size: <%= number_to_human_size(@document.revisions.current.first.file_size) %><br />
Filename: <%= @current_revision.file_name %><br />
Size: <%= number_to_human_size(@current_revision.file_size) %><br />
Search Text: <br />
<textarea id="debug_data"><%= @document.revisions.current.first.search_text %></textarea>
<textarea id="debug_data"><%= @current_revision.search_text %></textarea>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Expand Up @@ -24,5 +24,5 @@
<%# end %>
</ul>
<h2>Documents</h2>
<div id="doc_holder"><%#= render :partial => 'documents/table', :locals => {:documents => @user.documents} %></div>
<div id="doc_holder"><%= render :partial => 'documents/table', :locals => {:documents => @user.related_documents} %></div>
</div>
6 changes: 6 additions & 0 deletions db/migrate/20120114063856_add_users_to_docs_and_revs.rb
@@ -0,0 +1,6 @@
class AddUsersToDocsAndRevs < ActiveRecord::Migration
def change
add_column :documents, :user_id, :integer
add_column :revisions, :user_id, :integer
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120107013135) do
ActiveRecord::Schema.define(:version => 20120114063856) do

create_table "categories", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -46,6 +46,7 @@
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end

create_table "revisions", :force => true do |t|
Expand All @@ -58,6 +59,7 @@
t.binary "file_data", :limit => 4194304
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end

create_table "users", :force => true do |t|
Expand Down

0 comments on commit f62cf29

Please sign in to comment.