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

feat: Access params ActionMailer::Preview style #1970

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/view_component/preview.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "active_support/core_ext/object/blank"
require "active_support/descendants_tracker"

module ViewComponent # :nodoc:
Expand All @@ -13,6 +14,12 @@ class Preview
include ActionView::Helpers::AssetTagHelper
extend ActiveSupport::DescendantsTracker

attr_reader :params

def initialize(params = {})
@params = params
end

def render(component, **args, &block)
{
args: args,
Expand Down Expand Up @@ -43,8 +50,16 @@ def all
# Returns the arguments for rendering of the component in its layout
def render_args(example, params: {})
example_params_names = instance_method(example).parameters.map(&:last)
provided_params = params.slice(*example_params_names).to_h.symbolize_keys
result = provided_params.empty? ? new.public_send(example) : new.public_send(example, **provided_params)

result =
if example_params_names.blank?
new(params).public_send(example)
else
Deprecation.deprecation_warning("Preview variant arguments are deprecated, user params instead")
provided_params = params.slice(*example_params_names).to_h.symbolize_keys
new(params).public_send(example, **provided_params)
end

result ||= {}
result[:template] = preview_example_template_path(example) if result[:template].nil?
@layout = nil unless defined?(@layout)
Expand Down
Loading