-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cms:page_file_link
tag to render page files
Examples: {{ cms:page_file_link header }} {{ cms:page_file_link attachments, filename: "cat.jpg", as: image }} Dragging page-level files onto the textarea generates these tags. See #796 for more information.
- Loading branch information
Showing
10 changed files
with
253 additions
and
81 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
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
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
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# A mixin for tags that returns the file as their content. | ||
module ComfortableMexicanSofa::Content::Tag::FileContent | ||
|
||
# @param [ActiveStorage::Blob] file | ||
# @param ["link", "image", "url"] as | ||
# @param [{String => String}] variant_attrs ImageMagick variant attributes | ||
# @param [String] label alt text for `as: "image"`, link text for `as: "link"` | ||
# @return [String] | ||
def content(file: self.file, as: self.as, variant_attrs: self.variant_attrs, label: self.label) | ||
return "" unless file | ||
|
||
if variant_attrs.present? && attachment.image? | ||
file = file.variant(variant_attrs) | ||
end | ||
|
||
url = rails_blob_path(file) | ||
|
||
case as | ||
when "link" | ||
"<a href='#{url}' target='_blank'>#{label}</a>" | ||
when "image" | ||
"<img src='#{url}' alt='#{label}'/>" | ||
else | ||
url | ||
end | ||
end | ||
|
||
# @param [ActiveStorage::Blob] | ||
# @return [String] | ||
def rails_blob_path(blob) | ||
Rails.application.routes.url_helpers.rails_blob_path(blob, only_path: true) | ||
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
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
80 changes: 80 additions & 0 deletions
80
lib/comfortable_mexican_sofa/content/tags/page_file_link.rb
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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require "comfortable_mexican_sofa/content/tags/file_content.rb" | ||
|
||
# This tag allows you to link page-level files from withing the page. | ||
# | ||
# E.g. if your layout has: | ||
# | ||
# {{ cms:file graphic }} | ||
# {{ cms:files attachments }} | ||
# | ||
# You can link to the files from an individual page like so: | ||
# | ||
# {{ cms:page_file_link graphic }} | ||
# {{ cms:page_file_link attachments, filename: "cat.jpg" }} | ||
# | ||
# `as` - url (default) | link | image - how file gets rendered out | ||
# `label` - attach label attribute to link or image tag | ||
# `resize` - imagemagick option. For example: "100x50>" | ||
# `gravity` - imagemagick option. For example: "center" | ||
# `crop` - imagemagick option. For example: "100x50+0+0" | ||
# | ||
class ComfortableMexicanSofa::Content::Tag::PageFileLink < ComfortableMexicanSofa::Content::Tag | ||
|
||
include ComfortableMexicanSofa::Content::Tag::FileContent | ||
|
||
# @return [String] A `cms:file(s)` identifier. | ||
attr_reader :identifier | ||
|
||
# @type ["url", "link", "image"] | ||
attr_reader :as | ||
|
||
# @type [{String => String}] | ||
attr_reader :variant_attrs | ||
|
||
# @return [String] Used to refer to a file in a {{ cms:files }} tag. | ||
attr_reader :filename | ||
|
||
# @param (see ComfortableMexicanSofa::Content::Tag#initialize) | ||
def initialize(context:, params: [], source: nil) | ||
super | ||
|
||
options = params.extract_options! | ||
@identifier = params[0] | ||
@as = options["as"] || "url" | ||
@variant_attrs = options.slice("resize", "gravity", "crop") | ||
@filename = options["filename"] | ||
|
||
unless @identifier.present? | ||
raise Error, "Missing identifier for page file link tag" | ||
end | ||
end | ||
|
||
# @return [Comfy::Cms::Fragment] | ||
def fragment | ||
@fragment ||= context.fragments.detect { |f| f.identifier == identifier } | ||
end | ||
|
||
# @return [ActiveStorage::Blob] | ||
def file | ||
@file ||= if fragment.nil? | ||
nil | ||
elsif filename.nil? | ||
fragment.attachments.first | ||
else | ||
fragment.attachments.detect { |a| a.filename.to_s == filename } | ||
end | ||
end | ||
|
||
# @return [String] | ||
def label | ||
return if file.nil? | ||
file.filename.to_s | ||
end | ||
|
||
end | ||
|
||
ComfortableMexicanSofa::Content::Renderer.register_tag( | ||
:page_file_link, ComfortableMexicanSofa::Content::Tag::PageFileLink | ||
) |
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
Oops, something went wrong.