]> git.openstreetmap.org Git - rails.git/commitdiff
Set Open Graph image to first image for diary entries
authorAnton Khorev <tony29@yandex.ru>
Thu, 6 Jun 2024 14:28:57 +0000 (17:28 +0300)
committerAnton Khorev <tony29@yandex.ru>
Thu, 6 Jun 2024 14:28:57 +0000 (17:28 +0300)
app/controllers/diary_entries_controller.rb
app/helpers/open_graph_helper.rb
app/models/diary_entry.rb
app/views/layouts/_meta.html.erb
test/controllers/diary_entries_controller_test.rb

index 9af36709eb20be1a2055a964daf2697d3443af1f..f3fbcd1fd0193aed5b70015f24490e0b2a7176bd 100644 (file)
@@ -68,6 +68,7 @@ class DiaryEntriesController < ApplicationController
     @entry = entries.find_by(:id => params[:id])
     if @entry
       @title = t ".title", :user => params[:display_name], :title => @entry.title
     @entry = entries.find_by(:id => params[:id])
     if @entry
       @title = t ".title", :user => params[:display_name], :title => @entry.title
+      @og_image = @entry.body.image
       @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
     else
       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
       @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
     else
       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
index 27d8d2451812e24e9c77c2ce60a7de49863860d9..89aeee6b91edf301d02baf88ed32bd2309b5db90 100644 (file)
@@ -1,10 +1,10 @@
 module OpenGraphHelper
 module OpenGraphHelper
-  def opengraph_tags(title = nil)
+  def opengraph_tags(title = nil, og_image = nil)
     tags = {
       "og:site_name" => t("layouts.project_name.title"),
       "og:title" => [title, t("layouts.project_name.title")].compact.join(" | "),
       "og:type" => "website",
     tags = {
       "og:site_name" => t("layouts.project_name.title"),
       "og:title" => [title, t("layouts.project_name.title")].compact.join(" | "),
       "og:type" => "website",
-      "og:image" => image_url("osm_logo_256.png"),
+      "og:image" => og_image || image_url("osm_logo_256.png"),
       "og:url" => url_for(:only_path => false),
       "og:description" => t("layouts.intro_text")
     }
       "og:url" => url_for(:only_path => false),
       "og:description" => t("layouts.intro_text")
     }
index e83dfb9ee47af81706161260781f7bb663a8fc88..089c7e6c60fecb1ebcc92238a00eb0d1e37a7c9e 100644 (file)
@@ -50,7 +50,7 @@ class DiaryEntry < ApplicationRecord
   after_save :spam_check
 
   def body
   after_save :spam_check
 
   def body
-    RichText.new(self[:body_format], self[:body])
+    @body ||= RichText.new(self[:body_format], self[:body])
   end
 
   private
   end
 
   private
index 2352ad0b6931993eb488bb5e5e257b869241d7e9..37afbb89498e5cc67a2e101a17c4e83d312bc62f 100644 (file)
@@ -21,7 +21,7 @@
 <% end -%>
 <%= tag.link :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") %>
 <%= tag.meta :name => "description", :content => "OpenStreetMap is the free wiki world map." %>
 <% end -%>
 <%= tag.link :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") %>
 <%= tag.meta :name => "description", :content => "OpenStreetMap is the free wiki world map." %>
-<%= opengraph_tags(@title) %>
+<%= opengraph_tags(@title, @og_image) %>
 <% if flash[:matomo_goal] -%>
 <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %>
 <% end -%>
 <% if flash[:matomo_goal] -%>
 <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %>
 <% end -%>
index d13a50163ea8bc59e15af03885e25385379ac71a..765391d24584eb3de83a343cff3e91632eb03392 100644 (file)
@@ -752,6 +752,17 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     end
   end
 
     end
   end
 
+  def test_show_og_image
+    user = create(:user)
+    diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
+
+    get diary_entry_path(user, diary_entry)
+    assert_response :success
+    assert_dom "head meta[property='og:image']" do
+      assert_dom "> @content", "https://example.com/picture.jpg"
+    end
+  end
+
   def test_hide
     user = create(:user)
     diary_entry = create(:diary_entry, :user => user)
   def test_hide
     user = create(:user)
     diary_entry = create(:diary_entry, :user => user)