-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Alchemy | ||
module Ingredients | ||
class TextView < BaseView | ||
attr_reader :disable_link | ||
|
||
delegate :dom_id, :link, :link_title, :link_target, | ||
to: :ingredient | ||
|
||
# @param ingredient [Alchemy::Ingredient] | ||
# @param disable_link [Boolean] (false) Whether to disable the link even if the picture has a link. | ||
# @param html_options [Hash] Options that will be passed to the a tag. | ||
def initialize(ingredient, disable_link: nil, html_options: {}) | ||
super(ingredient, html_options: html_options) | ||
@disable_link = disable_link.nil? ? ingredient.settings.fetch(:disable_link, false) : disable_link | ||
end | ||
|
||
def call | ||
if disable_link? | ||
dom_id.present? ? anchor : value | ||
else | ||
link_to(value, url_for(link), { | ||
id: dom_id.presence, | ||
title: link_title, | ||
target: ((link_target == "blank") ? "_blank" : nil), | ||
data: {link_target: link_target} | ||
}.merge(html_options)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def anchor | ||
content_tag(:a, value, {id: dom_id}.merge(html_options)) | ||
end | ||
|
||
def disable_link? | ||
link.blank? || disable_link | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,5 @@ | ||
<%- options = local_assigns.fetch(:options, {}) -%> | ||
<%- html_options = local_assigns.fetch(:html_options, {}) -%> | ||
<%- if text_view.link.blank? || text_view.settings_value(:disable_link, options) -%> | ||
<%- if text_view.dom_id.present? -%> | ||
<%= content_tag :a, text_view.value, id: text_view.dom_id %> | ||
<% else %> | ||
<%= text_view.value -%> | ||
<%- end -%> | ||
<%- else -%> | ||
<%= link_to( | ||
text_view.value, | ||
url_for(text_view.link), | ||
{ | ||
id: text_view.dom_id.presence, | ||
title: text_view.link_title, | ||
target: (text_view.link_target == "blank" ? "_blank" : nil), | ||
'data-link-target' => text_view.link_target | ||
}.merge(html_options) | ||
<%= render Alchemy::Ingredients::TextView.new( | ||
text_view, | ||
disable_link: local_assigns.dig(:options, :disable_link), | ||
html_options: local_assigns.fetch(:html_options, {}) | ||
) -%> | ||
<%- end -%> |