Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion app/components/op_primer/copy_to_clipboard_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ module OpPrimer
class CopyToClipboardComponent < ApplicationComponent
include OpPrimer::ComponentHelpers

SCHEME_OPTIONS = %i[value link].freeze

alias_method :value, :model

def initialize(value = nil, scheme: :value, **system_arguments)
super(value)

@scheme = scheme
@scheme = validate_scheme!(scheme)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You might want to consider using fetch_or_fallback (from Primer::FetchOrFallbackHelper) instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method somehow gives me left-pad vibes...

I mean, it's very simple to implement and I find it more likely that someone needs to look up what fetch_or_fallback does exactly, than that someone needs to understand what validate_scheme! is doing.

As a non-frequent primer user I might be biased towards not knowing well-established primer helpers here.

@system_arguments = system_arguments
@id = SecureRandom.hex(8)
end

private

def validate_scheme!(scheme)
scheme = scheme.to_sym
raise ArgumentError, "scheme must be one of #{SCHEME_OPTIONS}" unless SCHEME_OPTIONS.include?(scheme)

scheme
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def default(value: "Copy me!")
render(OpPrimer::CopyToClipboardComponent.new(value))
end

# @param url text
# @param scheme [Symbol] select [value, link]
def playground(url: "https://example.org", scheme: :value)
render(OpPrimer::CopyToClipboardComponent.new(url, scheme:))
end

# @param url text
def as_link(url: "http://example.org")
render(OpPrimer::CopyToClipboardComponent.new(url, scheme: :link))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
end

collection.with_component(
OpPrimer::CopyToClipboardComponent.new(provider.sp_entity_id, scheme: :input)
OpPrimer::CopyToClipboardComponent.new(provider.sp_entity_id, scheme: :value)
)

collection.with_component(Primer::Beta::Heading.new(tag: :h5, mt: 4, mb: 1)) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
end

collection.with_component(
OpPrimer::CopyToClipboardComponent.new(provider.slug, scheme: :input)
OpPrimer::CopyToClipboardComponent.new(provider.slug, scheme: :value)
)

collection.with_component(Primer::Beta::Heading.new(tag: :h5, mt: 4, mb: 1)) do
OpenIDConnect::Provider.human_attribute_name(:redirect_url)
end

collection.with_component(
OpPrimer::CopyToClipboardComponent.new(provider.callback_url, scheme: :input)
OpPrimer::CopyToClipboardComponent.new(provider.callback_url, scheme: :value)
)

collection.with_component(Primer::Beta::Heading.new(tag: :h5, mt: 4, mb: 1)) do
t(".backchannel_logout_url")
end

collection.with_component(
OpPrimer::CopyToClipboardComponent.new(provider.backchannel_logout_url, scheme: :input)
OpPrimer::CopyToClipboardComponent.new(provider.backchannel_logout_url, scheme: :value)
)
end
end
Expand Down
Loading