Refactoring: Agora o sistema se comporta bem com objetos excluidos

This commit is contained in:
2008-03-15 07:44:46 -03:00
parent 8d02e0d4ce
commit 4c55ce7b45
16 changed files with 114 additions and 96 deletions

View File

@@ -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

View 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

View File

@@ -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

View File

@@ -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 = {})

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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