move rake here, wrote another to clean data

This commit is contained in:
Torsten 2023-02-15 13:55:43 +02:00
parent a8fbf2d016
commit 38810859c5
5 changed files with 101 additions and 5 deletions

View File

@ -1,4 +1,5 @@
require "bundler/setup" require "bundler/setup"
require "merged"
load "rails/tasks/statistics.rake" load "rails/tasks/statistics.rake"

View File

@ -56,7 +56,6 @@ module ActionDispatch::Routing
next unless page.redirects next unless page.redirects
page.redirects.split.each do |old| page.redirects.split.each do |old|
next if old == page.name next if old == page.name
puts "redirect #{old} to #{page.name}"
get "/#{old}" => redirect("/#{page.name}") get "/#{old}" => redirect("/#{page.name}")
end end
end end

58
lib/tasks/condense.rake Normal file
View File

@ -0,0 +1,58 @@
module Merged
class Converter
def initialize
@page_mapping = linearize Page
@section_mapping = linearize Section
@card_mapping = linearize Card
@image_mapping = linearize Image
end
def linearize( model )
id_mapping = {}
count = 1
model.all.each do |m|
# map old to new
old_file = m.full_filename if model == Image
id_mapping[m.id] = count
m.id = count
FileUtils.mv( old_file , m.full_filename ) if model == Image
count = count + 1
end
id_mapping
end
def condense
fix_sections
fix_cards
[Page , Image , Section , Card].each {|m| m.save_all}
end
def fix_images
end
def fix_sections
Section.all.each do |section|
if( ! section.image_id.blank? )
section.image_id = @image_mapping[section.image_id]
end
section.page_id = @page_mapping[section.page_id]
end
end
def fix_cards
Card.all.each do |card|
if( ! card.image_id.blank? )
card.image_id = @image_mapping[card.image_id]
end
card.section_id = @section_mapping[card.section_id]
end
end
end
end
namespace :merged do
desc "Make ids consecutive on all models"
task :condense => :environment do
Merged::Converter.new.condense
end
end

View File

@ -0,0 +1,42 @@
require "rails"
require "merged"
namespace :merged do
desc "Check the data, usefull after hand edits"
task :consistency => :environment do
{Section: [:image, :page] , Card: [:section , :image]}.each do |name , keys|
Merged.check_class(name, keys)
end
Merged.check_images
end
end
module Merged
def self.check_class(name , ids)
clazz = self.const_get name.to_s
clazz.all.each do |elem|
ids.each do |id|
check_id(elem , id )
end
end
end
def self.check_id(elem , id)
attr = elem.attributes["#{id}_id".to_sym]
return if attr.blank?
begin
key = self.const_get(id.to_s.capitalize).find( attr.to_i)
rescue
puts "#{elem.class.name.split('::').last}:#{elem.id} missing #{id}:#{attr}"
end
end
def self.check_images
Image.all.each do |image|
next if File.exist?(image.full_filename)
puts "missing image #{image.full_filename}"
end
end
end

View File

@ -1,4 +0,0 @@
# desc "Explaining what the task does"
# task :merged do
# # Task goes here
# end