mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add a warning for enum elements with 'not_' prefix.
When a enum element contains the prefix 'not_'. I warns to users to be aware of this new feature. Example code: class Foo < ActiveRecord::Base enum status: [:sent, :not_sent] end
This commit is contained in:
parent
4b621df384
commit
77daacf94d
3 changed files with 39 additions and 0 deletions
|
@ -1,3 +1,11 @@
|
|||
* Add a warning for enum elements with 'not_' prefix.
|
||||
|
||||
class Foo
|
||||
enum status: [:sent, :not_sent]
|
||||
end
|
||||
|
||||
*Edu Depetris*
|
||||
|
||||
* Loading the schema for a model that has no `table_name` raises a `TableNotSpecified` error.
|
||||
|
||||
*Guilherme Mansur*, *Eugene Kenny*
|
||||
|
|
|
@ -200,6 +200,8 @@ module ActiveRecord
|
|||
# scope :active, -> { where(status: 0) }
|
||||
# scope :not_active, -> { where.not(status: 0) }
|
||||
if enum_scopes != false
|
||||
klass.send(:detect_negative_condition!, value_method_name)
|
||||
|
||||
klass.send(:detect_enum_conflict!, name, value_method_name, true)
|
||||
klass.scope value_method_name, -> { where(attr => value) }
|
||||
|
||||
|
@ -261,5 +263,12 @@ module ActiveRecord
|
|||
source: source
|
||||
}
|
||||
end
|
||||
|
||||
def detect_negative_condition!(method_name)
|
||||
if method_name.start_with?("not_") && logger
|
||||
logger.warn "An enum element in #{self.name} uses the prefix 'not_'." \
|
||||
" This will cause a conflict with auto generated negative scopes."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
require "cases/helper"
|
||||
require "models/author"
|
||||
require "models/book"
|
||||
require "active_support/log_subscriber/test_helper"
|
||||
|
||||
class EnumTest < ActiveRecord::TestCase
|
||||
fixtures :books, :authors, :author_addresses
|
||||
|
@ -565,4 +566,25 @@ class EnumTest < ActiveRecord::TestCase
|
|||
|
||||
assert_raises(NoMethodError) { klass.proposed }
|
||||
end
|
||||
|
||||
test "enums with a negative condition log a warning" do
|
||||
old_logger = ActiveRecord::Base.logger
|
||||
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
||||
|
||||
ActiveRecord::Base.logger = logger
|
||||
|
||||
expected_message = "An enum element in Book uses the prefix 'not_'."\
|
||||
" This will cause a conflict with auto generated negative scopes."
|
||||
|
||||
Class.new(ActiveRecord::Base) do
|
||||
def self.name
|
||||
"Book"
|
||||
end
|
||||
enum status: [:sent, :not_sent]
|
||||
end
|
||||
|
||||
assert_match(expected_message, logger.logged(:warn).first)
|
||||
ensure
|
||||
ActiveRecord::Base.logger = old_logger
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue