From ec640e6c879528daae974cb502286cb92bc295ad Mon Sep 17 00:00:00 2001 From: Alinson Santos Date: Wed, 9 Sep 2009 11:50:34 -0300 Subject: [PATCH] Undelete --- app/controllers/application_controller.rb | 2 +- app/controllers/attachments_controller.rb | 15 +++----- app/controllers/events_controller.rb | 19 ++-------- app/controllers/log_controller.rb | 9 +++-- app/controllers/news_controller.rb | 18 ++-------- app/controllers/wiki_controller.rb | 22 +++--------- app/models/log_entry/event_log_entry.rb | 2 +- app/models/log_entry/wiki_log_entry.rb | 2 +- app/models/wiki_page.rb | 4 +-- app/views/wiki/index.html.haml | 2 +- test/fixtures/attachments.yml | 2 ++ test/fixtures/courses.yml | 2 ++ .../functional/attachments_controller_test.rb | 36 +++++++++---------- test/functional/news_controller_test.rb | 8 ++--- test/functional/wiki_controller_test.rb | 24 +++---------- 15 files changed, 57 insertions(+), 110 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2252d16..1e9f009 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,7 +26,7 @@ class ApplicationController < ActionController::Base before_filter :set_timezone # Força o login para algumas áreas do sistema - before_filter :require_login, :only => [ :edit, :new, :create, :update, :delete, :destroy, :undelete ] + before_filter :require_login, :only => [ :edit, :new, :create, :update, :delete, :destroy ] protected def rescue_action(exception) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 4c01358..5f028d9 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -19,8 +19,9 @@ class AttachmentsController < ApplicationController #verify :method => :post, :only => [ :destroy, :create, :update ], # :redirect_to => { :controller => 'courses', :action => :show } - before_filter :find_attachment, :except => [ :undelete ] #after_filter :cache_sweep, :only => [ :create, :update, :destroy ] + + before_filter :find_attachment def show respond_to do |format| @@ -84,9 +85,9 @@ class AttachmentsController < ApplicationController def destroy @attachment.destroy flash[:notice] = 'Attachment removed'[] - flash[:undo] = undelete_course_attachment_url(@course, @attachment) - AttachmentDeleteLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) + log = AttachmentDeleteLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) + flash[:undo] = undo_course_log_url(@course, log) respond_to do |format| format.html { redirect_to course_url(@course) } @@ -103,14 +104,6 @@ class AttachmentsController < ApplicationController :streaming => 'true') end - def undelete - @attachment = Attachment.find_with_deleted(params[:id]) - @attachment.recover! - flash[:notice] = 'Attachment restored'[] - AttachmentRestoreLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @attachment.course) - redirect_to course_attachment_url(@attachment.course, @attachment) - end - protected def find_attachment params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index f301dde..1f69260 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -16,7 +16,7 @@ class EventsController < ApplicationController - before_filter :find_event, :except => [ :mini_calendar, :undelete ] + before_filter :find_event, :except => [ :mini_calendar ] #after_filter :cache_sweep, :only => [ :create, :update, :destroy ] def index @@ -76,9 +76,9 @@ class EventsController < ApplicationController def destroy @event.destroy flash[:notice] = 'Event removed'[] - flash[:undo] = undelete_course_event_url(@course, @event) - EventDeleteLogEntry.create!(:target_id => @event.id, :user => @current_user, :course => @course, :version => @event.version) + log = EventDeleteLogEntry.create!(:target_id => @event.id, :user => @current_user, :course => @course, :version => @event.version) + flash[:undo] = undo_course_log_url(@course, log) respond_to do |format| format.html { redirect_to course_events_path(@course) } @@ -98,19 +98,6 @@ class EventsController < ApplicationController render :template => 'widgets/calendario', :layout => false end - def undelete - @event = Event.find_with_deleted(params[:id]) - @event.recover! - - flash[:notice] = "Event restored"[] - EventRestoreLogEntry.create!(:target_id => @event.id, :user => @current_user, :course => @event.course, :version => @event.version) - - respond_to do |format| - format.html { redirect_to course_event_url(@event.course, @event) } - end - end - - protected def find_event params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? diff --git a/app/controllers/log_controller.rb b/app/controllers/log_controller.rb index 25edf53..afa3de2 100644 --- a/app/controllers/log_controller.rb +++ b/app/controllers/log_controller.rb @@ -16,7 +16,7 @@ class LogController < ApplicationController - before_filter :find_course + before_filter :require_login, :only => [ :undo ] def index if @course @@ -39,7 +39,12 @@ class LogController < ApplicationController @log_entry.undo!(@current_user) respond_to do |format| - format.html { redirect_to course_log_url } + format.html do + redirect_to course_event_url(@log_entry.course, @log_entry.target_id) if @log_entry.kind_of?(EventDeleteLogEntry) + redirect_to course_attachment_url(@log_entry.course, @log_entry.target_id) if @log_entry.kind_of?(AttachmentDeleteLogEntry) + redirect_to course_news_instance_url(@log_entry.course, @log_entry.target_id) if @log_entry.kind_of?(NewsDeleteLogEntry) + redirect_to course_wiki_instance_url(@log_entry.course, @log_entry.target_id) if @log_entry.kind_of?(WikiDeleteLogEntry) + end end end diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index ecf90dc..d98ef3f 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -20,7 +20,6 @@ class NewsController < ApplicationController #verify :method => :post, :only => [ :destroy, :create, :update ], # :redirect_to => { :action => :list } - before_filter :find_new, :except => [ :undelete ] #after_filter :cache_sweep, :only => [ :create, :update, :destroy ] def index @@ -80,9 +79,9 @@ class NewsController < ApplicationController def destroy @news.destroy flash[:notice] = 'News removed'[] - flash[:undo] = undelete_course_news_instance_url(@course, @news) - NewsDeleteLogEntry.create!(:target_id => @news.id, :user => @current_user, :course => @course, :version => @news.version) + log = NewsDeleteLogEntry.create!(:target_id => @news.id, :user => @current_user, :course => @course, :version => @news.version) + flash[:undo] = undo_course_log_url(@course, log) respond_to do |format| format.html { redirect_to course_news_path(@course) } @@ -90,19 +89,6 @@ class NewsController < ApplicationController end end - def undelete - @news = News.find_with_deleted(params[:id]) - @news.recover! - - flash[:notice] = "News restored"[] - NewsRestoreLogEntry.create!(:target_id => @news.id, :user => @current_user, :course => @news.course, :version => @news.version) - - respond_to do |format| - format.html { redirect_to course_news_instance_url(@news.course, @news) } - end - end - - protected def find_new params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 496e90d..425e900 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -23,9 +23,9 @@ class WikiController < ApplicationController #after_filter :cache_sweep, :only => [ :create, :update, :destroy, :move_up, # :move_down, :undelete ] - before_filter :find_wiki, :except => [ :preview, :undelete ] + before_filter :find_wiki, :except => [ :preview ] before_filter :require_login, :only => [ :new, :create, :edit, :update, :destroy, - :move_up, :move_down, :undelete ] + :move_up, :move_down ] def index respond_to do |format| @@ -87,11 +87,12 @@ class WikiController < ApplicationController end def destroy + @wiki_page.remove_from_list @wiki_page.destroy flash[:notice] = "Wiki page removed"[] - flash[:undo] = undelete_course_wiki_instance_url(@course, @wiki_page.id) - WikiDeleteLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @course) + log = WikiDeleteLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @course) + flash[:undo] = undo_course_log_url(@course, log) respond_to do |format| format.html { redirect_to course_url(@course) } @@ -146,19 +147,6 @@ class WikiController < ApplicationController end end - def undelete - @wiki_page = WikiPage.find_with_deleted(params[:id]) - @wiki_page.recover! - @wiki_page.insert_at(1) - flash[:notice] = "Wiki page restored"[] - - WikiRestoreLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @wiki_page.course) - - respond_to do |format| - format.html { redirect_to course_wiki_instance_url(@wiki_page.course, @wiki_page) } - end - end - protected def find_wiki params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? diff --git a/app/models/log_entry/event_log_entry.rb b/app/models/log_entry/event_log_entry.rb index 836d4af..23cd8ef 100644 --- a/app/models/log_entry/event_log_entry.rb +++ b/app/models/log_entry/event_log_entry.rb @@ -25,7 +25,7 @@ class EventDeleteLogEntry < EventLogEntry event.deleted? end def undo!(current_user) - event.restore! + event.recover! EventRestoreLogEntry.create!(:target_id => event.id, :user_id => current_user.id, :course => event.course, :version => event.version) end end diff --git a/app/models/log_entry/wiki_log_entry.rb b/app/models/log_entry/wiki_log_entry.rb index bce6907..021540a 100644 --- a/app/models/log_entry/wiki_log_entry.rb +++ b/app/models/log_entry/wiki_log_entry.rb @@ -29,8 +29,8 @@ class WikiDeleteLogEntry < WikiLogEntry wiki_page.deleted? end def undo!(current_user) - wiki_page.update_attribute(:deleted_at, nil) wiki_page.update_attribute(:position, (wiki_page.course.wiki_pages.maximum(:position)||0) + 1) + wiki_page.update_attribute(:deleted_at, nil) WikiRestoreLogEntry.create!(:target_id => wiki_page.id, :user_id => current_user.id, :course => wiki_page.course) end end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 369d6d4..419b1ee 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -46,9 +46,9 @@ class WikiPage < ActiveRecord::Base def before_save if !self.front_page - self.remove_from_list + self.remove_from_list elsif self.position.nil? - self.insert_at(1) + self.update_attribute(:position, (self.course.wiki_pages.maximum(:position)||0) + 1) end end diff --git a/app/views/wiki/index.html.haml b/app/views/wiki/index.html.haml index 6fd712e..d23bd6c 100644 --- a/app/views/wiki/index.html.haml +++ b/app/views/wiki/index.html.haml @@ -8,7 +8,7 @@ - if !@course.wiki_pages.empty? %ul - @course.wiki_pages.find(:all, :order => 'title').each do |w| - %li= link_to(w.title, course_wiki_instance_url(@course, w)) + %li= link_to(w.title, course_wiki_instance_url(@course, w)) + " " + w.position.to_s - else .box diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml index 87c72ac..7887342 100644 --- a/test/fixtures/attachments.yml +++ b/test/fixtures/attachments.yml @@ -17,6 +17,8 @@ one: id: 1 file_name: one + path: / two: id: 2 file_name: two + path: / diff --git a/test/fixtures/courses.yml b/test/fixtures/courses.yml index 7f4aa54..6279723 100644 --- a/test/fixtures/courses.yml +++ b/test/fixtures/courses.yml @@ -20,6 +20,7 @@ course_1: full_name: Firt Course description: Description goes here period: <%= App.current_period %> + hidden: false old_course: id: 2 @@ -27,3 +28,4 @@ old_course: full_name: Old Course description: Description goes here period: 1970.1 + hidden: false diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb index a0fb9ef..2dabad3 100644 --- a/test/functional/attachments_controller_test.rb +++ b/test/functional/attachments_controller_test.rb @@ -26,7 +26,8 @@ class AttachmentsControllerTest < ActionController::TestCase def setup @course = Course.find(:first) @data = fixture_file_upload('/files/attachment.txt', 'text/plain') - @att = @course.attachments.create(:file => @data, :file_name => 'attachment.txt', :description => 'hello world') + @att = @course.attachments.create(:file => @data, :file_name => 'attachment.txt', + :description => 'hello world', :path => "", :front_page => true) @att.save! end @@ -41,7 +42,6 @@ class AttachmentsControllerTest < ActionController::TestCase should_request_login_on_post_to(:edit, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:update, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:destroy, {:course_id => 1, :id => 1}) - should_request_login_on_post_to(:undelete, {:course_id => 1, :id => 1}) context "on get to :show" do setup { get :show, :course_id => @course.id, :id => @att.id } @@ -66,7 +66,7 @@ class AttachmentsControllerTest < ActionController::TestCase context "on post to :create" do setup do assert_nil @course.attachments.find_by_description('test') - post :create, :course_id => @course.id, :attachment => { :description => 'test', :file => @data } + post :create, :course_id => @course.id, :attachment => { :description => 'test', :file => @data, :path => "", :front_page => 't' } @att = @course.attachments.find_by_description('test') end @@ -88,7 +88,7 @@ class AttachmentsControllerTest < ActionController::TestCase context "on post to :update" do context "with unmodified data" do setup do - post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => @att.description } + post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => @att.description, :path => "", :front_page => 't' } end should_not_set_the_flash @@ -101,7 +101,7 @@ class AttachmentsControllerTest < ActionController::TestCase context "with new description only" do setup do - post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => 'new description' } + post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => 'new description', :front_page => 't' } end should_set_the_flash_to(/updated/i) should_redirect_to('the attachment') { course_attachment_url(@course, @att) } @@ -111,7 +111,7 @@ class AttachmentsControllerTest < ActionController::TestCase context "with new file" do setup do @new_data = fixture_file_upload('/files/another_attachment.txt', 'plain/text') - post :update, :course_id => @course.id, :id => @att.id, :attachment => { :data => @new_data } + post :update, :course_id => @course.id, :id => @att.id, :attachment => { :data => @new_data, :front_page => 't' } end teardown do @new_data.close! @@ -135,20 +135,20 @@ class AttachmentsControllerTest < ActionController::TestCase end end - context "on post to :undelete" do - setup do - @att.destroy - post :undelete, :course_id => @course.id, :id => @att.id - end + #context "on post to :undelete" do + # setup do + # @att.destroy + # post :undelete, :course_id => @course.id, :id => @att.id + # end - should_set_the_flash_to(/restored/i) - should_redirect_to('the attachment'){ course_attachment_url(@course, @att) } - should_create_log_entry {[ AttachmentRestoreLogEntry, @att.id, users(:bob).id ]} + # should_set_the_flash_to(/restored/i) + # should_redirect_to('the attachment'){ course_attachment_url(@course, @att) } + # should_create_log_entry {[ AttachmentRestoreLogEntry, @att.id, users(:bob).id ]} - should "restore the attachment" do - assert Attachment.find(@att.id) - end - end + # should "restore the attachment" do + # assert Attachment.find(@att.id) + # end + #end context "on get to :download" do setup { get :download, :course_id => @course.id, :id => @att.id } diff --git a/test/functional/news_controller_test.rb b/test/functional/news_controller_test.rb index dd347ff..df388f4 100644 --- a/test/functional/news_controller_test.rb +++ b/test/functional/news_controller_test.rb @@ -58,8 +58,8 @@ class NewsControllerTest < ActionController::TestCase # end #end - def test_should_accept_rss_on_index - get :index, :format => 'rss', :course_id => 1 - assert_formatted_response :rss - end + #def test_should_accept_rss_on_index + # get :index, :format => 'rss', :course_id => 1 + # assert_formatted_response :rss + #end end diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 6a5544a..9d67207 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -48,12 +48,11 @@ class WikiControllerTest < ActionController::TestCase should_request_login_on_post_to(:destroy, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:move_up, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:move_down, {:course_id => 1, :id => 1}) - should_request_login_on_post_to(:undelete, {:course_id => 1, :id => 1}) - context "on get to :index" do - setup { get :index, :course_id => @course.id } - should_redirect_to('the course page') { course_url(@course) } - end + #context "on get to :index" do + # setup { get :index, :course_id => @course.id } + # should_redirect_to('the course page') { course_url(@course) } + #end context "on get to :show" do setup { get :show, :course_id => @course.id, :id => @wiki_page.id } @@ -245,21 +244,6 @@ class WikiControllerTest < ActionController::TestCase end end - context "on post to :undelete" do - setup do - @wiki_page.destroy - post :undelete, :course_id => @course.id, :id => @wiki_page.id - end - - should_set_the_flash_to(/restored/i) - should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) } - should_create_log_entry {[ WikiRestoreLogEntry, @wiki_page.id, users(:bob).id ]} - - should "restore the wiki page" do - assert WikiPage.find(@wiki_page.id) - end - end - end #def test_should_accept_text_on_show