Permalink
Browse files

Fixing two broken-on-test bugs: coauthor assignment was broken and wo…

…rk delete emails were not going out
  • Loading branch information...
elzj committed May 12, 2012
1 parent 10f170d commit 2ee4a0de95572b2e3271ec00ee5b01cb8dc2d86c
Showing with 31 additions and 23 deletions.
  1. +2 −1 app/controllers/works_controller.rb
  2. +8 −1 app/models/work.rb
  3. +21 −21 app/models/work_observer.rb
@@ -10,7 +10,8 @@ class WorksController < ApplicationController
# this only works to check ownership of a SINGLE item and only if load_work has happened beforehand
before_filter :check_ownership, :except => [ :index, :show, :navigate, :new, :create, :import, :show_multiple, :edit_multiple, :update_multiple, :delete_multiple, :search, :marktoread, :drafts ]
before_filter :check_visibility, :only => [ :show, :navigate ]
- before_filter :set_author_attributes, :only => [ :edit, :update, :manage_chapters, :preview, :show, :navigate ]
+ # NOTE: new and create need set_author_attributes or coauthor assignment will break!
+ before_filter :set_author_attributes, :only => [ :new, :create, :edit, :update, :manage_chapters, :preview, :show, :navigate ]
before_filter :set_instance_variables, :only => [ :new, :create, :edit, :update, :manage_chapters, :preview, :show, :navigate, :import ]
before_filter :set_instance_variables_tags, :only => [ :edit_tags, :update_tags, :preview_tags ]
View
@@ -9,7 +9,9 @@ class Work < ActiveRecord::Base
# ASSOCIATIONS
########################################################################
- has_many :creatorships, :as => :creation, :dependent => :destroy
+ # creatorships can't have dependent => destroy because we email the
+ # user in a before_destroy callback
+ has_many :creatorships, :as => :creation
has_many :pseuds, :through => :creatorships
has_many :users, :through => :pseuds, :uniq => true
@@ -198,6 +200,11 @@ def check_for_invalid_chapters
def destroy_chapters_in_reverse
self.chapters.order("position DESC").map(&:destroy)
end
+
+ after_destroy :clean_up_creatorships
+ def clean_up_creatorships
+ self.creatorships.each{ |c| c.destroy }
+ end
def self.purge_old_drafts
draft_ids = Work.where('works.posted = ? AND works.created_at < ?', false, 1.week.ago).value_of(:id)
@@ -2,31 +2,31 @@ class WorkObserver < ActiveRecord::Observer
# TODO: Email a copy of the previous version of the work to all co-authors
def before_update(work)
- # users = work.pseuds.collect(&:user).uniq
- # orphan_account = User.orphan_account
- # unless users.blank?
- # for user in users
- # unless user.preference.edit_emails_off? || user == orphan_account
- # UserMailer.edit_work_notification(user, work).deliver!
- # end
- # end
- # end
+ # users = work.pseuds.collect(&:user).uniq
+ # orphan_account = User.orphan_account
+ # unless users.blank?
+ # for user in users
+ # unless user.preference.edit_emails_off? || user == orphan_account
+ # UserMailer.edit_work_notification(user, work).deliver!
+ # end
+ # end
+ # end
end
# Email a copy of the deleted work to all co-authors
def before_destroy(work)
- if work.posted?
- users = work.pseuds.collect(&:user).uniq
- orphan_account = User.orphan_account
- unless users.blank?
- for user in users
- unless user == orphan_account
- # this has to use the synchronous version because the work is going to be destroyed
- UserMailer.delete_work_notification(user, work).deliver!
- end
- end
- end
- end
+ if work.posted?
+ users = work.pseuds.collect(&:user).uniq
+ orphan_account = User.orphan_account
+ unless users.blank?
+ for user in users
+ unless user == orphan_account
+ # this has to use the synchronous version because the work is going to be destroyed
+ UserMailer.delete_work_notification(user, work).deliver!
+ end
+ end
+ end
+ end
end
end

0 comments on commit 2ee4a0d

Please sign in to comment.