puzzle / wagons

Wagons are extensions to your application train running on Rails
MIT License
37 stars 3 forks source link

`render_core_partial` #47

Open mtnstar opened 2 months ago

mtnstar commented 2 months ago

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 commented 1 month ago

:+1: 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.