Skip to content
Merged
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
26 changes: 13 additions & 13 deletions app/assets/stylesheets/lit/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,47 @@
*= require './backend/jquery-te-1.4.0.css'
*= require_self
*/
.detail_wrapper{
padding: 10px 0 0 50px;
.detail_wrapper {
padding: 10px 0 0 10px;
}
.detail_wrapper table tr td.locale_row{
.detail_wrapper table tr td.locale_row {
width: 75px;
}
.localization_key_row .localization_keys_options{
.localization_key_row .localization_keys_options {
display: none;
float: right;
}
.localization_key_row:hover .localization_keys_options{
.localization_key_row:hover .localization_keys_options {
display: block;
}

li.key_prefix .fa-chevron-right {
float: right;
margin-top: 2px;
margin-right: -6px;
opacity: .25;
opacity: 0.25;
}

.hidden{
.hidden {
display: none;
}
i.fa{
i.fa {
color: black;
}
.nav.nav-stacked>li>a {
padding: 5px 7px;
.nav.nav-stacked > li > a {
padding: 5px 7px;
}
.well{
.well {
background-color: white;
border-radius: 0px;
}
.well label{
.well label {
font-weight: normal;
}
.well .form-search {
margin-bottom: 15px;
}
.localization_row em{
.localization_row em {
color: #bbb;
}
.loading {
Expand Down
76 changes: 38 additions & 38 deletions app/controllers/lit/localization_keys_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module Lit
class LocalizationKeysController < ::Lit::ApplicationController
before_action :find_localization_scope,
except: %i[destroy find_localization]
before_action :find_localization_key,
only: %i[star destroy change_completed restore_deleted]
before_action :find_localization_scope, except: %i[destroy find_localization]
before_action :find_localization_key, only: %i[star destroy change_completed restore_deleted]

def index
get_localization_keys
Expand All @@ -20,27 +18,19 @@ def visited_again
end

def find_localization
localization_key = Lit::LocalizationKey.find_by!(
localization_key: params[:key]
)
localization_key = Lit::LocalizationKey.find_by!(localization_key: params[:key])
locale = Lit::Locale.find_by!(locale: params[:locale])
localization = localization_key.localizations.find_by(locale_id: locale)
render json: {
path: localization_key_localization_path(localization_key, localization)
}
render json: { path: localization_key_localization_path(localization_key, localization) }
end

def starred
@scope = @scope.where(is_starred: true)

if defined?(Kaminari) &&
@scope.respond_to?(Kaminari.config.page_method_name)
if defined?(Kaminari) && @scope.respond_to?(Kaminari.config.page_method_name)
@scope = @scope.send(Kaminari.config.page_method_name, params[:page])
end
if defined?(WillPaginate) &&
@scope.respond_to?(:paginate)
@scope = @scope.paginate(page: params[:page])
end
@scope = @scope.paginate(page: params[:page]) if defined?(WillPaginate) && @scope.respond_to?(:paginate)
get_localization_keys
render action: :index
end
Expand All @@ -66,26 +56,33 @@ def destroy
respond_to :js
end

def batch_touch
get_localization_keys
localization_key_ids = @scope.distinct(false).pluck(:id)
if localization_key_ids.any?
@scope.distinct(false).update_all updated_at: Time.current
Localization.where(localization_key_id: localization_key_ids).update_all updated_at: Time.current
end
respond_to :js
end

private

def find_localization_key
@localization_key = LocalizationKey.find params[:id].to_i
end

def find_localization_scope
@search_options = if params.respond_to?(:permit)
params.permit(*valid_keys)
else
params.slice(*valid_keys)
end
@scope = LocalizationKey.distinct.active
.preload(localizations: :locale)
.search(@search_options)
@search_options = params.respond_to?(:permit) ? params.permit(*valid_keys) : params.slice(*valid_keys)
@scope = LocalizationKey.distinct.active.preload(localizations: :locale).search(@search_options)
end

def get_localization_keys
key_parts = @search_options[:key_prefix].to_s.split('.').length
@prefixes = @scope.reorder(nil).distinct.pluck(:localization_key).map { |lk| lk.split('.').shift(key_parts + 1).join('.') }.uniq.sort
@prefixes =
@scope.reorder(nil).distinct.pluck(:localization_key).map do |lk|
lk.split('.').shift(key_parts + 1).join('.')
end.uniq.sort
if @search_options[:key_prefix].present?
parts = @search_options[:key_prefix].split('.')
@parent_prefix = parts[0, parts.length - 1].join('.')
Expand All @@ -104,16 +101,15 @@ def valid_keys
end

def grouped_localizations
@_grouped_localizations ||= begin
{}.tap do |hash|
@localization_keys.each do |lk|
hash[lk] = {}
lk.localizations.each do |l|
hash[lk][l.locale.locale.to_sym] = l
@_grouped_localizations ||=
begin
{}.tap do |hash|
@localization_keys.each do |lk|
hash[lk] = {}
lk.localizations.each { |l| hash[lk][l.locale.locale.to_sym] = l }
end
end
end
end
end

def localization_for(locale, localization_key)
Expand All @@ -136,12 +132,16 @@ def localization_for(locale, localization_key)
helper_method :localization_for

def versions?(localization)
@_versions ||= begin
ids = grouped_localizations.values.map(&:values).flatten.map(&:id)
Lit::Localization.active.where(id: ids).joins(:versions).group(
"#{Lit::Localization.quoted_table_name}.id"
).count
end
@_versions ||=
begin
ids = grouped_localizations.values.map(&:values).flatten.map(&:id)
Lit::Localization
.active
.where(id: ids)
.joins(:versions)
.group("#{Lit::Localization.quoted_table_name}.id")
.count
end
@_versions[localization.id].to_i > 0
end
helper_method :versions?
Expand Down
27 changes: 17 additions & 10 deletions app/views/lit/localization_keys/_localizations_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<table class="table">
<div class="col-12 text-right">
<%= link_to "batch touch", lit.batch_touch_localization_keys_path(key: params[:key], key_prefix: params[:key_prefix]), method: :post, remote: true, class: 'btn btn-sm btn-primary', data: { confirm: 'This will "touch" all search results making them subject of synchronization. Proceed?'} %>
</div>
<table class="table mt-1">
<%- @localization_keys.each do |lk| %>
<tr class="localization_key_row" data-id="<%= lk.id %>">
<td>
Expand Down Expand Up @@ -30,26 +33,30 @@
<div class="detail_wrapper">
<table class="table table-bordered table-striped">
<tr>
<th class="col-md-8">Translation</th>
<th class="col-md-2 text-center">Locale</th>
<th class="col-md-1"></th>
<th class="col-md-9">Translation</th>
<th class="col-md-1 text-center">Locale</th>
<% unless lk.is_deleted? %>
<th class="col-md-2 text-center">Completed</th>
<th class="col-md-1 text-center">Completed</th>
<% end %>
</tr>
<%- available_locales.each do |locale| %>
<%- localization = localization_for(locale, lk) %>
<tr>
<td>
<% if localization %>
<%= draw_icon 'clock-o', title: "Last updated at #{localization.updated_at.to_s(:db)}" %>
<%= link_to lit.previous_versions_localization_key_localization_path(lk, localization, format: :js), class: "js-show_prev_versions #{'hidden' unless versions?(localization)}", remote: true do %>
<%= draw_icon 'random', title: I18n.t('lit.common.previous_versions', default: 'Previous versions') %>
<% end %>
<% end %>
</td>
<td class="localization_row" data-id="<%= localization.id%>" data-edit="<%= edit_localization_key_localization_path(lk, localization, format: :js) %>" data-editing=0 data-content="">
<%= render partial: 'localization_row', locals: {localization: Lit.init.cache["#{locale}.#{lk.localization_key}"]} %>
</td>
<td class="locale_row text-center">
<%= EmojiFlag.new(locale) %>
<%= locale %>
<% if localization %>
<%= link_to lit.previous_versions_localization_key_localization_path(lk, localization, format: :js), class: "show_prev_versions #{'hidden' unless versions?(localization)}", remote: true do %>
<%= draw_icon 'random', title: I18n.t('lit.common.previous_versions', default: 'Previous versions') %>
<% end %>
<% end %>
</td>
<% unless lk.is_deleted? %>
<td class="text-center">
Expand All @@ -65,7 +72,7 @@
<% end %>
<% if Lit.store_request_info %>
<tr class="hidden request_info_row">
<td colspan="2">
<td colspan="3">
<strong>Translation key recently displayed on following pages:</strong>
<ul>
<% Lit.init.cache.get_request_info(lk.localization_key).split(' ').reverse.each do |l| %>
Expand Down
1 change: 1 addition & 0 deletions app/views/lit/localization_keys/batch_touch.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('All of search results have been marked as updated now, please retry synchronizing now');
2 changes: 1 addition & 1 deletion app/views/lit/localizations/update.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var $row = $('td.localization_row[data-id="<%= @localization.id %>"]');
$row.data('editing', 0);
$row.html("<%= ejs render(:partial=>"/lit/localization_keys/localization_row", formats: ['html'], :locals=>{:localization=>@localization.translated_value }) %>");
$row.siblings().find('.show_prev_versions').removeClass('hidden');
$row.siblings().find('.js-show_prev_versions').removeClass('hidden');
$('a.change_completed_<%= @localization.id %> input[type=checkbox]').prop("checked", true);

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
get :find_localization
get :not_translated
get :visited_again
post :batch_touch
end
resources :localizations, only: [:edit, :update, :show] do
member do
Expand Down
15 changes: 15 additions & 0 deletions test/functional/lit/localization_keys_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ class LocalizationKeysControllerTest < ActionController::TestCase
assert_not @localization_key.reload.is_deleted
end

test 'touches search results in batch' do
v = lit_localization_keys(:string)
a = lit_localization_keys(:array)
v.update_column :updated_at, 1.day.ago
a.update_column :updated_at, 1.day.ago
post :batch_touch, params: { key: 'value' }, format: :js
assert_in_delta(v.reload.updated_at, 1.second.ago, 5)
assert_in_delta(a.reload.updated_at, 1.day.ago, 5)
end

test 'wont fail when there wont be any search results when touching in batch' do
post :batch_touch, params: { key: 'valuefoobar' }, format: :js
assert_response :success
end

private

def with_fresh_cache
Expand Down