Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/super_diff/active_record/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
class ActiveRecord::Base
# TODO: Remove this monkey patch if possible
def attributes_for_super_diff
id_attr = self.class.primary_key
id_attrs = Array(self.class.primary_key).compact
other_attrs = attributes.keys.sort - id_attrs
attributes_to_hash = ->(keys, hash) { keys.reduce(hash) { |memo, key| memo.merge(key.to_sym => attributes[key]) } }

(attributes.keys.sort - [id_attr]).reduce(
id_attr.nil? ? {} : { id_attr.to_sym => id }
) { |hash, key| hash.merge(key.to_sym => attributes[key]) }
attributes_to_hash.call(id_attrs, {})
.then { |result| attributes_to_hash.call(other_attrs, result) }
end
end
# rubocop:enable Style/ClassAndModuleChildren
1 change: 1 addition & 0 deletions spec/support/models/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.define_tables
SuperDiff::Test::Models::ActiveRecord::Person.define_table
SuperDiff::Test::Models::ActiveRecord::ShippingAddress.define_table
SuperDiff::Test::Models::ActiveRecord::TimeSeriesData.define_table
SuperDiff::Test::Models::ActiveRecord::Order.define_table
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/support/models/active_record/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module SuperDiff
module Test
module Models
module ActiveRecord
class Order < ::ActiveRecord::Base
def self.define_table
::ActiveRecord::Base
.connection
.create_table(
:orders,
force: true,
primary_key: %i[store_id sku]
) do |t|
t.integer :latest_outcome, null: false
t.string :details
t.integer :store_id, null: false
t.string :sku, null: false
end
end
end
end
end
end
end
44 changes: 44 additions & 0 deletions spec/support/shared_examples/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@
end
end

context 'when comparing two instances of an ActiveRecord model that has a composite primary key' do
it 'produces the correct output' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = SuperDiff::Test::Models::ActiveRecord::Order.new(
store_id: 456,
sku: 'hi',
latest_outcome: 1,
)
expected = SuperDiff::Test::Models::ActiveRecord::Order.new(
store_id: 123,
sku: 'hello',
latest_outcome: 2,
)
expect(actual).to eq(expected)
TEST
program = make_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: 'expect(actual).to eq(expected)',
newline_before_expectation: true,
expectation:
proc do
line do
plain 'Expected '
actual %(#<SuperDiff::Test::Models::ActiveRecord::Order ["store_id", "sku"]: [456, "hi"], details: nil, latest_outcome: 1, sku: "hi", store_id: 456>)
end

line do
plain ' to eq '
expected %(#<SuperDiff::Test::Models::ActiveRecord::Order ["store_id", "sku"]: [123, "hello"], details: nil, latest_outcome: 2, sku: "hello", store_id: 123>)
end
end
)

expect(program).to produce_output_when_run(expected_output).in_color(
color_enabled
)
end
end
end

context 'when comparing instances of two different ActiveRecord models' do
it 'produces the correct output' do
as_both_colored_and_uncolored do |color_enabled|
Expand Down
Loading