Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix presence matcher w/ custom serializer #1236

Merged
merged 1 commit into from
Aug 1, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ def belongs_to_association_being_validated?
def attribute_accepts_string_values?
if association?
false
elsif attribute_serializer
attribute_serializer.object_class == String
elsif attribute_serialization_coder.respond_to?(:object_class)
attribute_serialization_coder.object_class == String
else
attribute_type.try(:type) == :string
RailsShim.supports_full_attributes_api?(model) &&
attribute_type.try(:type) == :string
end
end

Expand Down Expand Up @@ -355,12 +356,8 @@ def model_has_associations?(associations)
end
end

def attribute_serializer
if attribute_type.respond_to?(:coder)
attribute_type.coder
else
nil
end
def attribute_serialization_coder
RailsShim.attribute_serialization_coder_for(model, @attribute)
end

def attribute_type
Expand Down
8 changes: 2 additions & 6 deletions lib/shoulda/matchers/active_record/serialize_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,11 @@ def expectation
end

def attribute_is_serialized?
serialized_attributes.include?(@name)
!!serialization_coder
end

def serialization_coder
serialized_attributes[@name]
end

def serialized_attributes
Shoulda::Matchers::RailsShim.serialized_attributes_for(model)
RailsShim.attribute_serialization_coder_for(model, @name)
end

def model
Expand Down
65 changes: 38 additions & 27 deletions lib/shoulda/matchers/rails_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,20 @@ def make_controller_request(context, verb, action, request_params)
end

def 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,
)
attribute_types_for(model).
inject({}) do |hash, (attribute_name, attribute_type)|
if attribute_type.is_a?(::ActiveRecord::Type::Serialized)
hash.merge(attribute_name => attribute_type.coder)
else
hash
end
end
rescue NotImplementedError
{}
end

serialized_columns.inject({}) do |hash, column|
hash[column.name.to_s] = model.type_for_attribute(column.name).coder
hash
end
else
model.serialized_attributes
end
def attribute_serialization_coder_for(model, attribute_name)
serialized_attributes_for(model)[attribute_name.to_s]
end

def type_cast_default_for(model, column)
Expand Down Expand Up @@ -156,14 +155,35 @@ def secure_password_module
nil
end

def attribute_types_for(model)
if model.respond_to?(:attribute_types)
model.attribute_types
elsif model.respond_to?(:type_for_attribute)
model.columns.inject({}) do |hash, column|
key = column.name.to_s
value = model.type_for_attribute(column.name)
hash.merge(key => value)
end
else
raise NotImplementedError
end
end

def attribute_type_for(model, attribute_name)
if supports_full_attributes_api?(model)
model.attribute_types[attribute_name.to_s]
attribute_types_for(model)[attribute_name.to_s]
rescue NotImplementedError
if model.respond_to?(:type_for_attribute)
model.type_for_attribute(attribute_name)
else
LegacyAttributeType.new(model, attribute_name)
FakeAttributeType.new(model, attribute_name)
end
end

def supports_full_attributes_api?(model)
defined?(::ActiveModel::Attributes) &&
model.respond_to?(:attribute_types)
end

private

def simply_generate_validation_message(
Expand All @@ -188,23 +208,14 @@ def simply_generate_validation_message(
I18n.translate(primary_translation_key, translate_options)
end

def supports_full_attributes_api?(model)
defined?(::ActiveModel::Attributes) &&
model.respond_to?(:attribute_types)
end

class LegacyAttributeType
class FakeAttributeType
def initialize(model, attribute_name)
@model = model
@attribute_name = attribute_name
end

def coder
if model.respond_to?(:serialized_attributes)
ActiveSupport::Deprecation.silence do
model.serialized_attributes[attribute_name.to_s]
end
end
nil
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,50 @@
end

context 'when the attribute is decorated with serialize' do
context 'and the type is a string' do
context 'and the serializer is a built-in Ruby type' do
context 'and the type is a string' do
it 'still works' do
record = record_validating_presence_of(:traits) do
serialize :traits, String
end

expect(record).to validate_presence_of(:traits)
end
end

context 'and the type is not a string' do
it 'still works' do
record = record_validating_presence_of(:traits) do
serialize :traits, Array
end

expect(record).to validate_presence_of(:traits)
end
end
end

context 'and the serializer is JSON' do
it 'still works' do
record = record_validating_presence_of(:traits) do
serialize :traits, String
serialize :traits, JSON
end

expect(record).to validate_presence_of(:traits)
end
end

context 'and the type is not a string' do
context 'and the serializer is something custom' do
it 'still works' do
record = record_validating_presence_of(:traits) do
serialize :traits, Array
serializer = Class.new do
define_singleton_method(:dump) { |value| value }
define_singleton_method(:load) { |value| value }
end

expect(record).to validate_presence_of(:traits)
record = record_validating_presence_of(:data) do
serialize :data, serializer
end

expect(record).to validate_presence_of(:data)
end
end
end
Expand Down