-
-
Notifications
You must be signed in to change notification settings - Fork 913
Open
Description
Description
Model with dynamic enum breaks the specs when same is already taken and tried to be renamed.
Reproduction Steps
class Place < ApplicationRecord
enum :status, %i[state1 state2]
end
module Custom
class Place < ::Place
attribute :user_status, :integer
enum :user_status, %i[pending
validated
excluded], validate: { allow_nil: true }
scope :with_custom, lambda { |user|
joins(:something)
.select('places.*',
'something.status as user_status')
}
end
end By default user_status does not exist. It's SQL created when calling the scope with_custom(user)
To make it work on version 7.1.3.4 with the following monkey patch on dedicated class
NEW_FIELDS = %I[status].freeze
class << self
# hash to allow spec `define_enum_for` to pass
# source: https://apidock.com/rails/v6.1.3.1/ActiveRecord/ModelSchema/ClassMethods/load_schema%21
def columns_hash
super
new_columns = lease_connection.schema_cache
.columns_hash('somethings')
.select { |k, _v| NEW_FIELDS.include?(k.to_sym) }
new_columns['user_status'] = new_columns.delete('status') # rename column
@columns_hash = @columns_hash.dup # override frozen hash
@columns_hash.merge!(new_columns)
@columns_hash
end
endbut it's no longer working for Rails 7.2.2.1 and obviously not for Rails 8.0
Expected behavior
it { is_expected.to define_enum_for(:user_status).with_values(%i[pending validated excluded])}
Actual behavior
NoMethodError:
undefined method `type' for nil
# /usr/local/bundle/gems/shoulda-matchers-6.4.0/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb:533:in `column_type_matches?'
# /usr/local/bundle/gems/shoulda-matchers-6.4.0/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb:358:in `matches?'
System configuration
shoulda_matchers version: 6.4.0
rails version: 7.2-stable
ruby version: 3.3.4
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels