Skip to content

Delegate missing constants to object class via const_missing#964

Merged
Alexander-Senko merged 4 commits into
masterfrom
copilot/delegate-class-variable-to-decorator
Feb 28, 2026
Merged

Delegate missing constants to object class via const_missing#964
Alexander-Senko merged 4 commits into
masterfrom
copilot/delegate-class-variable-to-decorator

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

  • Add const_missing to Draper::AutomaticDelegation::ClassMethods to delegate missing constants to the object class when delegate_all is used
  • Add tests for the new const_missing behavior
  • Update README to document constant delegation via delegate_all
  • Update CHANGELOG with new feature entry
Original prompt

This section details on the original issue you should resolve

<issue_title>Delegate class variable to the decorated object class?</issue_title>
<issue_description>Hello wonderful draper team, here is my use-case, maybe some of you already meet similar issue.

I'd like to allow a decorator class to delegate the class variable access to the parent class, it does not seem to be handled by the delegate_all

for example:

class Car
  WHEELS_COUNT = 4
end

class CarDecorator < ApplicationDecorator
end

car = Car.new
car.class::WHEELS_COUNT => 4

car_decorated = car.decorate
car_decorated.class::WHEEL_NUMBER => uninitialized constant CarDecorator::WHEELS_COUNT (NameError)

Do you have any idea / suggestion about this issue?</issue_description>

Comments on the Issue (you are @copilot in this section)

@Alexander-Senko Delegation in Ruby is meant for methods, not for constants. Every constant belongs to its module/namespace. See [the same question](https://stackoverflow.com/questions/41594617/can-i-use-forwardable-to-delegate-a-constant-in-ruby) for Ruby standard library.

However, that could be implemented using .const_missing. Maybe, I'll do it for my Magic Decorator. But I'm not sure it could be implemented in Draper soon (see the reasons).</comment_new>
<comment_new>@Alexander-Senko

Do you have any idea / suggestion about this issue?

Try something like this (untested) in your ApplicationDecorator:

  def self.const_missing(...) = object_class.const_get(...)

If that works, it could be implemented as a PR. But I don't believe it to be released soon in a new Draper version.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: Alexander-Senko <1022687+Alexander-Senko@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix class variable delegation for decorator class Delegate missing constants to object class via const_missing Feb 27, 2026
Copy link
Copy Markdown
Collaborator

@Alexander-Senko Alexander-Senko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update both README and CHANGELOG.

@Alexander-Senko Alexander-Senko marked this pull request as ready for review February 27, 2026 13:15
Co-authored-by: Alexander-Senko <1022687+Alexander-Senko@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 27, 2026

Update both README and CHANGELOG.

Done in a2423d2. Added an "Unreleased" section to CHANGELOG and a constant delegation example to the README's "Delegating Methods" section.

Comment thread lib/draper/automatic_delegation.rb Outdated
Co-authored-by: y-yagi <yuuji.yaginuma@gmail.com>
@Alexander-Senko Alexander-Senko merged commit 43717f7 into master Feb 28, 2026
18 checks passed
@Alexander-Senko Alexander-Senko deleted the copilot/delegate-class-variable-to-decorator branch February 28, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delegate class variable to the decorated object class?

3 participants