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
|
|
|
|
class RailsShim
|
2014-04-20 02:48:43 +00:00
|
|
|
def self.verb_for_update
|
|
|
|
if action_pack_gte_4_1?
|
|
|
|
:patch
|
|
|
|
else
|
|
|
|
:put
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-24 21:46:49 +00:00
|
|
|
def self.type_cast_default_for(model, column)
|
|
|
|
if model.respond_to?(:column_defaults)
|
|
|
|
# Rails 4.2
|
|
|
|
model.column_defaults[column.name]
|
|
|
|
else
|
|
|
|
column.default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.serialized_attributes_for(model)
|
|
|
|
if defined?(::ActiveRecord::Type::Serialized)
|
|
|
|
# Rails 5+
|
|
|
|
model.columns.select do |column|
|
|
|
|
column.cast_type.is_a?(::ActiveRecord::Type::Serialized)
|
|
|
|
end.inject({}) do |hash, column|
|
|
|
|
hash[column.name.to_s] = column.cast_type.coder
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
else
|
|
|
|
model.serialized_attributes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-21 22:46:26 +00:00
|
|
|
def self.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)
|
|
|
|
end
|
|
|
|
rescue RangeError
|
|
|
|
simply_generate_validation_message(attribute, type, model_name, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.simply_generate_validation_message(attribute, type, model_name, options)
|
|
|
|
default_translation_keys = [
|
|
|
|
:"activerecord.errors.models.#{model_name}.#{type}",
|
|
|
|
:"activerecord.errors.messages.#{type}",
|
|
|
|
:"errors.attributes.#{attribute}.#{type}",
|
|
|
|
:"errors.messages.#{type}"
|
|
|
|
]
|
|
|
|
primary_translation_key = :"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{type}"
|
|
|
|
translate_options = { default: default_translation_keys }.merge(options)
|
|
|
|
I18n.translate(primary_translation_key, translate_options)
|
|
|
|
end
|
|
|
|
|
2013-12-16 17:33:19 +00:00
|
|
|
def self.active_record_major_version
|
|
|
|
::ActiveRecord::VERSION::MAJOR
|
|
|
|
end
|
|
|
|
|
2014-04-20 02:48:43 +00:00
|
|
|
def self.action_pack_gte_4_1?
|
|
|
|
Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.action_pack_version
|
|
|
|
Gem::Version.new(::ActionPack::VERSION::STRING)
|
|
|
|
end
|
2013-05-10 18:10:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|