2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2013-05-10 18:10:46 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
# @private
|
2017-09-18 00:51:41 +00:00
|
|
|
module RailsShim
|
2017-09-17 19:33:33 +00:00
|
|
|
class << self
|
2017-01-09 12:31:46 +00:00
|
|
|
def action_pack_gte_5?
|
|
|
|
Gem::Requirement.new('>= 5').satisfied_by?(action_pack_version)
|
|
|
|
end
|
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
def action_pack_lt_5?
|
|
|
|
Gem::Requirement.new('< 5').satisfied_by?(action_pack_version)
|
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def action_pack_version
|
|
|
|
Gem::Version.new(::ActionPack::VERSION::STRING)
|
2017-09-18 00:51:41 +00:00
|
|
|
rescue NameError
|
|
|
|
Gem::Version.new('0')
|
2017-09-17 19:33:33 +00:00
|
|
|
end
|
|
|
|
|
2017-10-02 05:19:03 +00:00
|
|
|
def active_record_gte_5?
|
|
|
|
Gem::Requirement.new('>= 5').satisfied_by?(active_record_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_record_version
|
|
|
|
Gem::Version.new(::ActiveRecord::VERSION::STRING)
|
2017-09-18 00:51:41 +00:00
|
|
|
rescue NameError
|
|
|
|
Gem::Version.new('0')
|
2014-12-24 21:46:49 +00:00
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def generate_validation_message(
|
|
|
|
record,
|
|
|
|
attribute,
|
|
|
|
type,
|
|
|
|
model_name,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
if record && record.errors.respond_to?(:generate_message)
|
|
|
|
record.errors.generate_message(attribute.to_sym, type, options)
|
|
|
|
else
|
|
|
|
simply_generate_validation_message(
|
|
|
|
attribute,
|
|
|
|
type,
|
|
|
|
model_name,
|
|
|
|
options,
|
2016-10-18 00:02:20 +00:00
|
|
|
)
|
2014-12-24 21:46:49 +00:00
|
|
|
end
|
2017-09-17 19:33:33 +00:00
|
|
|
rescue RangeError
|
|
|
|
simply_generate_validation_message(
|
|
|
|
attribute,
|
|
|
|
type,
|
|
|
|
model_name,
|
|
|
|
options,
|
|
|
|
)
|
2014-12-24 21:46:49 +00:00
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def make_controller_request(context, verb, action, request_params)
|
|
|
|
params =
|
2017-01-09 12:31:46 +00:00
|
|
|
if action_pack_gte_5?
|
2017-09-17 19:33:33 +00:00
|
|
|
{ params: request_params }
|
|
|
|
else
|
|
|
|
request_params
|
|
|
|
end
|
|
|
|
|
|
|
|
context.__send__(verb, action, params)
|
2015-01-21 22:46:26 +00:00
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def serialized_attributes_for(model)
|
2019-07-26 05:31:21 +00:00
|
|
|
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
|
2017-09-17 19:33:33 +00:00
|
|
|
end
|
2019-07-26 05:31:21 +00:00
|
|
|
rescue NotImplementedError
|
|
|
|
{}
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
2019-07-26 05:31:21 +00:00
|
|
|
def attribute_serialization_coder_for(model, attribute_name)
|
|
|
|
serialized_attributes_for(model)[attribute_name.to_s]
|
2016-06-28 15:43:41 +00:00
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def tables_and_views(connection)
|
2017-10-02 05:19:03 +00:00
|
|
|
if active_record_gte_5?
|
2017-09-17 19:33:33 +00:00
|
|
|
connection.data_sources
|
|
|
|
else
|
|
|
|
connection.tables
|
|
|
|
end
|
|
|
|
end
|
2013-12-16 17:33:19 +00:00
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
def verb_for_update
|
2020-01-02 03:10:35 +00:00
|
|
|
:patch
|
2017-09-17 19:33:33 +00:00
|
|
|
end
|
|
|
|
|
2017-10-02 05:19:03 +00:00
|
|
|
def validation_message_key_for_association_required_option
|
|
|
|
if active_record_gte_5?
|
|
|
|
:required
|
|
|
|
else
|
|
|
|
:blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-21 05:24:02 +00:00
|
|
|
def parent_of(mod)
|
|
|
|
if mod.respond_to?(:module_parent)
|
|
|
|
mod.module_parent
|
|
|
|
else
|
|
|
|
mod.parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_secure_password?(record, attribute_name)
|
|
|
|
if secure_password_module
|
|
|
|
attribute_name == :password &&
|
|
|
|
record.class.ancestors.include?(secure_password_module)
|
|
|
|
else
|
|
|
|
record.respond_to?("authenticate_#{attribute_name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def digestible_attributes_in(record)
|
|
|
|
record.methods.inject([]) do |array, method_name|
|
|
|
|
match = method_name.to_s.match(
|
|
|
|
/\A(\w+)_(?:confirmation|digest)=\Z/,
|
|
|
|
)
|
|
|
|
|
|
|
|
if match
|
|
|
|
array.concat([match[1].to_sym])
|
|
|
|
else
|
|
|
|
array
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def secure_password_module
|
|
|
|
::ActiveModel::SecurePassword::InstanceMethodsOnActivation
|
|
|
|
rescue NameError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-07-26 05:31:21 +00:00
|
|
|
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
|
|
|
|
|
2019-07-07 02:30:56 +00:00
|
|
|
def attribute_type_for(model, attribute_name)
|
2019-07-26 05:31:21 +00:00
|
|
|
attribute_types_for(model)[attribute_name.to_s]
|
|
|
|
rescue NotImplementedError
|
|
|
|
if model.respond_to?(:type_for_attribute)
|
|
|
|
model.type_for_attribute(attribute_name)
|
2019-07-07 02:30:56 +00:00
|
|
|
else
|
2019-07-26 05:31:21 +00:00
|
|
|
FakeAttributeType.new(model, attribute_name)
|
2019-07-07 02:30:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 05:31:21 +00:00
|
|
|
def supports_full_attributes_api?(model)
|
|
|
|
defined?(::ActiveModel::Attributes) &&
|
|
|
|
model.respond_to?(:attribute_types)
|
|
|
|
end
|
|
|
|
|
2017-09-17 19:33:33 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def simply_generate_validation_message(
|
|
|
|
attribute,
|
|
|
|
type,
|
|
|
|
model_name,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
default_translation_keys = [
|
2019-01-30 05:58:03 +00:00
|
|
|
:"activemodel.errors.models.#{model_name}.attributes.#{attribute}.#{type}",
|
|
|
|
:"activemodel.errors.models.#{model_name}.#{type}",
|
|
|
|
:"activemodel.errors.messages.#{type}",
|
|
|
|
:"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{type}",
|
2017-09-17 19:33:33 +00:00
|
|
|
:"activerecord.errors.models.#{model_name}.#{type}",
|
|
|
|
:"activerecord.errors.messages.#{type}",
|
|
|
|
:"errors.attributes.#{attribute}.#{type}",
|
|
|
|
:"errors.messages.#{type}",
|
|
|
|
]
|
2019-01-30 05:58:03 +00:00
|
|
|
primary_translation_key = default_translation_keys.shift
|
2017-09-17 19:33:33 +00:00
|
|
|
translate_options =
|
|
|
|
{ default: default_translation_keys }.merge(options)
|
|
|
|
I18n.translate(primary_translation_key, translate_options)
|
|
|
|
end
|
2019-07-07 02:30:56 +00:00
|
|
|
|
2019-07-26 05:31:21 +00:00
|
|
|
class FakeAttributeType
|
2019-07-07 02:30:56 +00:00
|
|
|
def initialize(model, attribute_name)
|
|
|
|
@model = model
|
|
|
|
@attribute_name = attribute_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def coder
|
2019-07-26 05:31:21 +00:00
|
|
|
nil
|
2019-07-07 02:30:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :model, :attribute_name
|
|
|
|
end
|
2014-04-20 02:48:43 +00:00
|
|
|
end
|
2013-05-10 18:10:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|