未验证 提交 e5172265 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #34605 from alfie-max/disable_enum_scopes

option to disable all scopes that `ActiveRecord.enum` generates
* Allow disabling scopes generated by `ActiveRecord.enum`.
*Alfred Dominic*
* Ensure that `delete_all` on collection proxy returns affected count.
*Ryuta Kamizono*
......
......@@ -149,6 +149,7 @@ def enum(definitions)
klass = self
enum_prefix = definitions.delete(:_prefix)
enum_suffix = definitions.delete(:_suffix)
enum_scopes = definitions.delete(:_scopes)
definitions.each do |name, values|
assert_valid_enum_definition_values(values)
# statuses = { }
......@@ -195,8 +196,10 @@ def enum(definitions)
define_method("#{value_method_name}!") { update!(attr => value) }
# scope :active, -> { where(status: 0) }
klass.send(:detect_enum_conflict!, name, value_method_name, true)
klass.scope value_method_name, -> { where(attr => value) }
if enum_scopes != false
klass.send(:detect_enum_conflict!, name, value_method_name, true)
klass.scope value_method_name, -> { where(attr => value) }
end
end
end
enum_values.freeze
......
......@@ -551,4 +551,13 @@ def self.name; "Book"; end
test "data type of Enum type" do
assert_equal :integer, Book.type_for_attribute("status").type
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册