Created
December 22, 2023 00:41
-
-
Save bradgessler/1d6d9b24800070b6fe28d028f75f2052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Superview | |
module Helpers | |
module Links | |
# Give us some sane link helpers to work with in Phlex. They kind | |
# of mimic Rails helpers, but are "Phlexable". | |
def link_to(target = nil, method: nil, **attributes, &) | |
url = case target | |
when URI, URI::Builder::DSL | |
target.to_s | |
when NilClass | |
url_for(attributes) | |
else | |
url_for(target) | |
end | |
a(href: url, data_turbo_method: method, **attributes, &) | |
end | |
def show(model, attribute = nil, *args, **kwargs, &content) | |
content ||= Proc.new { model.send(attribute) } if attribute | |
link_to(model, *args, **kwargs, &content) | |
end | |
def edit(model, *args, **kwargs, &content) | |
content ||= Proc.new { "Edit #{model.class.model_name}" } | |
link_to([:edit, model], *args, **kwargs, &content) | |
end | |
def delete(model, *args, **kwargs, &content) | |
content ||= Proc.new { "Delete #{model.class.model_name}" } | |
link_to(model, *args, method: :delete, **kwargs, &content) | |
end | |
def create(scope = nil, *args, **kwargs, &content) | |
target = if scope.respond_to? :proxy_association | |
owner = scope.proxy_association.owner | |
model = scope.proxy_association.reflection.klass.model_name | |
element = scope.proxy_association.reflection.klass.model_name.element.to_sym | |
[:new, owner, element] | |
elsif scope.respond_to? :model | |
model = scope.model | |
[:new, model.model_name.singular_route_key.to_sym] | |
elsif scope.respond_to? :model_name | |
[:new, scope.model_name.singular_route_key.to_sym] | |
end | |
content ||= Proc.new { "Create #{model}" } | |
link_to(target, *args, **kwargs, &content) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment