Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.26 canada #27

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions decidim-core/app/cells/decidim/diff/attribute.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
</h3>
</div>

<div class="row diff_view diff_view_unified_unescaped<%= " hide" unless show_html_view_dropdown? %>" id="<%= attribute_diff_id("diff_view_unified_unescaped") %>">
<%= diff_unified(data, :unescaped_html) %>
<div class="row diff_view diff_view_unified" id="attribute-diff-unified-<%= data[:label].parameterize %>-<%= format %>">
<%= diff_unified(data, format) %>
</div>

<div class="row diff_view diff_view_unified_escaped<%= " hide" if show_html_view_dropdown? %>" id="<%= attribute_diff_id("diff_view_unified_escaped") %>">
<%= diff_unified(data, :html) %>
<div class="row diff_view diff_view_split hide" id="attribute-diff-unified-<%= data[:label].parameterize %>-<%= format %>">
<%= diff_split(data, "left", format) %>
<%= diff_split(data, "right", format) %>
</div>

<div class="row diff_view diff_view_split_unescaped hide" id="<%= attribute_diff_id("diff_view_split_unescaped") %>">
<%= diff_split(data, "left", :unescaped_html) %>

<%= diff_split(data, "right", :unescaped_html) %>
</div>

<div class="row diff_view diff_view_split_escaped hide" id="<%= attribute_diff_id("diff_view_split_escaped") %>">
<%= diff_split(data, "left", :html) %>

<%= diff_split(data, "right", :html) %>
</div>
</div>
4 changes: 2 additions & 2 deletions decidim-core/app/cells/decidim/diff/diff_mode_dropdown.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

<ul class="menu is-dropdown-submenu" id="diffmode-chooser-menu" role="menu" aria-labelledby="diff-view-selected">
<li role="presentation">
<%= link_to "#diff-view-unified", class: "diff-view-mode", id:"diff-view-unified", role: "menuitem" do %>
<%= link_to "#diff-view-unified", class: "diff-view-mode diff-view-unified", id:"diff-view-unified", role: "menuitem" do %>
<%= t("versions.dropdown.option_unified") %>
<% end %>
</li>

<li role="presentation">
<%= link_to "#diff-view-split", class: "diff-view-mode", id:"diff-view-split", role: "menuitem" do %>
<%= link_to "#diff-view-split", class: "diff-view-mode diff-view-split", id:"diff-view-split", role: "menuitem" do %>
<%= t("versions.dropdown.option_split") %>
<% end %>
</li>
Expand Down
36 changes: 0 additions & 36 deletions decidim-core/app/cells/decidim/diff/diff_mode_html.erb

This file was deleted.

14 changes: 10 additions & 4 deletions decidim-core/app/cells/decidim/diff/diff_split.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
<%= t(direction, scope: "versions.directions") %>
</div>

<div class="card">
<div class="diff-data">
<%= output_split_diff(data, direction, format) %>
<% available_locales_for(data).each do |locale, filled| %>
<% next unless filled %>
<% unless I18n.locale.to_s == locale %>
<p><strong><%= locale_name(locale) %></strong></p>
<% end %>
<div class="card">
<div class="diff-data">
<%= output_split_diff(data, direction, format, locale) %>
</div>
</div>
</div>
<% end %>
</div>
14 changes: 10 additions & 4 deletions decidim-core/app/cells/decidim/diff/diff_unified.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div class="mediumlarge-12 columns">
<div class="card">
<div class="diff-data">
<%= output_unified_diff(data, format) %>
<% available_locales_for(data).each do |locale, filled| %>
<% next unless filled %>
<% unless I18n.locale.to_s == locale %>
<p><strong><%= locale_name(locale) %></strong></p>
<% end %>
<div class="card">
<div class="diff-data">
<%= output_unified_diff(data, format, locale) %>
</div>
</div>
</div>
<% end %>
</div>
54 changes: 48 additions & 6 deletions decidim-core/app/cells/decidim/diff/show.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
<%= render :diff_mode_dropdown %>
<%= render :diff_mode_html %>
<div class="versions-diff">
<div class="versions-selector">
<ul class="tabs" data-tabs id="diff-tabs">
<li class="tabs-title is-active" id="diff-text-title">
<!-- Wrap the anchor inside a div to keep correct button styling -->
<div>
<%= link_to "#diff-text", class: "button button--nomargin button--sc hollow" do %>
<%= t("versions.tabs.text") %>
<% end %>
</div>
</li>
<li class="tabs-title" id="diff-source-title">
<!-- Wrap the anchor inside a div to keep correct button styling -->
<div>
<%= link_to "#diff-source", class: "button button--nomargin button--sc hollow" do %>
<%= t("versions.tabs.source") %>
<% end %>
</div>
</li>
<li class="tabs-title" id="diff-preview-title">
<!-- Wrap the anchor inside a div to keep correct button styling -->
<div>
<%= link_to "#diff-preview", class: "button button--nomargin button--sc hollow" do %>
<%= t("versions.tabs.preview") %>
<% end %>
</div>
</li>
</ul>
</div>

<div class="tabs-content" data-tabs-content="diff-tabs">

<div class="tabs-panel is-active" id="diff-text">
<%= render :diff_mode_dropdown %>
<% diff_data.each do |data| %>
<%= attribute(data, :text) %>
<% end %>
</div>
<div class="tabs-panel" id="diff-source">
<%= render :diff_mode_dropdown %>
<% diff_data.each do |data| %>
<%= attribute(data, :html) %>
<% end %>
</div>
<div class="tabs-panel" id="diff-preview">
<%= preview %>
</div>
</div>

<div aria-live="polite">
<% diff_data.each do |data| %>
<%= attribute(data) %>
<% end %>
</div>
71 changes: 53 additions & 18 deletions decidim-core/app/cells/decidim/diff_cell.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

require "decidim/diffy_extension"

module Decidim
# This cell renders the diff between `:old_data` and `:new_data`.
class DiffCell < Decidim::ViewModel
include Cell::ViewModel::Partial
include ::HtmlToPlainText # from the premailer gem
include LanguageChooserHelper
include LayoutHelper

def attribute(data)
render locals: { data: data }
def attribute(data, format)
render locals: { data: data, format: format }
end

def diff_unified(data, format)
Expand All @@ -22,12 +22,6 @@ def diff_split(data, direction, format)

private

# Adds a unique ID prefix for the attribute div IDs to avoid duplicate IDs
# in the DOM.
def attribute_diff_id(id)
"#{SecureRandom.uuid}_#{id}"
end

# A PaperTrail::Version.
def current_version
model
Expand All @@ -38,6 +32,11 @@ def item
current_version.item
end

# preview (if associated item allows it)
def preview
diff_renderer.preview
end

# DiffRenderer class for the current_version's item; falls back to `BaseDiffRenderer`.
def diff_renderer_class
if current_version.item_type.deconstantize == "Decidim"
Expand All @@ -63,34 +62,70 @@ def diff_data
diff_renderer.diff.values
end

def available_locales_for(data)
locales = { I18n.locale.to_s => true }

locales.merge! valid_locale_keys(data[:old_value]) if data[:old_value].is_a?(Hash)
locales.merge! valid_locale_keys(data[:new_value]) if data[:new_value].is_a?(Hash)

locales.filter { |k| I18n.locale_available?(k) }
end

def valid_locale_keys(input)
locales = input.transform_values(&:present?)
locales.merge!(input["machine_translations"].transform_values(&:present?)) if input["machine_translations"].is_a?(Hash)
locales
end

# Outputs the diff as HTML with inline highlighting of the character
# changes between lines.
#
# Returns an HTML-safe string.
def output_unified_diff(data, format)
def output_unified_diff(data, format, locale)
Diffy::Diff.new(
data[:old_value].to_s,
data[:new_value].to_s,
old_new_values(data, format, locale)[0],
old_new_values(data, format, locale)[1],
allow_empty_diff: false,
include_plus_and_minus_in_html: true
).to_s(format)
).to_s(:html)
end

# Outputs the diff as HTML with side-by-side changes between lines.
# Splits it in two parts (or two sides): left and right.
# The left side represents deletions while the right side represents insertions.
#
# Returns an HTML-safe string.
def output_split_diff(data, direction, format)
def output_split_diff(data, direction, format, locale)
Diffy::SplitDiff.new(
data[:old_value].to_s,
data[:new_value].to_s,
old_new_values(data, format, locale)[0],
old_new_values(data, format, locale)[1],
allow_empty_diff: false,
format: format,
format: :html,
include_plus_and_minus_in_html: true
).send(direction)
end

def old_new_values(data, format, locale)
original_translations = data[:old_value].try(:filter) { |key, value| value.present? && key != "machine_translations" }
[
value_from_locale(data[:old_value], format, locale),
value_from_locale(data[:new_value], format, locale, original_translations)
]
end

def value_from_locale(value, format, locale, skip_machine_keys = {})
text = value.is_a?(Hash) ? find_locale_value(value, locale, skip_machine_keys) : value

text = text.first if text.is_a?(Array)
# return text.to_s if format == :html || text.blank?

convert_to_text(text.to_s.dup, 100)
end

def find_locale_value(input, locale, skip_machine_keys = {})
input.dig(locale).presence || skip_machine_keys[locale].presence || input.dig("machine_translations", locale)
end

# Gives the option to view HTML unescaped for better user experience.
# Official means created from admin (where rich text editor is enabled).
def show_html_view_dropdown?
Expand Down
1 change: 1 addition & 0 deletions decidim-core/app/commands/decidim/update_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def update_personal_data
@user.email = @form.email
@user.personal_url = @form.personal_url
@user.about = @form.about
@user.time_zone = @form.time_zone
end

def update_avatar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def use_organization_time_zone(&action)
#
# Returns a String.
def organization_time_zone
@organization_time_zone ||= current_organization.time_zone
@organization_time_zone ||= current_user&.time_zone.presence || current_organization.time_zone
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions decidim-core/app/forms/decidim/account_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AccountForm < Form
attribute :remove_avatar, Boolean, default: false
attribute :personal_url
attribute :about
attribute :time_zone

validates :name, presence: true, format: { with: Decidim::User::REGEXP_NAME }
validates :email, presence: true, "valid_email_2/email": { disposable: true }
Expand All @@ -28,6 +29,7 @@ class AccountForm < Form
validates :password, password: { name: :name, email: :email, username: :nickname }, if: -> { password.present? }
validates :password_confirmation, presence: true, if: :password_present
validates :avatar, passthru: { to: Decidim::User }
validates :time_zone, time_zone: true, if: -> { time_zone.present? }

validate :unique_email
validate :unique_nickname
Expand Down
1 change: 1 addition & 0 deletions decidim-core/app/models/decidim/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def self.all
validates :tos_agreement, acceptance: true, allow_nil: false, on: :create
validates :tos_agreement, acceptance: true, if: :user_invited?
validates :email, :nickname, uniqueness: { scope: :organization }, unless: -> { deleted? || managed? || nickname.blank? }
validates :time_zone, time_zone: true, if: -> { time_zone.present? }

validate :all_roles_are_valid

Expand Down
Loading
Loading