Refactoring due to codeclimate issues

This commit is contained in:
Sergei Tsõganov 2022-03-29 11:16:09 +03:00
parent eb64b3aca4
commit b2bfc94121
2 changed files with 36 additions and 26 deletions

View file

@ -43,32 +43,20 @@ class RegistrantUser < User
def update_contacts def update_contacts
user = self user = self
contacts = [] contacts = []
contacts.concat Contact.with_different_registrant_name(user) contacts.concat(Contact.with_different_registrant_name(user).each do |c|
.each{ |c| c.write_attribute(:name, user.username) } c.write_attribute(:name, user.username)
end)
companies.each do |company| companies.each do |company|
contacts.concat Contact.with_different_company_name(company) contacts.concat(Contact.with_different_company_name(company).each do |c|
.each{ |c| c.write_attribute(:name, company.company_name) } c.write_attribute(:name, company.company_name)
end)
end end
return [] if contacts.blank? return [] if contacts.blank?
grouped_contacts = contacts.group_by(&:registrar_id) group_and_bulk_update(contacts)
grouped_contacts.each do |registrar_id, contacts|
bulk_action, action = actions.create!(operation: :bulk_update) if contacts.size > 1
contacts.each do |c|
if c.save(validate: false)
action = actions.create!(contact: c, operation: :update, bulk_action_id: bulk_action&.id)
end
end
notify_registrar_contacts_updated(action: bulk_action || action,
registrar_id: registrar_id)
end
contacts
end
def notify_registrar_contacts_updated(action:, registrar_id:) contacts
registrar = Registrar.find(registrar_id)
registrar.notify(action) if registrar
end end
def contacts(representable: true) def contacts(representable: true)
@ -146,4 +134,25 @@ class RegistrantUser < User
user user
end end
end end
private
def group_and_bulk_update(contacts)
grouped_contacts = contacts.group_by(&:registrar_id)
grouped_contacts.each do |registrar_id, reg_contacts|
bulk_action, action = actions.create!(operation: :bulk_update) if reg_contacts.size > 1
reg_contacts.each do |c|
if c.save(validate: false)
action = actions.create!(contact: c, operation: :update, bulk_action_id: bulk_action&.id)
end
end
notify_registrar_contacts_updated(action: bulk_action || action,
registrar_id: registrar_id)
end
end
def notify_registrar_contacts_updated(action:, registrar_id:)
registrar = Registrar.find(registrar_id)
registrar&.notify(action)
end
end end

View file

@ -8,9 +8,10 @@ builder.extension do
builder.tag!('changePoll:date', obj.created_at.utc.xmlschema) builder.tag!('changePoll:date', obj.created_at.utc.xmlschema)
builder.tag!('changePoll:svTRID', obj.id) builder.tag!('changePoll:svTRID', obj.id)
builder.tag!('changePoll:who', obj.user) builder.tag!('changePoll:who', obj.user)
if obj.bulk_action? builder.tag!(
builder.tag!('changePoll:reason', 'Auto-update according to official data') 'changePoll:reason',
end 'Auto-update according to official data'
) if obj.bulk_action?
when 'state' when 'state'
builder.tag!('changePoll:operation', obj) builder.tag!('changePoll:operation', obj)
end end