Refactoring: Agora o sistema se comporta bem com objetos excluidos
This commit is contained in:
@@ -100,8 +100,8 @@ class WikiController < ApplicationController
|
||||
end
|
||||
|
||||
def versions
|
||||
@history_to = params[:to] || @wiki_page.versions.count
|
||||
@history_from = params[:from] || @wiki_page.versions.count - 1
|
||||
@history_to = params[:to] || @wiki_page.versions[-1].version
|
||||
@history_from = params[:from] || (@wiki_page.versions.count > 1 ? @wiki_page.versions[-2].version : @history_to)
|
||||
@offset = params[:offset] || 0
|
||||
|
||||
respond_to do |format|
|
||||
|
||||
@@ -18,10 +18,15 @@ require 'fileutils.rb'
|
||||
|
||||
class Attachment < ActiveRecord::Base
|
||||
|
||||
belongs_to :course
|
||||
generate_validations
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
|
||||
# Associacoes
|
||||
belongs_to :course
|
||||
|
||||
# Validacao
|
||||
generate_validations
|
||||
|
||||
# Atributo virtual file
|
||||
def file=(new_file)
|
||||
@tmp_file = new_file
|
||||
|
||||
@@ -16,10 +16,13 @@
|
||||
|
||||
class Course < ActiveRecord::Base
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
|
||||
# Associacoes
|
||||
has_many :attachments,
|
||||
:order => "file_name",
|
||||
:dependent => :destroy
|
||||
:dependent => :destroy
|
||||
|
||||
has_many :events,
|
||||
:order => "time asc",
|
||||
@@ -27,8 +30,8 @@ class Course < ActiveRecord::Base
|
||||
|
||||
has_many :news,
|
||||
:foreign_key => "receiver_id",
|
||||
:order => "id desc",
|
||||
:dependent => :destroy
|
||||
:order => "id desc",
|
||||
:dependent => :destroy
|
||||
|
||||
has_many :log_entries,
|
||||
:order => "created_at desc",
|
||||
@@ -37,10 +40,7 @@ class Course < ActiveRecord::Base
|
||||
has_many :wiki_pages,
|
||||
:order => "position",
|
||||
:dependent => :destroy
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
|
||||
|
||||
# Validacao
|
||||
generate_validations
|
||||
validates_uniqueness_of :short_name
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class Event < ActiveRecord::Base
|
||||
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
generate_validations
|
||||
|
||||
# Associacoes
|
||||
belongs_to :course
|
||||
|
||||
# Validacao
|
||||
generate_validations
|
||||
|
||||
def Event.to_ical(courses)
|
||||
courses = [courses] unless courses.kind_of?(Array)
|
||||
cal = Icalendar::Calendar.new
|
||||
|
||||
@@ -15,11 +15,14 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class LogEntry < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :course
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
|
||||
# Associacoes
|
||||
belongs_to :course
|
||||
belongs_to :user, :with_deleted => true
|
||||
|
||||
def reversible?() false end
|
||||
|
||||
def to_xml(options = {})
|
||||
|
||||
@@ -15,21 +15,18 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class AttachmentLogEntry < LogEntry
|
||||
def attachment
|
||||
Attachment.find_with_deleted(target_id)
|
||||
end
|
||||
belongs_to :attachment,
|
||||
:foreign_key => "target_id",
|
||||
:with_deleted => true
|
||||
end
|
||||
|
||||
class AttachmentDeleteLogEntry < AttachmentLogEntry
|
||||
def reversible?()
|
||||
a = Attachment.find_with_deleted(target_id)
|
||||
a.deleted_at != nil
|
||||
attachment.deleted?
|
||||
end
|
||||
def undo!(current_user)
|
||||
a = Attachment.find_with_deleted(target_id)
|
||||
a.update_attribute(:deleted_at, nil)
|
||||
AttachmentRestoreLogEntry.create!(:target_id => a.id, :user_id => current_user.id,
|
||||
:course => a.course)
|
||||
attachment.update_attribute(:deleted_at, nil)
|
||||
AttachmentRestoreLogEntry.create!(:target_id => attachment.id, :user_id => current_user.id, :course => attachment.course)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,21 +15,18 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class EventLogEntry < LogEntry
|
||||
def event
|
||||
Event.find_with_deleted(target_id)
|
||||
end
|
||||
belongs_to :event,
|
||||
:foreign_key => "target_id",
|
||||
:with_deleted => true
|
||||
end
|
||||
|
||||
class EventDeleteLogEntry < EventLogEntry
|
||||
def reversible?()
|
||||
e = Event.find_with_deleted(target_id)
|
||||
e.deleted_at != nil
|
||||
event.deleted?
|
||||
end
|
||||
def undo!(current_user)
|
||||
e = Event.find_with_deleted(target_id)
|
||||
e.update_attribute(:deleted_at, nil)
|
||||
EventRestoreLogEntry.create!(:target_id => e.id, :user_id => current_user.id,
|
||||
:course_id => e.course_id)
|
||||
event.update_attribute(:deleted_at, nil)
|
||||
EventRestoreLogEntry.create!(:target_id => event.id, :user_id => current_user.id, :course => event.course)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,21 +15,18 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class NewsLogEntry < LogEntry
|
||||
def news
|
||||
News.find_with_deleted(target_id)
|
||||
end
|
||||
belongs_to :news,
|
||||
:foreign_key => "target_id",
|
||||
:with_deleted => true
|
||||
end
|
||||
|
||||
class NewsDeleteLogEntry < NewsLogEntry
|
||||
def reversible?()
|
||||
n = News.find_with_deleted(target_id)
|
||||
n.deleted_at != nil
|
||||
news.deleted?
|
||||
end
|
||||
def undo!(current_user)
|
||||
n = News.find_with_deleted(target_id)
|
||||
n.update_attribute(:deleted_at, nil)
|
||||
NewsRestoreLogEntry.create!(:target_id => n.id, :user_id => current_user.id,
|
||||
:course => n.course)
|
||||
news.update_attribute(:deleted_at, nil)
|
||||
NewsRestoreLogEntry.create!(:target_id => news.id, :user_id => current_user.id, :course => news.course)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class WikiLogEntry < LogEntry
|
||||
def wiki_page
|
||||
w = WikiPage.find_with_deleted(target_id)
|
||||
w.revert_to(version)
|
||||
return w
|
||||
end
|
||||
belongs_to :wiki_page,
|
||||
:foreign_key => "target_id",
|
||||
:with_deleted => true
|
||||
end
|
||||
|
||||
class WikiEditLogEntry < WikiLogEntry
|
||||
@@ -28,16 +26,12 @@ end
|
||||
|
||||
class WikiDeleteLogEntry < WikiLogEntry
|
||||
def reversible?()
|
||||
w = WikiPage.find_with_deleted(target_id)
|
||||
w.deleted_at != nil
|
||||
wiki_page.deleted?
|
||||
end
|
||||
def undo!(current_user)
|
||||
w = WikiPage.find_with_deleted(target_id)
|
||||
w.update_attribute(:deleted_at, nil)
|
||||
w.position = w.course.wiki_pages.maximum(:position) + 1
|
||||
w.save!
|
||||
WikiRestoreLogEntry.create!(:target_id => w.id, :user_id => current_user.id,
|
||||
:course => w.course)
|
||||
wiki_page.update_attribute(:deleted_at, nil)
|
||||
wiki_page.update_attribute(:position, wiki_page.course.wiki_pages.maximum(:position) + 1)
|
||||
WikiRestoreLogEntry.create!(:target_id => wiki_page.id, :user_id => current_user.id, :course => wiki_page.course)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,28 +15,19 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
class Message < ActiveRecord::Base
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
belongs_to :user, :foreign_key => "sender_id"
|
||||
|
||||
# Associacoes
|
||||
belongs_to :user,
|
||||
:foreign_key => "sender_id",
|
||||
:with_deleted => true
|
||||
|
||||
end
|
||||
|
||||
|
||||
class PrivateMessage < Message
|
||||
end
|
||||
|
||||
|
||||
class News < Message
|
||||
validates_presence_of :title
|
||||
belongs_to :course, :foreign_key => "receiver_id"
|
||||
end
|
||||
|
||||
|
||||
class ShoutboxMessage < Message
|
||||
end
|
||||
|
||||
|
||||
class CourseShoutboxMessage < ShoutboxMessage
|
||||
end
|
||||
|
||||
|
||||
class UserShoutboxMessage < ShoutboxMessage
|
||||
belongs_to :course,
|
||||
:foreign_key => "receiver_id"
|
||||
end
|
||||
|
||||
@@ -18,10 +18,13 @@ require 'digest/sha1'
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
|
||||
# Associacoes
|
||||
has_and_belongs_to_many :courses, :order => 'full_name'
|
||||
|
||||
# Validacao
|
||||
validates_length_of :login, :within => 3..40
|
||||
validates_length_of :name, :within => 3..40
|
||||
validates_length_of :display_name, :within => 3..40
|
||||
@@ -35,6 +38,7 @@ class User < ActiveRecord::Base
|
||||
validates_format_of :email,
|
||||
:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
|
||||
|
||||
# Seguranca
|
||||
attr_protected :id, :salt
|
||||
attr_accessor :password, :password_confirmation
|
||||
|
||||
|
||||
@@ -19,25 +19,29 @@ require 'tempfile'
|
||||
|
||||
class WikiPage < ActiveRecord::Base
|
||||
|
||||
belongs_to :course
|
||||
belongs_to :user
|
||||
|
||||
generate_validations
|
||||
validates_uniqueness_of :title, :scope => :course_id
|
||||
validates_format_of :title, :with => /^[^0-9]/
|
||||
|
||||
# Plugins
|
||||
acts_as_paranoid
|
||||
acts_as_list :scope => 'course_id = #{course_id}'
|
||||
acts_as_versioned :if_changed => [ :content, :description, :title ]
|
||||
self.non_versioned_fields << 'position'
|
||||
|
||||
def validate
|
||||
begin
|
||||
to_html
|
||||
rescue
|
||||
errors.add("content", "possui erro de sintaxe")
|
||||
end
|
||||
end
|
||||
# Associacoes
|
||||
belongs_to :course
|
||||
belongs_to :user, :with_deleted => true
|
||||
|
||||
# Valicacao
|
||||
generate_validations
|
||||
validates_uniqueness_of :title, :scope => :course_id
|
||||
validates_format_of :title, :with => /^[^0-9]/
|
||||
|
||||
|
||||
def validate
|
||||
begin
|
||||
to_html
|
||||
rescue
|
||||
errors.add("content", "possui erro de sintaxe")
|
||||
end
|
||||
end
|
||||
|
||||
def to_html(text = self.content)
|
||||
return BlueCloth.new(text).to_html
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
= "Página " + link_to(h(entry.wiki_page.versions.last.title), course_wiki_url(entry.course, entry.wiki_page.id, :version => entry.wiki_page.version))
|
||||
= "Página " + link_to(h(entry.wiki_page.title), course_wiki_url(entry.course, entry.wiki_page.id, :version => entry.version))
|
||||
|
||||
= "criada " if entry.kind_of?(WikiCreateLogEntry)
|
||||
= "editada " if entry.kind_of?(WikiEditLogEntry)
|
||||
= "excluída " if entry.kind_of?(WikiDeleteLogEntry)
|
||||
= "restaurada " if entry.kind_of?(WikiRestoreLogEntry)
|
||||
|
||||
- current_version = WikiPage.find_version(entry.wiki_page.id, entry.version)
|
||||
- previous_version = entry.wiki_page.previous_version(entry.version)
|
||||
|
||||
- if entry.kind_of?(WikiEditLogEntry)
|
||||
- if entry.wiki_page.description and !entry.wiki_page.description.empty?
|
||||
= "(<i>#{h(entry.wiki_page.description)}</i>)"
|
||||
= "(" + link_to("diff", diff_course_wiki_url(entry.course, entry.wiki_page.id, :from => entry.wiki_page.version - 1, :to => entry.wiki_page.version)) + ")"
|
||||
= "(" + link_to("edit", edit_course_wiki_url(entry.course, entry.wiki_page.id, :description => "Revertendo para versão #{entry.wiki_page.version}", :version => entry.wiki_page.version)) + ")"
|
||||
= "(" + link_to("undo", edit_course_wiki_url(entry.course, entry.wiki_page.id, :description => "Revertendo para versão #{entry.wiki_page.version-1}", :version => entry.wiki_page.version - 1)) + ")"
|
||||
- if current_version.description and !current_version.description.empty?
|
||||
= "(<i>#{h(current_version.description)}</i>)"
|
||||
|
||||
- unless previous_version.nil?
|
||||
= "(" + link_to("diff", diff_course_wiki_url(entry.course, entry.wiki_page.id, :from => previous_version.version, :to => entry.version)) + ")"
|
||||
|
||||
= "(" + link_to("edit", edit_course_wiki_url(entry.course, entry.wiki_page.id, :description => "Revertendo para versão #{entry.version}", :version => entry.version)) + ")"
|
||||
|
||||
- unless previous_version.nil?
|
||||
= "(" + link_to("undo", edit_course_wiki_url(entry.course, entry.wiki_page.id, :description => "Revertendo para versão #{previous_version.version}", :version => previous_version.version)) + ")"
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
%td.narrow
|
||||
%input{:type => "radio", :name => "to", :value => entry.version, :onclick => %"history_to(#{entry.version})"}
|
||||
%td= link_to(tz(entry.updated_at).strftime("%d/%m/%y %H:%M:%S"), course_wiki_url(@course, @wiki_page, :version => entry.version))
|
||||
%td= link_to truncate(h(User.find(entry.user_id).display_name), 20), user_path(User.find(entry.user_id))
|
||||
%td= link_to truncate(h(User.find_with_deleted(entry.user_id).display_name), 20), user_path(User.find_with_deleted(entry.user_id))
|
||||
%td
|
||||
= entry.description
|
||||
- if (entry.version > @wiki_page.versions.minimum(:version))
|
||||
|
||||
@@ -234,4 +234,14 @@ class UserTest < Test::Unit::TestCase
|
||||
assert_not_equal "I-want-to-set-my-salt", u.salt
|
||||
assert_equal "verybadbob", u.login
|
||||
end
|
||||
|
||||
def test_paranoid
|
||||
assert User.paranoid?
|
||||
|
||||
u = users(:bob)
|
||||
u.destroy
|
||||
|
||||
assert_raises(ActiveRecord::RecordNotFound) { User.find(u.id) }
|
||||
assert_nothing_raised { User.find_with_deleted(u.id) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -279,6 +279,10 @@ module ActiveRecord #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
def previous_version(current_version = self.version)
|
||||
self.versions.find(:first, :conditions => [ 'version < ?', current_version ], :order => 'version desc')
|
||||
end
|
||||
|
||||
protected
|
||||
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
|
||||
def set_new_version
|
||||
@@ -394,4 +398,4 @@ module ActiveRecord #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Versioned }
|
||||
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Versioned }
|
||||
|
||||
Reference in New Issue
Block a user