Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
include WorkChapterCountCaching
include CreationNotifier
include Creatable
include Responder

belongs_to :work, inverse_of: :chapters
# acts_as_list scope: 'work_id = #{work_id}'

acts_as_commentable
has_many :comments, as: :commentable # Handled in #delete_all_comments

Check warning on line 14 in app/models/chapter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Specify a `:dependent` option. Raw Output: app/models/chapter.rb:14:3: C: Rails/HasManyOrHasOneDependent: Specify a `:dependent` option.

validates_length_of :title, allow_blank: true, maximum: ArchiveConfig.TITLE_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.TITLE_MAX)
Expand Down Expand Up @@ -50,7 +52,13 @@
scope :in_order, -> { order(:position) }
scope :posted, -> { where(posted: true) }

before_destroy :fix_positions_before_destroy, :invalidate_chapter_count, :delete_all_comments
after_destroy :update_work_stats

after_save :fix_positions
after_save :invalidate_chapter_count, if: proc { |chapter| chapter.saved_change_to_posted? }
after_commit :update_series_index

def fix_positions
if work&.persisted?
positions_changed = false
Expand All @@ -76,18 +84,20 @@
end
end

after_save :invalidate_chapter_count,
if: Proc.new { |chapter| chapter.saved_change_to_posted? }

before_destroy :fix_positions_before_destroy, :invalidate_chapter_count
def fix_positions_before_destroy
if work&.persisted? && position
chapters = work.chapters.where(["position > ?", position])
chapters.each { |c| c.update_attribute(:position, c.position - 1) }
end
end

after_commit :update_series_index
def delete_all_comments
inbox_comments = InboxComment.where(feedback_comment_id: total_comments.pluck(:id))

total_comments.in_batches.delete_all
inbox_comments.in_batches.delete_all
end

def update_series_index
return unless work&.series.present? && should_reindex_series?
work.serial_works.each(&:update_series_index)
Expand Down
3 changes: 2 additions & 1 deletion app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
accepts_nested_attributes_for :challenge_claims

acts_as_commentable
has_many :comments, as: :commentable # Handled in chapters#delete_all_comments

Check warning on line 44 in app/models/work.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Specify a `:dependent` option. Raw Output: app/models/work.rb:44:3: C: Rails/HasManyOrHasOneDependent: Specify a `:dependent` option.
has_many :total_comments, class_name: 'Comment', through: :chapters
has_many :kudos, as: :commentable, dependent: :destroy
has_many :kudos, as: :commentable, dependent: :delete_all

has_many :original_creators, class_name: "WorkOriginalCreator", dependent: :destroy

Expand Down
3 changes: 2 additions & 1 deletion lib/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def get_work
work = self.commentable
elsif self.respond_to?(:bookmarkable)
work = self.bookmarkable
elsif self.respond_to?(:work)
work = self.work
end

work.is_a?(Work) ? work : nil
end
end

31 changes: 28 additions & 3 deletions spec/controllers/works/default_rails_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
suspended_user.update!(suspended: true, suspended_until: 1.week.from_now)
work
end

describe "before_action #clean_work_search_params" do
let(:params) { {} }

Expand Down Expand Up @@ -883,6 +883,8 @@ def call_with_params(params)

context "when a work has consecutive deleted comments in a thread" do
before do
work.kudos.create(user: create(:user)) # Add a kudo to the work

thread_depth = 4
chapter = work.first_chapter

Expand All @@ -900,12 +902,35 @@ def call_with_params(params)
fake_login_known_user(work.users.first)
end

it "deletes the work and redirects to the user's works with a notice" do
it "deletes the work and its associations, and redirects to the user's works with a notice" do
delete :destroy, params: { id: work.id }

it_redirects_to_with_notice(user_works_path(controller.current_user), "Your work #{work_title} was deleted.")
expect { work.reload }.to raise_exception(ActiveRecord::RecordNotFound)
expect(Kudo.count).to eq(0)
expect(Comment.count).to eq(0)
expect(InboxComment.count).to eq(0)
end
end

context "when a work has multiple chapters" do
before do
first_chapter = work.first_chapter
second_chapter = create(:chapter, work: work, position: 2)

create(:comment, commentable: first_chapter, parent: first_chapter)
create(:comment, commentable: second_chapter, parent: second_chapter)
fake_login_known_user(work.users.first)
end

it "deletes the work and its associations, and redirects to the user's works with a notice" do
delete :destroy, params: { id: work.id }

it_redirects_to_with_notice(user_works_path(controller.current_user), "Your work #{work_title} was deleted.")
expect { work.reload }
.to raise_exception(ActiveRecord::RecordNotFound)
expect(Comment.count).to eq(0)
expect(InboxComment.count).to eq(0)
end
end

Expand All @@ -917,7 +942,7 @@ def call_with_params(params)
it "errors and redirects to user page" do
fake_login_known_user(suspended_user)
delete :destroy, params: { id: suspended_users_work.id }

it_redirects_to_simple(user_path(suspended_user))
expect(flash[:error]).to include("Your account has been suspended")
end
Expand Down
Loading