Skip to content

Commit

Permalink
Attachments are now done through a multi-step process.
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Oct 25, 2009
1 parent 9bbada3 commit 49ff3b1
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 42 deletions.
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -24,6 +24,8 @@ class ApplicationController < ActionController::Base

before_filter :set_default_theme

private

def set_default_theme
@default_theme = Theme.find_by_is_default(true)
end
Expand All @@ -35,4 +37,8 @@ def check_page_value
def set_time_zone
Time.zone = current_user.time_zone if logged_in? && !current_user.time_zone.nil?
end

def go_directly_to_post
redirect_to forum_topic_path(@post.forum,@topic) + "/#{@post.page_for(current_user)}" + "#post_#{@post.id}"
end
end
55 changes: 30 additions & 25 deletions app/controllers/attachments_controller.rb
@@ -1,5 +1,6 @@
class AttachmentsController < ApplicationController
before_filter :find_forum
before_filter :find_parents
before_filter :login_required

def new
@attachment = Attachment.new
Expand All @@ -8,36 +9,40 @@ def new
def create
@attachment = Attachment.new
currently = params[:currently]
if !logged_in?
flash.now[:error] = t(:you_must_be_logged_in)
else
# We find the forum because we want to see if the current user can post in it.
forum = Forum.find(params[:forum_id])

if current_user.can?(:use_attachments, @forum)
if params[:attachment] && params[:attachment][:file]
# Get the time of the post, this is set in PostsController#new/#create/#edit
# Anything goes. For the time being.
session[currently] ||= []
session[currently] << Attachment.create(params[:attachment]).id
flash.now[:success] = t(:Upload_successful)
else
flash.now[:error] = t(:Please_select_a_file_to_upload)
end
if current_user.can?(:use_attachments, @forum)
if params[:attachment] && !params[:attachment][:file].blank?
@post.attachments.create(params[:attachment])
flash[:success] = t(:Upload_successful)
else
flash.now[:error] = t(:You_cannot_post_attachments_in_this_forum)
if params[:commit] != t(:Post)
flash[:error] = t(:Please_select_a_file_to_upload)
render :action => "new"
end
end
else
flash[:error] = t(:You_cannot_post_attachments_in_this_forum)
redirect_to forum_path(@forum)
end
if params[:commit] == t(:Attach_this_and_more)
redirect_to new_topic_post_attachment_path(@topic, @post)
elsif [t(:Attach_this_and_post), t(:Post)].include?(params[:commit]) && !performed?
@topic.finished!
@post.finished!
flash[:notice] = t(:created, :thing => t(:Post))
go_directly_to_post
end

# session[currently] may not be set because this action failed.
# Previous uploads for this post may have succeeded, so we need to gather regardless.
@attachments = Attachment.find(session[currently]) if session[currently]
render :action => "new"
end

private
def find_forum
@forum = Forum.find(params[:forum_id])
def find_parents
@post = Post.find(params[:post_id], :include => [{ :topic => :forum }, :attachments])
@topic = @post.topic
@forum = @topic.forum

if !@post.belongs_to?(current_user) && !current_user.can?(:edit_posts, @forum)
flash[:notice] = t(:That_post_does_not_belong_to_you)
redirect_to forum_path(@forum)
end

# Cheat a little and use this to instantize @attachments.
@attachments ||= []
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/posts_controller.rb
Expand Up @@ -134,8 +134,4 @@ def check_ownership
redirect_back_or_default(forums_path)
end
end

def go_directly_to_post
redirect_to forum_topic_path(@post.forum,@topic) + "/#{@post.page_for(current_user)}" + "#post_#{@post.id}"
end
end
2 changes: 1 addition & 1 deletion app/controllers/topics_controller.rb
Expand Up @@ -47,7 +47,7 @@ def create
if @topic.save && @post.save
if params[:attachments] && current_user.can?(:use_attachments, @forum)
flash[:notice] = t(:Now_you_may_attach_files)
redirect_to topic_post_attachments_path(@topic, @post)
redirect_to new_topic_post_attachment_path(@topic, @post)
else
flash[:notice] = t(:created, :thing => "Topic")
redirect_to forum_topic_path(@topic.forum, @topic)
Expand Down
5 changes: 5 additions & 0 deletions app/models/post.rb
Expand Up @@ -88,6 +88,11 @@ def find_latest_post
end
end

def finished!
self.finished = true
save!
end

def editor
edits.last.user
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/topic.rb
Expand Up @@ -70,6 +70,11 @@ def to_s
subject
end

def finished!
self.finished = true
save!
end

def move!(new_forum_id, leave_redirect=false)
# May actually have selected a shadow redirect topic.
actual_topic = moved_to.nil? ? self : self.moved_to
Expand Down
12 changes: 7 additions & 5 deletions app/views/attachments/new.html.erb
@@ -1,17 +1,19 @@
<div class='action-box'>
<h2><%= t(:Attachments) %></h2>
<h2><%= t(:Attachments, :count => 0) %></h2>
<% flash.each do |key, value| %>
<div class='attachment_message <%= key %>'><%= value %></div>
<% end %>
<% form_for [@forum, @attachment], :html => { :multipart => true } do |f| %>
<% form_for [@topic, @post, @attachment], :html => { :multipart => true } do |f| %>
<%= hidden_field_tag :currently, params[:currently] %>
<%= f.label :file %>
<%= f.file_field :file %>
<%= f.submit "Upload" %>
<%= f.submit t(:Attach_this_and_more) %>
<%= f.submit t(:Attach_this_and_post) %>
<%= f.submit t(:Post) %>
<% end %>
<h3>
<h3><%= t(:Attachments, :count => @post.attachments.size) %></h3>
<ul>
<% @attachments.each do |attachment| %>
<% @post.attachments.each do |attachment| %>
<li><%= attachment.file_file_name %></li>
<% end %>
</ul>
Expand Down
5 changes: 4 additions & 1 deletion config/locales/en.rb
Expand Up @@ -116,6 +116,8 @@
:At_first_post => "At first post",
:At_last_post => "At last post",
:Attachments => { :zero => "Attachments", :one => "1 attachment", :other => "{{count}} attachments" },
:Attach_this_and_more => "Attach this and more",
:Attach_this_and_post => "Attach this and post",
:Author => "Author",
:Banned => "Banned",
:banned_by => "banned by {{user}}",
Expand Down Expand Up @@ -366,7 +368,7 @@
:Position => "Position",
:Post => "Post",
:Posts => "Posts",
:posts => { :one => "1 post", :zero => "no posts", :other => "{{count}} posts" },
:posts => { :one => "1 post", :zero => "no posts", :other => "{{count}} posts" },
:posts_per_topic => "Posts per topic",
:Posts_Required => "Posts Required",
:Posts_since_last_visit => "Posts since last visit",
Expand Down Expand Up @@ -432,6 +434,7 @@
:Systemwide_Permissions => "System-wide Permissions",
:Text => "Text",
:thanks_for_signing_up => "Thanks for signing up!",
:That_post_does_not_belong_to_you => "That post does not belong to you.",
:Theme => "Theme",
:Themes => "Themes",
:theme_is_now_default => "{{theme}} is now the default theme.",
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Expand Up @@ -83,7 +83,9 @@
map.resources :subscriptions

map.resources :topics, :member => { :reply => :get, :unlock => :put, :lock => :put } do |topic|
topic.resources :posts, :member => { :reply => :get }
topic.resources :posts, :member => { :reply => :get } do |post|
post.resources :attachments
end
topic.resources :subscriptions
topic.resources :reports
end
Expand Down
14 changes: 9 additions & 5 deletions features/attachments.feature
Expand Up @@ -15,11 +15,15 @@ Feature: Attachments
And I fill in "Text" with "Woah! Look at those attachments!"
And I check "I have attachments"
When I press "Create"
Then I should see "Now you may attach files to your post"
Then I should see "Attachments"
Then show me the page
When I attach the file at "../spec/fixtures/fugu.png" to "file"
And I press "Attach"
Then I should see "1 Attachment"
When I attach the file at "spec/fixtures/fugu.png" to "file"
And I press "Attach this and more"
Then I should see "1 attachment"
And I should see "fugu.png"
When I press "Attach this and post"
Then I should see "Please select a file to upload"
When I press "Post"
Then I should see "Look Ma, Attachments!"
Then I should see "Look Ma, Attachments!"
And I should see "1 attachment"
And I should see "fugu.png"

0 comments on commit 49ff3b1

Please sign in to comment.