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