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

render_core_partial #47

Open
mtnstar opened this issue Oct 3, 2024 · 1 comment
Open

render_core_partial #47

mtnstar opened this issue Oct 3, 2024 · 1 comment

Comments

@mtnstar
Copy link

mtnstar commented Oct 3, 2024

would it make sense adding this view helper to this gem?

in hitobito we do have now a view helper for explicit render of a core partial:

core/app/helpers/wagon_helper.rb

# frozen_string_literal: true

#  Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
#  hitobito and licensed under the Affero General Public License version 3
#  or later. See the COPYING file at the top-level directory or at
#  https://github.com/hitobito/hitobito.

module WagonHelper
  def render_core_partial(partial_name, locals = {})
    core_view_path = Rails.root.join("app", "views")

    with_view_path(core_view_path) do
      render(partial_name, locals)
    end
  end

  private

  def with_view_path(path)
    original_view_paths = view_paths.dup

    view_paths = ActionView::PathSet.new([path])
    lookup_context.instance_variable_set(:@view_paths, view_paths)
    begin
      yield
    ensure
      lookup_context.instance_variable_set(:@view_paths, original_view_paths)
    end
  end
end

use case

let's say you want to customize a certain partial in a wagon, but then inside this partial want to fallback to the core's definition.

$wagon/app/views/roles/_fields.html.haml

- if my_wagon_check
  = render 'something/wagon/specific'
- else
  = render_core_partial 'roles/fields'
@codez
Copy link
Contributor

codez commented Oct 15, 2024

👍 I like the idea. The term core is not established in wagons, I suggest to name the method render_app_partial. If absolute paths could be used with render, I would prefer them instead of messing with instance variables of the lookup context.

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

No branches or pull requests

2 participants