Skip to content

Commit 79adbf3

Browse files
committed
Merge pull request #72 from prograils/async_synchronization
Asynchronous loading of translations synchronized via API
2 parents 0d66e5e + 274cb8c commit 79adbf3

File tree

12 files changed

+139
-13
lines changed

12 files changed

+139
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
$ ->
2+
sourceId = $('#source_id').attr('value')
3+
4+
updateFunc = ->
5+
$.ajax "/lit/sources/" + sourceId + "/sync_complete",
6+
type: 'GET'
7+
format: 'json'
8+
success: (xml, textStatus, xhr) ->
9+
if xhr.responseJSON.sync_complete
10+
$('.loading').addClass('loaded').removeClass('loading')
11+
clearInterval(interval)
12+
location.reload()
13+
statusCode:
14+
404: ->
15+
$('.loading').text('Could not update synchronization status, please try refreshing page')
16+
401: ->
17+
$('.loading').text('You are not authorized. Please check if you are properly logged in')
18+
500: ->
19+
$('.loading').text('Something went wrong, please try synchronizing again')
20+
error: ->
21+
clearInterval(interval)
22+
23+
if $('.loading').length > 0
24+
interval = window.setInterval(updateFunc, 500)

app/assets/stylesheets/lit/application.css

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ padding: 5px 7px;
5151
.localization_row em{
5252
color: #bbb;
5353
}
54+
.loading {
55+
display: inline;
56+
}
57+
.loaded {
58+
display: none;
59+
}

app/controllers/lit/sources_controller.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ def edit
2020

2121
def synchronize
2222
@source = Source.find(params[:id])
23-
@source.synchronize
23+
@source.update_column(:sync_complete, false)
24+
if defined?(ActiveJob)
25+
SynchronizeSourceJob.perform_later(@source)
26+
else
27+
@source.synchronize
28+
end
2429
redirect_to lit.source_incomming_localizations_path(@source)
2530
end
2631

@@ -54,6 +59,11 @@ def destroy
5459
redirect_to sources_url
5560
end
5661

62+
def sync_complete
63+
@source = Source.find(params[:id])
64+
render json: { sync_complete: @source.sync_complete }
65+
end
66+
5767
private
5868

5969
def clear_params

app/helpers/lit/sources_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module Lit
22
module SourcesHelper
3+
def source_loading_class(source)
4+
source.sync_complete ? 'loaded' : 'loading'
5+
end
36
end
47
end
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Lit
2+
if defined?(::ActiveJob)
3+
class SynchronizeSourceJob < ::ActiveJob::Base
4+
queue_as :default
5+
6+
def perform(source)
7+
source.synchronize
8+
end
9+
end
10+
end
11+
end

app/models/lit/source.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ def synchronize
4848
unless il.is_duplicate?(r['value'])
4949
il.save!
5050
IncommingLocalization.where(id: il.id).
51-
update_all ['translated_value=?', r['value']]
51+
update_all(translated_value: r['value'])
5252
end
5353
end
5454
last_change = get_last_change
5555
last_change = DateTime.parse(last_change) unless last_change.nil?
5656
touch_last_updated_at(last_change)
57+
update_column(:sync_complete, true)
5758
save
5859
end
5960
end

app/views/lit/incomming_localizations/index.html.erb

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<h1>Incomming localizations</h1>
2-
<div class="row">
3-
<span class="pull-right">
4-
<%= link_to accept_all_source_incomming_localizations_path(@source), :class=>"btn btn-success btn-sm", :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
5-
<%= t('lit.common.accept_all', :default=>"Accept all") %>
6-
<% end %>
7-
<%= link_to reject_all_source_incomming_localizations_path(@source), :class=>"btn btn-danger btn-sm", :method=>:post, :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
8-
<%= t('lit.common.reject_all', :default=>"Reject all") %>
9-
<% end %>
10-
</span>
2+
<input type="hidden" id="source_id" value="<%= @source.id %>">
3+
<div class="container">
4+
<div class="row inline-headers">
5+
<h4 class="<%= source_loading_class(@source) %>">Synchronization in progress, please wait. The page will refresh automatically when done.</h2>
6+
<span class="pull-right">
7+
<%= link_to accept_all_source_incomming_localizations_path(@source), :class=>"btn btn-success btn-sm", :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
8+
<%= t('lit.common.accept_all', :default=>"Accept all") %>
9+
<% end %>
10+
<%= link_to reject_all_source_incomming_localizations_path(@source), :class=>"btn btn-danger btn-sm", :method=>:post, :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
11+
<%= t('lit.common.reject_all', :default=>"Reject all") %>
12+
<% end %>
13+
</span>
14+
</div>
1115
</div>
1216

17+
1318
<table class="table table-bordered table-striped incomming-localizations-table">
1419
<tr>
1520
<th>Current value</th>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
resources :sources do
3333
member do
3434
get :synchronize
35+
get :sync_complete
3536
put :touch
3637
end
3738
resources :incomming_localizations, :only=>[:index, :destroy] do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddSyncCompleteToLitSources < ActiveRecord::Migration
2+
def change
3+
add_column :lit_sources, :sync_complete, :boolean
4+
end
5+
end

test/functional/lit/sources_controller_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,12 @@ class SourcesControllerTest < ActionController::TestCase
6262
end
6363
assert_redirected_to sources_path
6464
end
65+
66+
test 'should indicate sync completion' do
67+
@source.update_column(:sync_complete, true)
68+
get :sync_complete, format: :json, id: @source.id
69+
body = JSON.parse(response.body)
70+
assert_equal true, body['sync_complete']
71+
end
6572
end
6673
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'test_helper'
2+
require 'fakeweb'
3+
4+
# Applicable only in an ActiveJob-enabled environment (Rails 4.2+ or 4.0/4.1
5+
# with additional gem)
6+
if defined?(ActiveJob)
7+
module Lit
8+
class SynchronizeSourceJobTest < ActiveJob::TestCase
9+
include ActiveJob::TestHelper
10+
fixtures :all
11+
12+
def setup
13+
after = 3.hours.ago
14+
after_str = after.strftime('%F %T')
15+
after_param = Rack::Utils.escape(after_str)
16+
@source = Source.first
17+
puts @source.incomming_localizations
18+
@source.update_column(:last_updated_at, after)
19+
localizations_addr = "http://testhost.com/lit/api/v1/localizations.json?after=#{after_param}"
20+
last_change_addr = 'http://testhost.com/lit/api/v1/last_change.json'
21+
FakeWeb.register_uri(:get, localizations_addr,
22+
body: Localization.all.to_json)
23+
FakeWeb.register_uri(:get, last_change_addr,
24+
body: { last_change: after_str }.to_json)
25+
end
26+
27+
def do_job
28+
SynchronizeSourceJob.perform_later(@source)
29+
end
30+
31+
test 'performs synchronization' do
32+
assert_performed_jobs(1) do
33+
assert_equal 0, @source.incomming_localizations.count
34+
do_job
35+
assert_equal 2, @source.incomming_localizations.count
36+
end
37+
end
38+
end
39+
end
40+
end
+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
require 'test_helper'
22

3-
module Lit
4-
class SourcesHelperTest < ActionView::TestCase
3+
class SourcesHelperTest < ActionView::TestCase
4+
include Lit::SourcesHelper
5+
6+
def setup
7+
@source = Lit::Source.new
8+
end
9+
10+
test 'returns "loaded" when source sync is complete' do
11+
@source.sync_complete = true
12+
assert_equal source_loading_class(@source), 'loaded'
13+
end
14+
15+
test 'returns "loading" when source sync is not complete' do
16+
@source.sync_complete = false
17+
assert_equal source_loading_class(@source), 'loading'
518
end
619
end

0 commit comments

Comments
 (0)