better PSQL compatibility for aggregated journals

pull/3230/head
Jan Sandbrink 9 years ago
parent f02b32faad
commit 5f06a8900d
  1. 7
      app/models/journal/aggregated_journal.rb
  2. 31
      spec/models/journal/aggregated_journal_spec.rb

@ -158,4 +158,11 @@ class Journal::AggregatedJournal < Journal
def initial?
predecessor.nil?
end
# ARs automagic addition of dynamic columns (those not present in the physical table) seems
# not to work with PostgreSQL and simply return a string for unknown columns.
# Thus we need to ensure manually that this column is correctly casted.
def notes_id
ActiveRecord::ConnectionAdapters::Column.value_to_integer(attributes['notes_id'])
end
end

@ -30,29 +30,36 @@
require 'spec_helper'
RSpec::Matchers.define :be_equivalent_to_journal do |expected|
expected_attributes = expected.attributes.symbolize_keys
if expected_attributes[:created_at]
# µs are not stored in DB
expected_attributes[:created_at] = expected_attributes[:created_at].change(usec: 0)
end
ignored_attributes = [:notes_id]
match do |actual|
actual_attributes = actual.attributes.symbolize_keys
expected_attributes.except(*ignored_attributes) == actual_attributes.except(*ignored_attributes)
end
expected_attributes = get_normalized_attributes expected
actual_attributes = get_normalized_attributes actual
def display_sorted_hash(hash)
'{ ' + hash.sort.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(', ') + ' }'
expected_attributes.except(*ignored_attributes) == actual_attributes.except(*ignored_attributes)
end
failure_message do |actual|
expected_attributes = get_normalized_attributes expected
actual_attributes = actual.attributes.symbolize_keys
["expected attributes: #{display_sorted_hash(expected_attributes.except(*ignored_attributes))}",
"actual attributes: #{display_sorted_hash(actual_attributes.except(*ignored_attributes))}"]
.join($/)
end
def get_normalized_attributes(journal)
result = journal.attributes.symbolize_keys
if result[:created_at]
# µs are not stored in all DBMS
result[:created_at] = result[:created_at].change(usec: 0)
end
result
end
def display_sorted_hash(hash)
'{ ' + hash.sort.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(', ') + ' }'
end
end
describe Journal::AggregatedJournal, type: :model do
@ -63,7 +70,7 @@ describe Journal::AggregatedJournal, type: :model do
let(:user2) { FactoryGirl.create(:user) }
let(:initial_author) { user1 }
subject { described_class.all }
subject { described_class.order('id ASC').to_a }
before do
allow(User).to receive(:current).and_return(initial_author)

Loading…
Cancel
Save