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

Components rendered in a subclass of AV::Base ignore the subclass #1523

Open
p8 opened this issue Sep 16, 2022 · 2 comments
Open

Components rendered in a subclass of AV::Base ignore the subclass #1523

p8 opened this issue Sep 16, 2022 · 2 comments

Comments

@p8
Copy link
Contributor

p8 commented Sep 16, 2022

When rendering a component in a subclass of ActionView::Base, methods called within the component are called on ActionView::Base instead of the subclass.
This prevents overriding methods on ActionView::Base in a subclass.

Steps to reproduce

# frozen_string_literal: true
#
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "~> 7.0.0"
  gem "view_component"
end

require "action_view/railtie"
require "action_view/testing/resolvers"

class CustomView < ActionView::Base
  def hi
    "Hi!"
  end
end

class GreetingComponent < ViewComponent::Base
  def call
    # Calling `hi` directly doesn't work because ViewComponent::Base doesn't
    # inherit from ActionView::Base:
    #
    #   hi # raises: undefined method `hi' for #<GreetingComponent....
    #
    helpers.hi
  end
end

view_paths = [ActionView::FixtureResolver.new(
  "main.erb" => %Q{
    <%= render GreetingComponent.new %>
  },
)]

custom_view = CustomView.with_empty_template_cache.with_view_paths view_paths
puts custom_view.render template: 'main'

Expected behavior

Methods called in components rendered in a subclass of ActionView::Base delegate methods to the subclass.

Actual behavior

Methods called in components rendered in a subclass of ActionView::Base delegate methods to the ActionView::Base.
Methods defined on the subclass need to be called on the helper.

@joelhawksley
Copy link
Member

Thanks for filing! Might you be up for writing a test case for this? How do you think we should fix it?

@p8
Copy link
Contributor Author

p8 commented Nov 14, 2022

@joelhawksley Sorry for the late reply. I have no idea how to fix it. But I'll give it a try... 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants