Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Last active September 10, 2024 15:53
Show Gist options
  • Save mabsboza/f9ce8513becd93ad37e397b1d9f9c9f6 to your computer and use it in GitHub Desktop.
Save mabsboza/f9ce8513becd93ad37e397b1d9f9c9f6 to your computer and use it in GitHub Desktop.
class MetaContractorObject
VALID_SUB_CATEGORIES = ['PERMANENTE', 'ESPORÁDICO', 'HABITUAL'].freeze
attr_accessor :sub_category, :meta_contractor_id
def initialize(meta_contractor_id, sub_category)
raise ArgumentError, "Invalid sub_category: #{sub_category}. Must be one of: #{VALID_SUB_CATEGORIES.join(', ')}" unless VALID_SUB_CATEGORIES.include?(sub_category)
@meta_contractor_id = meta_contractor_id
@sub_category = sub_category
end
end
# Initializing meta contractors from Excel data
meta_contractors = [
MetaContractorObject.new(24,'PERMANENTE'),
MetaContractorObject.new(25,'PERMANENTE'),
MetaContractorObject.new(80,'ESPORÁDICO'),
MetaContractorObject.new(81,'PERMANENTE'),
MetaContractorObject.new(84,'ESPORÁDICO'),
MetaContractorObject.new(87,'PERMANENTE'),
MetaContractorObject.new(95,'PERMANENTE'),
MetaContractorObject.new(96,'PERMANENTE'),
MetaContractorObject.new(99,'PERMANENTE'),
MetaContractorObject.new(100,'PERMANENTE')
]
puts "Total Meta Contractors: #{meta_contractors.count}"
meta_contractors.each.with_index do |meta_contractor, index|
puts "Processing MetaContractor #{index + 1}"
meta_contractor_record = MetaContractor.find_by(id: meta_contractor.meta_contractor_id)
unless meta_contractor_record
puts "MetaContractor with ID #{meta_contractor.meta_contractor_id} not found."
next
end
sub_category_record = MetaContractorSubcategory.find_by(name: meta_contractor.sub_category)
ActiveRecord::Base.transaction do
if sub_category_record
meta_contractor_record.update(subcategory_id: sub_category_record.id)
puts "Updated MetaContractor #{meta_contractor_record.id} with SubCategory #{sub_category_record.id}."
else
puts "SubCategory '#{meta_contractor.sub_category}' not found for MetaContractor #{meta_contractor_record.id}."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment