diff --git a/Rakefile b/Rakefile index 8577623..a140d0a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,5 @@ require "bundler/setup" +require "merged" load "rails/tasks/statistics.rake" diff --git a/lib/merged/engine.rb b/lib/merged/engine.rb index eaf38fd..0272b16 100644 --- a/lib/merged/engine.rb +++ b/lib/merged/engine.rb @@ -56,7 +56,6 @@ module ActionDispatch::Routing next unless page.redirects page.redirects.split.each do |old| next if old == page.name - puts "redirect #{old} to #{page.name}" get "/#{old}" => redirect("/#{page.name}") end end diff --git a/lib/tasks/condense.rake b/lib/tasks/condense.rake new file mode 100644 index 0000000..29c59d4 --- /dev/null +++ b/lib/tasks/condense.rake @@ -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 diff --git a/lib/tasks/consistency.rake b/lib/tasks/consistency.rake new file mode 100644 index 0000000..a9b2114 --- /dev/null +++ b/lib/tasks/consistency.rake @@ -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 diff --git a/lib/tasks/merged_tasks.rake b/lib/tasks/merged_tasks.rake deleted file mode 100644 index 7693645..0000000 --- a/lib/tasks/merged_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :merged do -# # Task goes here -# end