From 6d902e07d908769a909db0bf93d84f5be28720af Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Wed, 13 Jun 2018 13:21:12 +0200 Subject: [PATCH] Fix shoulda-matchers in Rails 5 Backports a fix for https://github.com/thoughtbot/shoulda-matchers/issues/913. This can be removed once new shoulda-matchers version is released. --- spec/support/shoulda/matchers/rails_shim.rb | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/support/shoulda/matchers/rails_shim.rb diff --git a/spec/support/shoulda/matchers/rails_shim.rb b/spec/support/shoulda/matchers/rails_shim.rb new file mode 100644 index 00000000000..8d70598beb5 --- /dev/null +++ b/spec/support/shoulda/matchers/rails_shim.rb @@ -0,0 +1,27 @@ +# monkey patch which fixes serialization matcher in Rails 5 +# https://github.com/thoughtbot/shoulda-matchers/issues/913 +# This can be removed when a new version of shoulda-matchers +# is released +module Shoulda + module Matchers + class RailsShim + def self.serialized_attributes_for(model) + if defined?(::ActiveRecord::Type::Serialized) + # Rails 5+ + serialized_columns = model.columns.select do |column| + model.type_for_attribute(column.name).is_a?( + ::ActiveRecord::Type::Serialized + ) + end + + serialized_columns.inject({}) do |hash, column| # rubocop:disable Style/EachWithObject + hash[column.name.to_s] = model.type_for_attribute(column.name).coder + hash + end + else + model.serialized_attributes + end + end + end + end +end