|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module BetterTogether |
| 4 | + # Helper methods for rendering JSON-LD structured data for schema.org |
| 5 | + module StructuredDataHelper |
| 6 | + def structured_data_tag(data) |
| 7 | + return if data.blank? |
| 8 | + |
| 9 | + tag.script(type: 'application/ld+json') do |
| 10 | + raw(Array.wrap(data).to_json) |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + def platform_structured_data(platform) |
| 15 | + { |
| 16 | + '@context': 'https://schema.org', |
| 17 | + '@type': 'WebSite', |
| 18 | + name: platform.name, |
| 19 | + url: platform.url |
| 20 | + } |
| 21 | + end |
| 22 | + |
| 23 | + def community_structured_data(community) |
| 24 | + data = { |
| 25 | + '@context': 'https://schema.org', |
| 26 | + '@type': 'Organization', |
| 27 | + name: community.name, |
| 28 | + url: community_url(community) |
| 29 | + } |
| 30 | + data[:description] = community.description.to_plain_text if community.respond_to?(:description) && community.description.present? |
| 31 | + if community.logo.attached? |
| 32 | + attachment = community.respond_to?(:optimized_logo) ? community.optimized_logo : community.logo |
| 33 | + data[:logo] = rails_storage_proxy_url(attachment) |
| 34 | + end |
| 35 | + data |
| 36 | + end |
| 37 | + |
| 38 | + def event_structured_data(event) |
| 39 | + data = { |
| 40 | + '@context': 'https://schema.org', |
| 41 | + '@type': 'Event', |
| 42 | + name: event.name, |
| 43 | + startDate: event.starts_at&.iso8601, |
| 44 | + endDate: event.ends_at&.iso8601, |
| 45 | + url: event_url(event) |
| 46 | + } |
| 47 | + data[:description] = event.description.to_plain_text if event.respond_to?(:description) && event.description.present? |
| 48 | + data[:location] = event.location.to_s if event.respond_to?(:location) && event.location.present? |
| 49 | + data.compact |
| 50 | + end |
| 51 | + end |
| 52 | +end |
0 commit comments