Remove code for unsupported versions (#1270)

This commit is contained in:
Gui Vieira 2020-01-01 19:10:35 -08:00 committed by GitHub
parent d97bdd6cab
commit 0e6db542be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 259 deletions

View File

@ -269,7 +269,7 @@ module Shoulda
end
def type_cast_default
Shoulda::Matchers::RailsShim.type_cast_default_for(model, self)
model.column_defaults[name]
end
def primary?

View File

@ -3,10 +3,6 @@ module Shoulda
# @private
module RailsShim
class << self
def action_pack_gte_4_1?
Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version)
end
def action_pack_gte_5?
Gem::Requirement.new('>= 5').satisfied_by?(action_pack_version)
end
@ -85,15 +81,6 @@ module Shoulda
serialized_attributes_for(model)[attribute_name.to_s]
end
def 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 tables_and_views(connection)
if active_record_gte_5?
connection.data_sources
@ -103,11 +90,7 @@ module Shoulda
end
def verb_for_update
if action_pack_gte_4_1?
:patch
else
:put
end
:patch
end
def validation_message_key_for_association_required_option

View File

@ -13,13 +13,7 @@ module AcceptanceTests
end
def default_test_framework
if rails_version =~ '< 4'
:test_unit
elsif rails_version =~ '~> 4.0.0'
:minitest_4
else
:minitest
end
:minitest
end
end
end

View File

@ -112,10 +112,6 @@ module AcceptanceTests
end
def add_spring_to_project
if rails_version < 4
add_gem 'spring'
end
add_gem 'spring-commands-rspec'
end
end

View File

@ -11,26 +11,6 @@ module UnitTests
Tests::Version.new(Rails::VERSION::STRING)
end
def rails_3_x?
rails_version =~ '~> 3.0'
end
def rails_4_x?
rails_version =~ '~> 4.0'
end
def rails_lte_4?
rails_version <= 4
end
def rails_gte_4_1?
rails_version >= 4.1
end
def rails_gte_4_2?
rails_version >= 4.2
end
def rails_lt_5?
rails_version < 5
end

View File

@ -508,36 +508,19 @@ describe Shoulda::Matchers::ActionController::PermitMatcher, type: :controller d
end
context 'when given :update' do
if rails_gte_4_1?
it 'PATCHes to the controller' do
controller = ActionController::Base.new
context = build_context
matcher = permit(:name).for(:update).in_context(context)
it 'PATCHes to the controller' do
controller = ActionController::Base.new
context = build_context
matcher = permit(:name).for(:update).in_context(context)
matcher.matches?(controller)
matcher.matches?(controller)
expect_to_have_made_controller_request(
verb: :patch,
action: :update,
params: {},
context: context,
)
end
else
it 'PUTs to the controller' do
controller = ActionController::Base.new
context = build_context
matcher = permit(:name).for(:update).in_context(context)
matcher.matches?(controller)
expect_to_have_made_controller_request(
verb: :put,
action: :update,
params: {},
context: context,
)
end
expect_to_have_made_controller_request(
verb: :patch,
action: :update,
params: {},
context: context,
)
end
end

View File

@ -946,37 +946,23 @@ validation exception on failure, but this could not be proved.
end
end
if rails_lte_4?
context 'an active_resource model' do
context 'with the validation context' do
it 'does not raise an exception' do
expect do
expect(active_resource_model).to validate_presence_of(:attr)
end.to_not raise_exception
end
context 'against a pre-set password in a model that has_secure_password' do
it 'raises a CouldNotSetPasswordError' do
user_class = define_model :user, password_digest: :string do
has_secure_password validations: false
validates_presence_of :password
end
end
end
if rails_4_x?
context 'against a pre-set password in a model that has_secure_password' do
it 'raises a CouldNotSetPasswordError' do
user_class = define_model :user, password_digest: :string do
has_secure_password validations: false
validates_presence_of :password
end
user = user_class.new
user.password = 'something'
user = user_class.new
user.password = 'something'
assertion = lambda do
expect(user).to validate_presence_of(:password)
end
expect(&assertion).to raise_error(
Shoulda::Matchers::ActiveModel::CouldNotSetPasswordError
)
assertion = lambda do
expect(user).to validate_presence_of(:password)
end
expect(&assertion).to raise_error(
Shoulda::Matchers::ActiveModel::CouldNotSetPasswordError
)
end
end

View File

@ -87,163 +87,34 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection d
end
describe '#association_relation' do
if rails_4_x?
context 'when the reflection object has a #scope method' do
context 'when the scope is a block' do
it 'executes the block in the context of an empty scope' do
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, -> { where(mood: 'nice') }
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.where(mood: 'nice').to_sql
expect(actual_sql).to eq expected_sql
context 'when the reflection object has a #scope method' do
context 'when the scope is a block' do
it 'executes the block in the context of an empty scope' do
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, -> { where(mood: 'nice') }
end
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
context 'when the scope is nil' do
it 'returns an empty scope' do
define_model(:country)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.all.to_sql
expect(actual_sql).to eq expected_sql
end
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.where(mood: 'nice').to_sql
expect(actual_sql).to eq expected_sql
end
end
end
if rails_3_x?
context 'when the reflection object does not have a #scope method' do
context 'when the reflection options contain :conditions' do
it 'creates an ActiveRecord::Relation from the conditions' do
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, conditions: { mood: 'nice' }
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.where(mood: 'nice').to_sql
expect(actual_sql).to eq expected_sql
context 'when the scope is nil' do
it 'returns an empty scope' do
define_model(:country)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country
end
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
context 'when the reflection options contain :order' do
it 'creates an ActiveRecord::Relation from the order' do
define_model(:person, country_id: :integer, age: :integer)
country_model = define_model(:country) do
has_many :people, order: 'age'
end
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.order('age').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :include' do
it 'creates an ActiveRecord::Relation from the include' do
define_model(:city, country_id: :integer)
define_model(:country) do
has_many :cities
end
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, include: :cities
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_includes = reflection.association_relation.includes_values
expected_includes = Country.includes(:cities).includes_values
expect(actual_includes).to eq expected_includes
end
end
context 'when the reflection options contain :group' do
it 'creates an ActiveRecord::Relation from the group' do
country_model = define_model(:country, mood: :string) do
has_many :people, group: 'age'
end
define_model(:person, country_id: :integer, age: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.group('age').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :having' do
it 'creates an ActiveRecord::Relation from the having' do
country_model = define_model(:country) do
has_many :people, having: 'country_id > 1'
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.having('country_id > 1').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :limit' do
it 'creates an ActiveRecord::Relation from the limit' do
country_model = define_model(:country) do
has_many :people, limit: 10
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.limit(10).to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :offset' do
it 'creates an ActiveRecord::Relation from the offset' do
country_model = define_model(:country) do
has_many :people, offset: 5
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.offset(5).to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :select' do
it 'creates an ActiveRecord::Relation from the select' do
country_model = define_model(:country) do
has_many :people, select: 'age'
end
define_model(:person, country_id: :integer, age: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.select('age').to_sql
expect(actual_sql).to eq expected_sql
end
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.all.to_sql
expect(actual_sql).to eq expected_sql
end
end
end

View File

@ -31,10 +31,6 @@ RSpec.configure do |config|
UnitTests::ValidationMatcherScenarioHelpers.configure_example_group(config)
UnitTests::MessageHelpers.configure_example_group(config)
if UnitTests::RailsVersions.rails_lte_4?
UnitTests::ActiveResourceBuilder.configure_example_group(config)
end
config.include UnitTests::Matchers
config.infer_spec_type_from_file_location!