mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
dry
This commit is contained in:
parent
7560e0bc0c
commit
63df0d880b
2 changed files with 29 additions and 44 deletions
29
lib/tasks/data.rake
Normal file
29
lib/tasks/data.rake
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
namespace :data do
|
||||
desc 'Nullify wrong foreign keys'
|
||||
|
||||
task events: :environment do
|
||||
# Track
|
||||
events_track = Event.all.select { |e| e.track_id && Track.find_by(id: e.track_id).nil? }
|
||||
nullify_attribute(events_track, 'track_id')
|
||||
|
||||
# Difficulty level
|
||||
events_difficulty_level = Event.all.select { |e| e.difficulty_level_id && DifficultyLevel.find_by(id: e.difficulty_level_id).nil? }
|
||||
nullify_attribute(events_difficulty_level, 'difficulty_level_id')
|
||||
|
||||
# Room
|
||||
events_room = Event.all.select { |e| e.room_id && Room.find_by(id: e.room_id).nil? }
|
||||
nullify_attribute(events_room, 'room_id')
|
||||
end
|
||||
|
||||
def nullify_attribute(collection, attribute)
|
||||
puts "Will nullify #{attribute} in #{ActionController::Base.helpers.pluralize(collection.length, 'event')}."
|
||||
if collection.any?
|
||||
puts "IDs: #{collection.map(&:id)}"
|
||||
collection.each do |item|
|
||||
item.send(attribute+'=', nil)
|
||||
item.save!
|
||||
end
|
||||
puts "Fixed #{attribute}!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
namespace :data do
|
||||
desc 'Nullifies wrong foreign keys for Track, Room, DifficultyLevel'
|
||||
|
||||
task fix_wrong_foreign_keys: :environment do
|
||||
# Tracks
|
||||
events = Event.all.select { |e| e.track_id && Track.find_by(id: e.track_id).nil? }
|
||||
puts "Will remove track_id from #{ActionController::Base.helpers.pluralize(events.length, 'event')}."
|
||||
|
||||
if events.any?
|
||||
puts "Event IDs: #{events.map(&:id)}"
|
||||
events.each do |event|
|
||||
event.track_id = nil
|
||||
event.save!
|
||||
end
|
||||
puts 'Fixed track_id!'
|
||||
end
|
||||
|
||||
# Difficulty levels
|
||||
events = Event.all.select { |e| e.difficulty_level_id && DifficultyLevel.find_by(id: e.difficulty_level_id).nil? }
|
||||
puts "Will remove difficulty_level_id from #{ActionController::Base.helpers.pluralize(events.length, 'event')}."
|
||||
|
||||
if events.any?
|
||||
puts "Event IDs: #{events.map(&:id)}"
|
||||
events.each do |event|
|
||||
event.difficulty_level_id = nil
|
||||
event.save!
|
||||
end
|
||||
puts 'Fixed difficulty_level_id!'
|
||||
end
|
||||
|
||||
#Rooms
|
||||
events = Event.all.select { |e| e.room_id && Room.find_by(id: e.room_id).nil? }
|
||||
puts "Will remove track_id from #{ActionController::Base.helpers.pluralize(events.length, 'event')}."
|
||||
|
||||
if events.any?
|
||||
puts "Event IDs: #{events.map(&:id)}"
|
||||
events.each do |event|
|
||||
event.room_id = nil
|
||||
event.save!
|
||||
end
|
||||
puts 'Fixed room_id!'
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue