Repository: rails/rails
File: activesupport/lib/active_support/core_ext/class/attribute.rb (Ruby)

Code before:
  # To set a default value for the attribute, pass <tt>default:</tt>, like so:
  #
  #   class_attribute :settings, default: {}
  def class_attribute(*attrs, instance_accessor: true,
    instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil
  )
    class_methods, methods = [], []
    attrs.each do |name|
      unless name.is_a?(Symbol) || name.is_a?(String)
        raise TypeError, "#{name.inspect} is not a symbol nor a string"
      end

      name = name.to_sym
      namespaced_name = :"__class_attr_#{name}"
      ::ActiveSupport::ClassAttribute.redefine(self, name, namespaced_name, default)

      delegators = [
        "def #{name}; #{namespaced_name}; end",
        "def #{name}=(value); self.#{namespaced_name} = value; end",
      ]

      class_methods.concat(delegators)
      if singleton_class?
        methods.concat(delegators)
      else
        methods << <<~RUBY if instance_reader
          silence_redefinition_of_method def #{name}
            if defined?(@#{name})
              @#{name}
            else
              self.class.#{name}
            end
          end
        RUBY
      end

      methods << <<~RUBY if instance_writer
        silence_redefinition_of_method(:#{name}=)
        attr_writer :#{name}
      RUBY

      if instance_predicate
        class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
        if instance_reader
          methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
        end
      end
    end

    location = caller_locations(1, 1).first
    class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno)

Review comment (suggestion):
The writer that was previously added here shouldn't be needed. It is correctly set below when `instance_writer` is set. Also, previously, the reader was added unconditionally when it should be checking `instance_reader`.

Note however that this reader definition preserves some confusing behaviour, see: https://github.com/rails/rails/issues/55776#issuecomment-3341321483.

Code after:
  # To set a default value for the attribute, pass <tt>default:</tt>, like so:
  #
  #   class_attribute :settings, default: {}
  def class_attribute(*attrs, instance_accessor: true,
    instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil
  )
    class_methods, methods = [], []
    attrs.each do |name|
      unless name.is_a?(Symbol) || name.is_a?(String)
        raise TypeError, "#{name.inspect} is not a symbol nor a string"
      end

      name = name.to_sym
      namespaced_name = :"__class_attr_#{name}"
      ::ActiveSupport::ClassAttribute.redefine(self, name, namespaced_name, default)

      class_methods << "def #{name}; #{namespaced_name}; end"
      class_methods << "def #{name}=(value); self.#{namespaced_name} = value; end"

      if singleton_class?
        methods << <<~RUBY if instance_reader
          silence_redefinition_of_method(:#{name})
          def #{name}
            self.singleton_class.#{name}
          end
        RUBY
      else
        methods << <<~RUBY if instance_reader
          silence_redefinition_of_method def #{name}
            if defined?(@#{name})
              @#{name}
            else
              self.class.#{name}
            end
          end
        RUBY
      end

      methods << <<~RUBY if instance_writer
        silence_redefinition_of_method(:#{name}=)
        attr_writer :#{name}
      RUBY

      if instance_predicate
        class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
        if instance_reader
          methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
        end
      end
    end