From 59a4310f7ca60a3ac7248a144e02e51b0b35ea50 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sun, 17 Sep 2017 14:33:33 -0500 Subject: [PATCH] Refactor RailsShim * Satisfy Rubocop * Alphabetize methods * Use `class << self` instead of `self.` so that we can privatize one of the methods --- lib/shoulda/matchers/rails_shim.rb | 126 ++++++++++++++++++----------- 1 file changed, 80 insertions(+), 46 deletions(-) diff --git a/lib/shoulda/matchers/rails_shim.rb b/lib/shoulda/matchers/rails_shim.rb index d540c9834..97979d557 100644 --- a/lib/shoulda/matchers/rails_shim.rb +++ b/lib/shoulda/matchers/rails_shim.rb @@ -2,31 +2,55 @@ module Shoulda module Matchers # @private class RailsShim - def self.verb_for_update - if action_pack_gte_4_1? - :patch - else - :put - end + class << self + def active_record_major_version + ::ActiveRecord::VERSION::MAJOR end - def self.type_cast_default_for(model, column) - if model.respond_to?(:column_defaults) - # Rails 4.2 - model.column_defaults[column.name] + def action_pack_gte_4_1? + Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version) + end + + def action_pack_version + Gem::Version.new(::ActionPack::VERSION::STRING) + end + + 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 - column.default + simply_generate_validation_message( + attribute, + type, + model_name, + options, + ) end + rescue RangeError + simply_generate_validation_message( + attribute, + type, + model_name, + options, + ) end - def self.serialized_attributes_for(model) + def serialized_attributes_for(model) if defined?(::ActiveRecord::Type::Serialized) # Rails 5+ - model.columns.select do |column| + serialized_columns = model.columns.select do |column| model.type_for_attribute(column.name).is_a?( ::ActiveRecord::Type::Serialized, ) - end.inject({}) do |hash, column| + end + + serialized_columns.inject({}) do |hash, column| hash[column.name.to_s] = model.type_for_attribute(column.name).coder hash end @@ -35,29 +59,16 @@ def self.serialized_attributes_for(model) end end - 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) + def type_cast_default_for(model, column) + if model.respond_to?(:column_defaults) + # Rails 4.2 + model.column_defaults[column.name] else - simply_generate_validation_message(attribute, type, model_name, options) + column.default 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 - def self.tables_and_views(connection) + def tables_and_views(connection) if active_record_major_version >= 5 connection.data_sources else @@ -65,19 +76,7 @@ def self.tables_and_views(connection) end end - def self.active_record_major_version - ::ActiveRecord::VERSION::MAJOR - end - - 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 - - def self.make_controller_request(context, verb, action, request_params) + def make_controller_request(context, verb, action, request_params) params = if active_record_major_version >= 5 { params: request_params } @@ -87,6 +86,41 @@ def self.make_controller_request(context, verb, action, request_params) context.__send__(verb, action, params) end + + def verb_for_update + if action_pack_gte_4_1? + :patch + else + :put + end + end + + private + + def 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 end end end