Solve both sides of the missing fields bug, not only one

This commit is contained in:
Maciej Szlosarczyk 2018-04-25 16:03:13 +03:00
parent cec05c3943
commit a82db7a67a
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
7 changed files with 21 additions and 11 deletions

View file

@ -1,10 +1,15 @@
module ObjectVersionsHelper
def attach_existing_fields(version, new_object)
version.object_changes.to_h.each do |k, v|
method_name = "#{k}=".to_sym
version.object_changes.to_h.each do |key, value|
method_name = "#{key}=".to_sym
if new_object.respond_to?(method_name)
new_object.public_send(method_name, v.last)
new_object.public_send(method_name, value.last)
end
end
end
def only_present_fields(version, model)
field_names = model.column_names
version.object.to_h.select { |key, _value| field_names.include?(key) }
end
end

View file

@ -57,8 +57,9 @@
%tbody
- @versions.each do |version|
- if version
- contact = Contact.new(version.object.to_h)
- attach_existing_fields(version, contact)
- attributes = only_present_fields(version, Contact)
- contact = Contact.new(attributes)
- attach_existing_fields(version, contact)
%tr
%td= link_to(contact.name, admin_contact_version_path(version.id))

View file

@ -1,4 +1,5 @@
- contact = Contact.new(@version.object.to_h)
- attributes = only_present_fields(@version, Contact)
- contact = Contact.new(attributes)
- attach_existing_fields(@version, contact)
= render 'shared/title', name: contact.name

View file

@ -55,7 +55,8 @@
%tbody
- @versions.each do |version|
- if version
- domain = Domain.new(version.object.to_h)
- attributes = only_present_fields(version, Domain)
- domain = Domain.new(attributes)
- attach_existing_fields(version, domain)
%tr

View file

@ -1,4 +1,5 @@
- domain = Domain.new(@version.object.to_h)
- present_fields = only_present_fields(@version, Domain)
- domain = Domain.new(present_fields)
- attach_existing_fields(@version, domain)
- if @version