Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tidy task #1449

Merged
merged 2 commits into from
May 18, 2018
Merged
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
10 changes: 9 additions & 1 deletion lib/alchemy/tasks/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,20 @@ def remove_orphaned_contents
end
end

def remove_duplicate_folded_pages
Alchemy::FoldedPage.select(:page_id, :user_id).group(:page_id, :user_id).having("count(*) > 1").each do |duplicate_page|
duplicate_page_ids = Alchemy::FoldedPage.where(duplicate_page.attributes.except("id")).pluck(:id)
duplicate_page_ids_except_oldest = duplicate_page_ids.reject { |i| i == duplicate_page_ids.min }
Alchemy::FoldedPage.where(id: duplicate_page_ids_except_oldest).delete_all
end
end

private

def destroy_orphaned_records(records, class_name)
records.each do |record|
log "Destroy orphaned #{class_name}: #{record.inspect}"
record.destroy
record.reload.try(:destroy)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/alchemy/tidy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace :alchemy do
Rake::Task['alchemy:tidy:element_positions'].invoke
Rake::Task['alchemy:tidy:content_positions'].invoke
Rake::Task['alchemy:tidy:remove_orphaned_records'].invoke
Rake::Task['alchemy:tidy:remove_duplicate_folded_pages'].invoke
end

desc "Creates missing cells for pages."
Expand Down Expand Up @@ -56,5 +57,10 @@ namespace :alchemy do
task remove_orphaned_contents: [:environment] do
Alchemy::Tidy.remove_orphaned_contents
end

desc "Remove duplicate folded pages."
task remove_duplicate_folded_pages: [:environment] do
Alchemy::Tidy.remove_duplicate_folded_pages
end
end
end