Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove section renaming utilities.

They will no longer work now the section field: if we need their
functionality again, we can write similar tools to work around tags.
  • Loading branch information...
commit cbed96c691f8fdd949fede122b708cea340c6f01 1 parent 2296021
David Thompson authored
View
46 lib/bulk_section_setter.rb
@@ -1,46 +0,0 @@
-class BulkSectionSetter
- attr_accessor :artefacts
-
- def initialize(filename, logger = nil)
- @artefacts = load(filename)
- @logger = logger || NullLogger.instance
- end
-
- def load(filename)
- artefacts = []
- section = nil
- subsection = nil
- File.open(filename, 'r').each do |line|
- if line =~ /^ /
- artefacts << {
- title: line.strip,
- subsection: subsection,
- section: section
- }
- elsif line =~ /^ /
- subsection = line.strip
- else
- section = line.strip
- end
- end
- artefacts
- end
-
- def set_all
- @artefacts.each do |record|
- artefact = Artefact.find_by_name(record[:title])
- section = "#{record[:section]}:#{record[:subsection]}"
- if artefact
- artefact.section = section
- begin
- artefact.save!
- @logger.info("Set '#{record[:title]}' to set section '#{section}'")
- rescue => e
- @logger.warn("Couldn't save '#{record[:title]}' because '#{e}'")
- end
- else
- @logger.warn("Couldn't find '#{record[:title]}' to set section '#{section}'")
- end
- end
- end
-end
View
12 lib/tasks/bulk_set_sections.rake
@@ -1,12 +0,0 @@
-namespace :sections do
-
- desc "Set sections in bulk (specify DOCUMENT_SECTIONS_FILE)"
- task :bulk_set => :environment do
- raise "Specify DOCUMENT_SECTIONS_FILE" unless ENV['DOCUMENT_SECTIONS_FILE']
- raise "DOCUMENT_SECTIONS_FILE '#{ENV['DOCUMENT_SECTIONS_FILE']} not found" unless File.exist?(ENV['DOCUMENT_SECTIONS_FILE'])
-
- require 'bulk_section_setter'
- s = BulkSectionSetter.new(ENV['DOCUMENT_SECTIONS_FILE'], Logger.new(STDOUT))
- s.set_all
- end
-end
View
27 lib/tasks/sections.rake
@@ -1,27 +0,0 @@
-namespace :sections do
-
- desc "Migrate section assignments to new sections"
- task :migrate => :environment do
-
- @section_migrations = {
- 'Driving:Owning a car/motorbike' => 'Driving:Owning a car or motorbike',
- 'Education:In schools' => 'Education:In school',
- 'Family:Separation and divorce' => 'Family:Divorce and separation',
- 'Driving:Buying/selling a vehicle'=> 'Driving:Buying and selling a vehicle'
- }
-
- Artefact.all.each do |a|
-
- current_section = a.section
- new_section = @section_migrations[current_section]
-
- if !new_section or new_section.nil?
- puts "Not updating \"#{a.name}\" (\"#{current_section}\")".colorize(:white)
- elsif a.update_attribute(:section, new_section)
- puts "Updated \"#{a.name}\" from \"#{current_section}\" to \"#{new_section}\"".colorize(:green)
- else
- puts "Error: Could not update \"#{a.name}\", existing section \"#{current_section}\"".colorize(:red)
- end
- end
- end
-end
View
3  test/fixtures/sample_bulk_section_settings.txt
@@ -1,3 +0,0 @@
-Section
- SubSection
- Article Title
View
46 test/unit/bulk_section_setter_test.rb
@@ -1,46 +0,0 @@
-require 'test_helper'
-require 'bulk_section_setter'
-
-class BulkSectionSetterTest < ActiveSupport::TestCase
- def sample_bulk_section_settings_file
- File.expand_path("../fixtures/sample_bulk_section_settings.txt", File.dirname(__FILE__))
- end
-
- test "Reads settings from a file" do
- setter = BulkSectionSetter.new(sample_bulk_section_settings_file)
- assert_equal 1, setter.artefacts.size
- expected_artefact = {title: "Article Title", section: "Section", subsection: "SubSection"}
- assert_equal expected_artefact, setter.artefacts.first
- end
-
- test "Apply section to artefacts which can be found" do
- artefact = stub_everything("Article")
- Artefact.expects(:find_by_name).with('Article Title').returns(artefact)
- s = sequence("setting section")
- artefact.expects(:section=).with("Section:SubSection").in_sequence(s)
- artefact.expects(:save!).in_sequence(s)
-
- setter = BulkSectionSetter.new(sample_bulk_section_settings_file)
- setter.set_all
- end
-
- test "Keep a log of artefacts which could not be found" do
- logger = stub("Logger")
- Artefact.stubs(:find_by_name).returns(nil)
- logger.expects(:warn).with("Couldn't find 'Article Title' to set section 'Section:SubSection'")
-
- setter = BulkSectionSetter.new(sample_bulk_section_settings_file, logger)
- setter.set_all
- end
-
- test "Catch validation errors on save" do
- artefact = stub("Article")
- artefact.stubs(:section=)
- artefact.stubs(:save!).raises("something bad happened")
- Artefact.expects(:find_by_name).returns(artefact)
- logger = stub("Logger")
- logger.expects(:warn).with(regexp_matches(/Couldn't save 'Article Title' because /))
- setter = BulkSectionSetter.new(sample_bulk_section_settings_file, logger)
- setter.set_all
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.