Skip to content

Commit

Permalink
Slightly refactor presence matcher
Browse files Browse the repository at this point in the history
...in preparation for detecting models that support the full Attributes
API more closely.
  • Loading branch information
mcmire committed Jul 7, 2019
1 parent 677af78 commit 51b45b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
55 changes: 15 additions & 40 deletions lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def disallows_original_or_typecast_value?(value)
end

def disallowed_values
if collection?
if collection_association?
[Array.new]
else
values = []
Expand All @@ -251,16 +251,6 @@ def disallowed_values
end
end

def collection?
if association_reflection
[:has_many, :has_and_belongs_to_many].include?(
association_reflection.macro,
)
else
false
end
end

def should_add_footnote_about_belongs_to?
belongs_to_association_being_validated? &&
presence_validation_exists_on_attribute?
Expand Down Expand Up @@ -316,18 +306,27 @@ def belongs_to_association_configured_to_be_required?
end

def belongs_to_association_being_validated?
!!association_reflection &&
association_reflection.macro == :belongs_to
association? && association_reflection.macro == :belongs_to
end

def attribute_accepts_string_values?
!association_reflection && (
!association? && (
!attribute_type.respond_to?(:coder) ||
!attribute_type.coder ||
attribute_type.coder.object_class == String
)
end

def association?
association_reflection.present?
end

def collection_association?
association? && association_reflection.macro.in?(
[:has_many, :has_and_belongs_to_many],
)
end

def association_name
association_reflection.name
end
Expand All @@ -337,16 +336,11 @@ def association_options
end

def association_reflection
model.respond_to?(:reflect_on_association) &&
model.reflect_on_association(@attribute)
model.try(:reflect_on_association, @attribute)
end

def attribute_type
if model.respond_to?(:attribute_types)
model.attribute_types[@attribute.to_s]
else
LegacyAttributeType.new(model, @attribute)
end
RailsShim.attribute_type_for(model, @attribute)
end

def presence_validation_exists_on_attribute?
Expand All @@ -356,25 +350,6 @@ def presence_validation_exists_on_attribute?
def model
@subject.class
end

class LegacyAttributeType
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
end

private

attr_reader :model, :attribute_name
end
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/shoulda/matchers/rails_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def secure_password_module
nil
end

def attribute_type_for(model, attribute_name)
if model.respond_to?(:attribute_types)
model.attribute_types[attribute_name.to_s]
else
LegacyAttributeType.new(model, attribute_name)
end
end

private

def simply_generate_validation_message(
Expand All @@ -179,6 +187,25 @@ def simply_generate_validation_message(
{ default: default_translation_keys }.merge(options)
I18n.translate(primary_translation_key, translate_options)
end

class LegacyAttributeType
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
end

private

attr_reader :model, :attribute_name
end
end
end
end
Expand Down

0 comments on commit 51b45b1

Please sign in to comment.