1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

option to disable scopes that ActiveRecord.enum generates by default

This commit is contained in:
Alfred Dominic 2018-12-03 15:22:59 +05:30
parent 609c58bfa6
commit c4d157aafe
3 changed files with 18 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* Allow disabling scopes generated by `ActiveRecord.enum`.
*Alfred Dominic*
* Ensure that `delete_all` on collection proxy returns affected count. * Ensure that `delete_all` on collection proxy returns affected count.
*Ryuta Kamizono* *Ryuta Kamizono*

View file

@ -149,6 +149,7 @@ module ActiveRecord
klass = self klass = self
enum_prefix = definitions.delete(:_prefix) enum_prefix = definitions.delete(:_prefix)
enum_suffix = definitions.delete(:_suffix) enum_suffix = definitions.delete(:_suffix)
enum_scopes = definitions.delete(:_scopes)
definitions.each do |name, values| definitions.each do |name, values|
assert_valid_enum_definition_values(values) assert_valid_enum_definition_values(values)
# statuses = { } # statuses = { }
@ -195,8 +196,10 @@ module ActiveRecord
define_method("#{value_method_name}!") { update!(attr => value) } define_method("#{value_method_name}!") { update!(attr => value) }
# scope :active, -> { where(status: 0) } # scope :active, -> { where(status: 0) }
klass.send(:detect_enum_conflict!, name, value_method_name, true) if enum_scopes != false
klass.scope value_method_name, -> { where(attr => value) } klass.send(:detect_enum_conflict!, name, value_method_name, true)
klass.scope value_method_name, -> { where(attr => value) }
end
end end
end end
enum_values.freeze enum_values.freeze

View file

@ -551,4 +551,13 @@ class EnumTest < ActiveRecord::TestCase
test "data type of Enum type" do test "data type of Enum type" do
assert_equal :integer, Book.type_for_attribute("status").type assert_equal :integer, Book.type_for_attribute("status").type
end end
test "scopes can be disabled" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"
enum status: [:proposed, :written], _scopes: false
end
assert_raises(NoMethodError) { klass.proposed }
end
end end