Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to disable all scopes that `ActiveRecord.enum` generates #34605

Merged
merged 1 commit into from Dec 4, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,3 +1,7 @@
* 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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.