Skip to content

Commit

Permalink
change erb
Browse files Browse the repository at this point in the history
  • Loading branch information
antopalidi committed Sep 4, 2024
1 parent 86ea930 commit 9197173
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 59 deletions.
40 changes: 36 additions & 4 deletions decidim-admin/app/commands/decidim/admin/create_share_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,44 @@ module Decidim
module Admin
# A command with all the business logic to create a taxonomy.
# This command is called from the controller.
class CreateShareToken < Decidim::Commands::CreateResource
fetch_form_attributes :token, :expires_at, :registered_only, :organization, :user, :token_for
class CreateShareToken < Decidim::Command
# Public: Initializes the command.
#
# form - A form object with the params.
def initialize(form)
@form = form
end

protected
# Executes the command. Broadcasts these events:
#
# - :ok when everything is valid.
# - :invalid if the form wasn't valid and we couldn't proceed.
#
# Returns nothing.
def call
return broadcast(:invalid) if form.invalid?

def resource_class = Decidim::ShareToken
create_share_token
broadcast(:ok)
end

private

attr_reader :form

def create_share_token
Decidim.traceability.create!(
ShareToken,
form.user,
{
token: form.token,
expires_at: form.expires_at,
token_for: form.token_for,
organization: form.organization,
user: form.user
}
)
end
end
end
end
87 changes: 50 additions & 37 deletions decidim-admin/app/views/decidim/admin/share_tokens/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
<% add_decidim_page_title(t(".title", name: resource_title)) %>
<div class="card share_tokens">
<div class="item_show__header">
<h2 class="item_show__header-title">
<div class="card-divider">
<h2 class="card-title">
<%= t(".title", name: resource_title) %>
<a class="button button__sm button__secondary" href="<%= share_tokens_path(:new) %>"><%= icon "add-line" %><%= t(".new_share_token_button") %></a>
<%= link_to t(".new_share_token_button"), share_tokens_path(:new), class: "button tiny button--title new" %>
</h2>
</div>
<div class="mb-4">
<p class="help-text whitespace-pre-line"><%= t(".share_tokens_help", clipboard: icon("clipboard-line", class: "inline-block mb-1")).html_safe %></p>
</div>
<% if @share_tokens.any? %>
<div class="table-scroll">
<table class="table-list">
<thead>
<tr>
<th><%= sort_link(query, :token, t("models.share_token.fields.token", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :expires_at, t("models.share_token.fields.expires_at", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :registered_only, t("models.share_token.fields.registered_only", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :times_used, t("models.share_token.fields.times_used", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= t("models.share_token.fields.actions", scope: "decidim.admin") %></th>
</tr>
</thead>
<tbody>
<% @share_tokens.each do |share_token| %>
<div class="card-section">
<div>
<p class="help-text whitespace-pre-line"><%= t(".share_tokens_help", clipboard: icon("clipboard", class: "inline-block mb-1")).html_safe %></p>
</div>
<% if @share_tokens.any? %>
<div class="card-section">
<div class="table-scroll">
<table class="table-list">
<thead>
<tr>
<td id="js-token-<%= share_token.id %>"><%= share_token.token %></td>
<td><%= share_token.expires_at.present? ? ("<span class=\"text-#{share_token.expired? ? "warning" : ""}\">#{l(share_token.expires_at, format: :decidim_short)}</span>".html_safe) : content_tag(:em, t(".never")) %></td>
<td><%= t("booleans.#{share_token.registered_only.present?}") %></td>
<td><%= share_token.times_used %></td>
<td class="table-list__actions">
<%= icon_link_to "pencil-line", share_tokens_path(:edit, id: share_token ), t("actions.edit", scope: "decidim.admin.share_tokens"), class: "action-icon--edit" %>
<%= icon_link_to "clipboard-line", "#", t("actions.copy_link", scope: "decidim.admin.share_tokens"), class: "action-icon--copy", data: { "clipboard-copy" => "#js-token-#{share_token.id}", "clipboard-content" => share_token.url,"clipboard-copy-label" => t(".copied"),"clipboard-copy-message" => t(".copy_message") } %>
<%= icon_link_to "eye-line", share_token.url, t("actions.preview", scope: "decidim.admin.share_tokens"), class: "action-icon--preview", target: :blank %>
<%= icon_link_to "delete-bin-line", share_tokens_path(:destroy, id: share_token ), t("actions.destroy", scope: "decidim.admin.share_tokens"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin.share_tokens") } %>
</td>
<th><%= sort_link(query, :token, t("models.share_token.fields.token", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :expires_at, t("models.share_token.fields.expires_at", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :registered_only, t("models.share_token.fields.registered_only", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= sort_link(query, :times_used, t("models.share_token.fields.times_used", scope: "decidim.admin"), default_order: :desc) %></th>
<th><%= t("models.share_token.fields.actions", scope: "decidim.admin") %></th>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<p><%= t(".empty", new_token_link: link_to(t(".create_new_token"), share_tokens_path(:new) , class: "button button__text-secondary")).html_safe %></p>
<% end %>
</thead>
<tbody>
<% @share_tokens.each do |share_token| %>
<tr>
<td id="js-token-<%= share_token.id %>"><%= share_token.token %></td>
<td><%= share_token.expires_at.present? ? ("<span class=\"text-#{share_token.expired? ? "warning" : ""}\">#{l(share_token.expires_at, format: :decidim_short)}</span>".html_safe) : content_tag(:em, t(".never")) %></td>
<td><%= t("booleans.#{share_token.registered_only.present?}") %></td>
<td><%= share_token.times_used %></td>
<td class="table-list__actions">
<%= icon_link_to "pencil", share_tokens_path(:edit, id: share_token ), t("actions.edit", scope: "decidim.admin.share_tokens"), class: "action-icon--edit" %>
<%= icon_link_to "clipboard",
"#",
t("actions.copy_link", scope: "decidim.admin.share_tokens"),
class: "action-icon--copy",
data: {
"clipboard-copy" => "#js-token-#{share_token.id}",
"clipboard-content" => share_token.url,
"clipboard-copy-label" => t(".copied"),
"clipboard-copy-message" => t(".copy_message") } %>
<%= icon_link_to "eye", share_token.url, t("actions.preview", scope: "decidim.admin.share_tokens"), class: "action-icon--preview", target: :blank %>
<%= icon_link_to "trash", share_tokens_path(:destroy, id: share_token ), t("actions.destroy", scope: "decidim.admin.share_tokens"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin.share_tokens") } %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<% else %>
<p><%= t(".empty", new_token_link: link_to(t(".create_new_token"), share_tokens_path(:new) , class: "")).html_safe %></p>
<% end %>
</div>
</div>
<%= decidim_paginate @share_tokens %>
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ module Decidim::Admin
subject { described_class.new(form) }

let(:organization) { create(:organization) }
let(:current_user) { create(:user, :admin, organization:) }
let(:component) { create(:component, participatory_space: create(:participatory_process, organization:)) }
let(:current_user) { create(:user, :admin, organization: organization) }
let(:component) { create(:component, participatory_space: create(:participatory_process, organization: organization)) }

let(:form) do
ShareTokenForm.from_params(
token:,
expires_at:,
automatic_token:,
no_expiration:,
registered_only:
token: token,
expires_at: expires_at,
automatic_token: automatic_token,
no_expiration: no_expiration,
registered_only: registered_only
).with_context(
current_user:,
current_user: current_user,
current_organization: organization,
resource: component
)
Expand Down
18 changes: 9 additions & 9 deletions decidim-admin/spec/forms/share_token_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
module Decidim::Admin
describe ShareTokenForm do
let(:organization) { create(:organization) }
let(:current_user) { create(:user, :admin, organization:) }
let(:component) { create(:component, participatory_space: create(:participatory_process, organization:)) }
let(:current_user) { create(:user, :admin, organization: organization) }
let(:component) { create(:component, participatory_space: create(:participatory_process, organization: organization)) }

let(:form) do
described_class.from_params(
token:,
automatic_token:,
expires_at:,
no_expiration:,
registered_only:
token: token,
automatic_token: automatic_token,
expires_at: expires_at,
no_expiration: no_expiration,
registered_only: registered_only
).with_context(
current_user:,
current_user: current_user,
current_organization: organization,
resource: component
)
Expand Down Expand Up @@ -91,7 +91,7 @@ module Decidim::Admin

context "when token exists" do
let(:automatic_token) { false }
let!(:share_token) { create(:share_token, organization:, token_for: component, token:) }
let!(:share_token) { create(:share_token, organization: organization, token_for: component, token: token) }

it "validates uniqueness of token" do
expect(form).to be_invalid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class AddRegisteredOnlyToDecidimShareTokens < ActiveRecord::Migration[7.0]
class AddRegisteredOnlyToDecidimShareTokens < ActiveRecord::Migration[6.1]
def change
add_column :decidim_share_tokens, :registered_only, :boolean
end
Expand Down

0 comments on commit 9197173

Please sign in to comment.