Skip to content

Commit

Permalink
Short link sharing (decidim#13847)
Browse files Browse the repository at this point in the history
* Added short link on share modal

* Fixed tests

* Small fix

* Insert short_link dependency

* Revert helpers

* Apply review recommendations

* Fix pipeline

* Rubocops

* Apply review recommendation

---------

Co-authored-by: Alexandru Emil Lupu <[email protected]>
  • Loading branch information
andra-panaite and alecslupu authored Feb 12, 2025
1 parent 061a4ac commit e82bb6a
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= cell "decidim/comments_button", nil %>

<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", post %>
</div>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ edit_link(
<%= cell "decidim/comments_button", nil %>

<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", project %>
</div>
</section>

Expand Down
26 changes: 26 additions & 0 deletions decidim-core/app/cells/decidim/share_widget/modal.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= decidim_modal id: "socialShare", class: "share-modal" do %>
<div data-dialog-container>
<%= icon "share-line" %>
<h2 id="dialog-title-socialShare" tabindex="-1" data-dialog-title><%= t("share", scope: "decidim.shared.share_modal") %></h2>
<div>
<%= social_share_button_tag(decidim_page_title,
url: decidim_meta_url,
image: decidim_meta_image_url,
desc: decidim_html_escape(decidim_meta_description || ""),
via: decidim_meta_twitter_handler) %>

<div class="share-modal__input">
<input id="urlShareLink" type="text" title="<%= t("share_link" , scope: "decidim.shared.share_modal") %>" value="<%= resource_name ? short_url(route_name: resource_name, params:) : decidim_meta_url %>" readonly>
<button type="button"
class="button button__sm button__text-secondary"
data-clipboard-copy="#urlShareLink"
data-clipboard-copy-label="<%= t("copy_share_link_copied", scope: "decidim.shared.share_modal") %>"
data-clipboard-copy-message="<%= t("copy_share_link_message", scope: "decidim.shared.share_modal") %>"
title="<%= t("copy_share_link_clarification", scope: "decidim.shared.share_modal") %>">
<span><%= t("copy_share_link", scope: "decidim.shared.share_modal") %></span>
<%= icon "file-copy-line" %>
</button>
</div>
</div>
</div>
<% end %>
2 changes: 2 additions & 0 deletions decidim-core/app/cells/decidim/share_widget/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= cell("decidim/share_button", model) %>
<%= render :modal %>
20 changes: 20 additions & 0 deletions decidim-core/app/cells/decidim/share_widget_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Decidim
class ShareWidgetCell < Decidim::ViewModel
include Decidim::ShortLinkHelper
include Decidim::SocialShareButtonHelper

def show
render
end

private

def resource_name
return "budget_project" if model.is_a?(Decidim::Budgets::Project)

model.class.name.demodulize.underscore
end
end
end
26 changes: 0 additions & 26 deletions decidim-core/app/views/decidim/shared/_share_modal.html.erb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<%= render partial: "decidim/shared/confirm_modal" %>
<%= render partial: "decidim/shared/login_modal" unless current_user %>
<%= render partial: "decidim/shared/authorization_modal" %>
<%= render partial: "decidim/shared/share_modal" %>
<%= render partial: "layouts/decidim/decidim_javascript" %>
<%= render partial: "layouts/decidim/data_consent_warning" %>

Expand Down
36 changes: 36 additions & 0 deletions decidim-core/spec/system/social_share_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
require "spec_helper"

describe "Social share button" do
let!(:organization) { create(:organization) }
let!(:resource) { create(:dummy_resource) }
let(:resource_path) { Decidim::ResourceLocatorPresenter.new(resource).path }

let(:short_link) do
create(
:short_link,
target: resource,
mounted_engine_name: engine_name,
route_name:
)
end
let(:engine_name) { "decidim_participatory_process_dummy" }
let(:route_name) { "dummy_resource" }

before { switch_to_host(resource.organization.host) }

shared_examples_for "showing the social share buttons" do
Expand Down Expand Up @@ -82,4 +94,28 @@
end
end
end

context "when sharing resource by link" do
before do
visit resource_path
click_on "Share"
end

it "short link is displayed on the share modal" do
within ".share-modal__input" do
expect(page).to have_css("button[data-clipboard-copy='#urlShareLink']")
link = find("input#urlShareLink")
within "#urlShareLink" do
expect(page).to have_no_content(resource_path)
expect(link[:value]).to include("#{resource.organization.host}:#{Capybara.current_session.server.port}/s/")
end
end
end

it "short link redirects to the correct resource path" do
visit "http://#{resource.organization.host}:#{Capybara.server_port}/s/#{short_link.identifier}"

expect(page).to have_current_path(resource_path)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ edit_link(

<%= cell "decidim/comments_button", nil %>
<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", debate %>
</div>
</section>
<%= cell "decidim/endorsers_list", debate %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Decidim
module Design
module ApplicationHelper
include Decidim::ApplicationHelper
include Decidim::IconHelper

# For the moment keep this as a constant and later decide where to move
Expand Down
6 changes: 3 additions & 3 deletions decidim-design/app/helpers/decidim/design/share_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def share_sections
type: :cell_table,
options: { headings: [t("decidim.design.helpers.share_button")] },
cell_snippet: {
cell: "decidim/share_button",
args: [],
call_string: 'cell("decidim/share_button", nil)'
cell: "decidim/share_widget",
args: [{}],
call_string: 'cell("decidim/share_widget", resource)'
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% add_decidim_page_title(t(".title")) %>
<% content_for :heading do %>
<%= t(".title") %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<section class="layout-main__section layout-main__buttons" data-buttons>
<%= cell "decidim/comments_button", nil %>
<div class="ml-auto">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", @commentable %>
</div>
</section>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ edit_link(
<%= cell "decidim/comments_button", nil, display: current_initiative.commentable? && current_initiative.published? %>
<% end %>
<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", current_initiative %>
</div>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<%= cell "decidim/comments_button", nil %>

<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", meeting %>
</div>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<section class="layout-main__section layout-main__buttons" data-buttons>
<%= cell "decidim/comments_button", nil %>
<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", @collaborative_draft %>
</div>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= cell "decidim/comments_button", nil %>

<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", @proposal %>
</div>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ edit_link(
<%= cell "decidim/comments_button", nil %>

<div class="ml-auto lg:ml-0">
<%= cell "decidim/share_button", nil %>
<%= cell "decidim/share_widget", sortition %>
</div>
</section>

Expand Down

0 comments on commit e82bb6a

Please sign in to comment.