Skip to content

Commit

Permalink
Remove unnecessary conditions for rails 5.0 (#1426)
Browse files Browse the repository at this point in the history
* Remove rails_lt_5? method

* Remove rails_5_x? method

* Remove conditionals for rails version equal to or greater than 5

* Remove action_pack_gte_5? method of RailsShim module

* Remove action_pack_lt_5? methods

* Remove active_record_gte_5? method

* Remove make_controller_request method

* Remove tables_and_views method

* Remove validation_message_key_for_association_required_option method
  • Loading branch information
vsppedro committed Mar 23, 2021
1 parent 73deafd commit fc08a73
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 88 deletions.
9 changes: 3 additions & 6 deletions lib/shoulda/matchers/action_controller/permit_matcher.rb
Expand Up @@ -251,12 +251,9 @@ def matches?(controller)
parameters_double_registry.register

Doublespeak.with_doubles_activated do
Shoulda::Matchers::RailsShim.make_controller_request(
context,
verb,
action,
request_params,
)
params = { params: request_params }

context.__send__(verb, action, **params)
end

unpermitted_parameter_names.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -1004,7 +1004,7 @@ def initialize(macro, name)
@submatchers = []
@missing = ''

if macro == :belongs_to && RailsShim.active_record_gte_5?
if macro == :belongs_to
required(belongs_to_required_by_default?)
end
end
Expand Down
Expand Up @@ -42,7 +42,7 @@ def join_table_option_correct?
end

def join_table_exists?
if RailsShim.tables_and_views(connection).
if connection.data_sources.
include?(join_table_name.to_s)
true
else
Expand Down
Expand Up @@ -65,7 +65,7 @@ def submatcher_passes?(subject)
end

def validation_message_key
RailsShim.validation_message_key_for_association_required_option
:required
end
end
end
Expand Down
39 changes: 0 additions & 39 deletions lib/shoulda/matchers/rails_shim.rb
Expand Up @@ -3,24 +3,12 @@ module Matchers
# @private
module RailsShim # rubocop:disable Metrics/ModuleLength
class << self
def action_pack_gte_5?
Gem::Requirement.new('>= 5').satisfied_by?(action_pack_version)
end

def action_pack_lt_5?
Gem::Requirement.new('< 5').satisfied_by?(action_pack_version)
end

def action_pack_version
Gem::Version.new(::ActionPack::VERSION::STRING)
rescue NameError
Gem::Version.new('0')
end

def active_record_gte_5?
Gem::Requirement.new('>= 5').satisfied_by?(active_record_version)
end

def active_record_gte_6?
Gem::Requirement.new('>= 6').satisfied_by?(active_record_version)
end
Expand Down Expand Up @@ -57,17 +45,6 @@ def generate_validation_message(
)
end

def make_controller_request(context, verb, action, request_params)
params =
if action_pack_gte_5?
{ params: request_params }
else
request_params
end

context.__send__(verb, action, **params)
end

def serialized_attributes_for(model)
attribute_types_for(model).
inject({}) do |hash, (attribute_name, attribute_type)|
Expand All @@ -85,26 +62,10 @@ def attribute_serialization_coder_for(model, attribute_name)
serialized_attributes_for(model)[attribute_name.to_s]
end

def tables_and_views(connection)
if active_record_gte_5?
connection.data_sources
else
connection.tables
end
end

def verb_for_update
:patch
end

def validation_message_key_for_association_required_option
if active_record_gte_5?
:required
else
:blank
end
end

def parent_of(mod)
if mod.respond_to?(:module_parent)
mod.module_parent
Expand Down
6 changes: 1 addition & 5 deletions spec/support/acceptance/helpers/rails_migration_helpers.rb
Expand Up @@ -5,11 +5,7 @@ module RailsMigrationHelpers
include RailsVersionHelpers

def migration_class_name
if rails_version >= 5
"ActiveRecord::Migration[#{rails_version_for_migration}]"
else
'ActiveRecord::Migration'
end
"ActiveRecord::Migration[#{rails_version_for_migration}]"
end

private
Expand Down
4 changes: 0 additions & 4 deletions spec/support/unit/helpers/action_pack_versions.rb
Expand Up @@ -11,10 +11,6 @@ def action_pack_gte_5?
action_pack_version >= 5
end

def action_pack_lt_5?
action_pack_version < 5
end

def action_pack_version
Tests::Version.new(ActionPack::VERSION::STRING)
end
Expand Down
8 changes: 0 additions & 8 deletions spec/support/unit/helpers/rails_versions.rb
Expand Up @@ -11,14 +11,6 @@ def rails_version
Tests::Version.new(Rails::VERSION::STRING)
end

def rails_lt_5?
rails_version < 5
end

def rails_5_x?
rails_version =~ '~> 5.0'
end

def rails_gte_5_2?
rails_version >= 5.2
end
Expand Down
4 changes: 1 addition & 3 deletions spec/support/unit/rails_application.rb
Expand Up @@ -82,9 +82,7 @@ def generate
write_activerecord_model_with_default_connection
write_activerecord_model_with_different_connection

if rails_version >= 5
add_initializer_for_time_zone_aware_types
end
add_initializer_for_time_zone_aware_types
end

def rails_new
Expand Down
Expand Up @@ -1051,30 +1051,28 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
}.to fail_with_message(message)
end

if rails_5_x?
context 'index_errors' do
it 'accepts an association with a matching :index_errors option' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, index_errors: true
end
expect(Parent.new).to have_many(:children).index_errors(true)
context 'index_errors' do
it 'accepts an association with a matching :index_errors option' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, index_errors: true
end
expect(Parent.new).to have_many(:children).index_errors(true)
end

it 'rejects an association with a non-matching :index_errors option and returns the correct message' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, autosave: false
end
it 'rejects an association with a non-matching :index_errors option and returns the correct message' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, autosave: false
end

message =
'Expected Parent to have a has_many association called children '\
'(children should have index_errors set to true)'
message =
'Expected Parent to have a has_many association called children '\
'(children should have index_errors set to true)'

expect {
expect(Parent.new).to have_many(:children).index_errors(true)
}.to fail_with_message(message)
end
expect {
expect(Parent.new).to have_many(:children).index_errors(true)
}.to fail_with_message(message)
end
end

Expand Down

0 comments on commit fc08a73

Please sign in to comment.