Skip to content

Commit

Permalink
Improve enum docs for instance_methods and scopes [ci-skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
sampatbadhe committed Nov 26, 2022
1 parent 2b2aea4 commit 9ffbb92
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions activerecord/lib/active_record/enum.rb
Expand Up @@ -115,7 +115,9 @@ module ActiveRecord
# If you want to disable the auto-generated methods on the model, you can do
# so by setting the +:instance_methods+ option to false:
#
# enum :status, [ :active, :archived ], instance_methods: false
# class Conversation < ActiveRecord::Base
# enum :status, [ :active, :archived ], instance_methods: false
# end
module Enum
def self.extended(base) # :nodoc:
base.class_attribute(:defined_enums, instance_writer: false, default: {})
Expand Down Expand Up @@ -261,12 +263,12 @@ def define_enum_methods(name, value_method_name, value, scopes, instance_methods
define_method("#{value_method_name}!") { update!(name => value) }
end

# scope :active, -> { where(status: 0) }
# scope :not_active, -> { where.not(status: 0) }
if scopes
# scope :active, -> { where(status: 0) }
klass.send(:detect_enum_conflict!, name, value_method_name, true)
klass.scope value_method_name, -> { where(name => value) }

# scope :not_active, -> { where.not(status: 0) }
klass.send(:detect_enum_conflict!, name, "not_#{value_method_name}", true)
klass.scope "not_#{value_method_name}", -> { where.not(name => value) }
end
Expand Down

0 comments on commit 9ffbb92

Please sign in to comment.