mirror of
https://github.com/discourse/discourse.git
synced 2024-12-21 09:33:45 +08:00
b2a5f5802a
We have a custom implementation of #symbolize_keys in our Onebox helpers. This is likely a legacy from when Onebox was a standalone gem. This change replaces all usages with either #deep_symbolize_keys from ActiveSupport, or appropriate option to the JSON parser gem used.
56 lines
1.4 KiB
Ruby
56 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "template_support"
|
|
|
|
module Onebox
|
|
class Layout < Mustache
|
|
include TemplateSupport
|
|
|
|
VERSION = "1.0.0"
|
|
|
|
attr_reader :record
|
|
attr_reader :view
|
|
|
|
def initialize(name, record)
|
|
@record = record.deep_symbolize_keys
|
|
|
|
# Fix any relative paths
|
|
if @record[:image] && @record[:image] =~ %r{\A/[^/]}
|
|
@record[:image] = "#{uri.scheme}://#{uri.host}/#{@record[:image]}"
|
|
end
|
|
|
|
@md5 = Digest::MD5.new
|
|
@view = View.new(name, @record)
|
|
@template_name = "_layout"
|
|
@template_path = load_paths.last
|
|
end
|
|
|
|
def to_html
|
|
render(details)
|
|
end
|
|
|
|
private
|
|
|
|
def uri
|
|
@uri ||= URI(::Onebox::Helpers.normalize_url_for_output(record[:link]))
|
|
end
|
|
|
|
def details
|
|
{
|
|
link: record[:link],
|
|
title: record[:title],
|
|
favicon: record[:favicon],
|
|
domain: record[:domain] || uri.host.to_s.sub(/\Awww\./, ""),
|
|
article_published_time: record[:article_published_time],
|
|
article_published_time_title: record[:article_published_time_title],
|
|
metadata_1_label: record[:metadata_1_label],
|
|
metadata_1_value: record[:metadata_1_value],
|
|
metadata_2_label: record[:metadata_2_label],
|
|
metadata_2_value: record[:metadata_2_value],
|
|
subname: view.template_name,
|
|
view: view.to_html,
|
|
}
|
|
end
|
|
end
|
|
end
|