inline_translation 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/test/babbel_integration_test.rb +29 -0
- data/test/fixtures/application_controller.rb +3 -0
- data/test/fixtures/rails.rb +36 -0
- data/test/lib/babbel_test.rb +9 -0
- data/test/lib/concerns/acts_as_translatable_test.rb +62 -0
- data/test/lib/concerns/translatable_test.rb +31 -0
- data/test/lib/controllers/translations_controller_test.rb +56 -0
- data/test/lib/generators/babbel_generator_install_test.rb +17 -0
- data/test/lib/helpers/translations_helper_test.rb +61 -0
- data/test/lib/models/translation_test.rb +55 -0
- data/test/lib/services/translation_service_test.rb +81 -0
- data/test/lib/translators/base_test.rb +65 -0
- data/test/lib/translators/bing_test.rb +39 -0
- data/test/lib/translators/null_test.rb +20 -0
- data/test/test_helper.rb +74 -0
- data/test/test_types/controller_test.rb +6 -0
- data/test/test_types/integration_test.rb +2 -0
- data/test/test_types/unit_test.rb +3 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 53cc9b45fed24636013c2424b96e2660bc225e03
|
4
|
+
data.tar.gz: 281e7519500a96ca6d8d45574c9b1c65b3e85da0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 031e8bed2799e0e519dc759a468bfe016aae4680cda3648d5ebe69ad46b96fbfeb08eafb25bacab3489748b432b33043cce51626b4eda68aebfcc6eb6e062db1
|
7
|
+
data.tar.gz: b34f21a4917721af118d9a3eb924094957b48682a888f819646bd7e7313e9e94e10a9125bca2d67ab50d0a88e893a3157fffb15c125a343aee4c9e09b4b29878
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
class InlineTranslationIntegrationTest < IntegrationTest
|
3
|
+
describe InlineTranslation do
|
4
|
+
setup_model :integration_model
|
5
|
+
|
6
|
+
let(:model) { IntegrationModel.create! column1: "column one", column2: "column2", language: :en }
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@controller ||= InlineTranslation::Controllers::TranslationsController.new
|
10
|
+
IntegrationModel.acts_as_translatable on: [:column1, :column2]
|
11
|
+
InlineTranslation::Translators::Null.stubs(:ready?).returns(true)
|
12
|
+
InlineTranslation::Translators::Null.any_instance.stubs(:translate).returns("this is a translation", "this is another translation")
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "creating translations" do
|
16
|
+
it "can create translations" do
|
17
|
+
post :create, translatable_type: "IntegrationModel", translatable_id: model.id, to: :fr
|
18
|
+
|
19
|
+
created = Translation.where(translatable_type: "IntegrationModel")
|
20
|
+
|
21
|
+
assert_equal created.where(translatable_id: model.id).size, 2
|
22
|
+
assert_equal created.where(language: :fr).size, 2
|
23
|
+
assert_equal created.where(translatable_type: "IntegrationModel").size, 2
|
24
|
+
assert_equal created.where(translation: "this is a translation").size, 1
|
25
|
+
assert_equal created.where(translation: "this is another translation").size, 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Rails
|
2
|
+
def self.application
|
3
|
+
OpenStruct.new routes: routes,
|
4
|
+
env_config: {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.root
|
8
|
+
Dir['../../../']
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.env
|
12
|
+
OpenStruct.new to_s: "test",
|
13
|
+
development?: false,
|
14
|
+
test?: true,
|
15
|
+
production?: false
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.backtrace_cleaner
|
19
|
+
ActiveSupport::BacktraceCleaner.new
|
20
|
+
end
|
21
|
+
|
22
|
+
class Engine
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.routes
|
28
|
+
@routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
|
29
|
+
routes.draw { resources :translations }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.draw_routes
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActsAsTranslatableTest < UnitTest
|
4
|
+
describe InlineTranslation::Concerns::ActsAsTranslatable do
|
5
|
+
|
6
|
+
before do
|
7
|
+
setup_model :concern_model
|
8
|
+
ConcernModel.define_singleton_method(:find_alt) { |id| "found #{id}!" }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "includes Translatable" do
|
12
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
13
|
+
assert ConcernModel.included_modules.include?(InlineTranslation::Concerns::Translatable)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "defines a translatable_fields class method" do
|
17
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
18
|
+
assert_equal ConcernModel.translatable_fields, [:column1, :column2]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defines a single translatable_field correctly" do
|
22
|
+
ConcernModel.class_eval "acts_as_translatable on: :column1"
|
23
|
+
assert_equal ConcernModel.translatable_fields, [:column1]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "defines a custom get_instance class method" do
|
27
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], load_via: :find_alt"
|
28
|
+
assert_equal ConcernModel.get_instance(42), ConcernModel.find_alt(42)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "defines a get_instance class method as :find by default" do
|
32
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
33
|
+
model = ConcernModel.create
|
34
|
+
assert_equal ConcernModel.get_instance(model.id), ConcernModel.find(model.id)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "defines an id_field method as :id by default" do
|
38
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
39
|
+
model = ConcernModel.new id: 42
|
40
|
+
assert_equal model.id_field, model.id
|
41
|
+
end
|
42
|
+
|
43
|
+
it "defines a custom id_field method" do
|
44
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], id_field: :id_alt"
|
45
|
+
model = ConcernModel.new id_alt: 42
|
46
|
+
assert_equal model.id_field, model.id_alt
|
47
|
+
end
|
48
|
+
|
49
|
+
it "defines a language_field method as :language by default" do
|
50
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
51
|
+
model = ConcernModel.new language: :en
|
52
|
+
assert_equal model.language_field, model.language
|
53
|
+
end
|
54
|
+
|
55
|
+
it "defines a custom language_field method" do
|
56
|
+
ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], language_field: :language_alt"
|
57
|
+
model = ConcernModel.new language_alt: :en
|
58
|
+
assert_equal model.language_field, model.language_alt
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TranslatableTest < UnitTest
|
4
|
+
describe InlineTranslation::Concerns::Translatable do
|
5
|
+
|
6
|
+
let(:model) { ConcernModel.create column1: "test text" }
|
7
|
+
|
8
|
+
before do
|
9
|
+
setup_model :concern_model
|
10
|
+
include_translatable ConcernModel
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has_many translations" do
|
14
|
+
assert_respond_to model, :translations
|
15
|
+
assert_instance_of InlineTranslation::Models::Translation, model.translations.build
|
16
|
+
end
|
17
|
+
|
18
|
+
it "destroys translations after update" do
|
19
|
+
model.translations.build language: :en, field: :column1, translation: "test translation"
|
20
|
+
model.save
|
21
|
+
assert_equal model.reload.translations.size, 1
|
22
|
+
|
23
|
+
model.update! column1: "changed text"
|
24
|
+
model.save
|
25
|
+
|
26
|
+
assert_equal model.reload.translations.size, 0
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TranslationsControllerTest < ControllerTest
|
4
|
+
describe InlineTranslation::Controllers::TranslationsController do
|
5
|
+
setup_model :controller_model
|
6
|
+
let(:service) { InlineTranslation::Services::TranslationService.new }
|
7
|
+
let(:translatable) { ControllerModel.create }
|
8
|
+
let(:translation_result) { { column1: 'A translation!', column2: 'A translation!' } }
|
9
|
+
|
10
|
+
setup do
|
11
|
+
ControllerModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
12
|
+
@controller ||= InlineTranslation::Controllers::TranslationsController.new
|
13
|
+
translatable
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "POST create" do
|
17
|
+
it "returns the translation for successful translation for JS" do
|
18
|
+
InlineTranslation.stubs(:ready?).returns(true)
|
19
|
+
InlineTranslation::Translators::Null.any_instance.stubs(:can_translate?).returns(true)
|
20
|
+
InlineTranslation::Translators::Null.any_instance.stubs(:translate).returns("A translation!")
|
21
|
+
post :create, translatable_type: "ControllerModel", translatable_id: translatable.id, format: :js
|
22
|
+
|
23
|
+
assert_equal response.status, 200
|
24
|
+
json = JSON.parse(response.body)
|
25
|
+
|
26
|
+
assert_equal json['translations']['column1'], translation_result[:column1]
|
27
|
+
assert_equal json['translations']['column2'], translation_result[:column2]
|
28
|
+
assert_equal json['translatable_id'], translatable.id.to_s
|
29
|
+
assert_equal json['translatable_type'], 'ControllerModel'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns the translation for successful translation for JSON" do
|
33
|
+
InlineTranslation.stubs(:ready?).returns(true)
|
34
|
+
InlineTranslation::Translators::Null.any_instance.stubs(:can_translate?).returns(true)
|
35
|
+
InlineTranslation::Translators::Null.any_instance.stubs(:translate).returns("A translation!")
|
36
|
+
post :create, translatable_type: "ControllerModel", translatable_id: translatable.id, format: :json
|
37
|
+
|
38
|
+
assert_equal response.status, 200
|
39
|
+
assert_template 'translations/create'
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
it "returns unprocessable entity for unsuccessful translation" do
|
44
|
+
InlineTranslation::Services::TranslationService.any_instance.stubs(:translate).returns(false)
|
45
|
+
post :create, translatable_type: "ControllerModel", translatable_id: translatable.id
|
46
|
+
assert_equal response.status, 422
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns unprocessable entity when translatable_type is not defined" do
|
50
|
+
InlineTranslation::Services::TranslationService.any_instance.stubs(:translate).returns(false)
|
51
|
+
post :create
|
52
|
+
assert_equal response.status, 422
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/inline_translation/install/install_generator'
|
3
|
+
|
4
|
+
class InlineTranslationGeneratorInstallTest < Rails::Generators::TestCase
|
5
|
+
tests InlineTranslation::InstallGenerator
|
6
|
+
setup_destination
|
7
|
+
|
8
|
+
test "generates a migration file" do
|
9
|
+
run_generator
|
10
|
+
assert_migration "db/migrate/add_inline_translations.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
test "generates an initializer file" do
|
14
|
+
run_generator
|
15
|
+
assert_file "config/initializers/inline_translation.rb"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TranslationsHelperTest < UnitTest
|
4
|
+
include InlineTranslation::Helpers::TranslationsHelper
|
5
|
+
include Rails.application.routes.url_helpers
|
6
|
+
|
7
|
+
setup_model :helper_model
|
8
|
+
|
9
|
+
let(:model) { HelperModel.new column1: 'a value', language: :en }
|
10
|
+
|
11
|
+
before do
|
12
|
+
I18n.locale = :fr
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ".translate_link_for" do
|
16
|
+
it "returns a link with another locale set" do
|
17
|
+
assert_match /\?to=fr/, translate_link_for(model)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns a link with a specified locale" do
|
21
|
+
I18n.locale = :en
|
22
|
+
assert_match /\?to=fr/, translate_link_for(model, to: :fr)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "defaults to 'Translate' for link text" do
|
26
|
+
assert_match /Translate/, translate_link_for(model)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "accepts a text parameter" do
|
30
|
+
assert_match /Other text/, translate_link_for(model, text: 'Other text')
|
31
|
+
end
|
32
|
+
|
33
|
+
it "does not return a link when locale is the same" do
|
34
|
+
I18n.locale = :en
|
35
|
+
refute translate_link_for(model)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not return a link when specified locale is the same" do
|
39
|
+
refute translate_link_for(model, to: :en)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".translated_element_for" do
|
44
|
+
it "returns an span with a language class" do
|
45
|
+
assert_match /to-fr/, translated_element_for(model, :column1)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns a span with a field class" do
|
49
|
+
assert_match /column1-translated/, translated_element_for(model, :column1)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns a span with a InlineTranslation class" do
|
53
|
+
assert_match /InlineTranslation-translated/, translated_element_for(model, :column1)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "accepts an element parameter" do
|
57
|
+
assert_match /<div/, translated_element_for(model, :column1, element: :div)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'inline_translation/models/translation'
|
3
|
+
|
4
|
+
class TranslationTest < UnitTest
|
5
|
+
describe InlineTranslation::Models::Translation do
|
6
|
+
|
7
|
+
setup_model :model_class
|
8
|
+
let(:translation) { InlineTranslation::Models::Translation.new translatable: ModelClass.new,
|
9
|
+
language: :en,
|
10
|
+
field: :field_name,
|
11
|
+
translation: :translation }
|
12
|
+
|
13
|
+
it "should be valid when all fields are present" do
|
14
|
+
assert translation.valid?
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have a translatable" do
|
18
|
+
assert_respond_to translation, :translatable
|
19
|
+
assert_instance_of ModelClass, translation.translatable
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should require a translatable" do
|
23
|
+
translation.translatable = nil
|
24
|
+
refute translation.valid?
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have a language" do
|
28
|
+
assert_equal translation.language, :en
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should require a language" do
|
32
|
+
translation.language = nil
|
33
|
+
refute translation.valid?
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have a field name" do
|
37
|
+
assert_equal translation.field, :field_name
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should require a field name" do
|
41
|
+
translation.field = nil
|
42
|
+
refute translation.valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have a translation" do
|
46
|
+
assert_equal translation.translation, :translation
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should require a translation" do
|
50
|
+
translation.translation = nil
|
51
|
+
refute translation.valid?
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'inline_translation/services/translation_service'
|
3
|
+
require 'inline_translation/models/translation'
|
4
|
+
|
5
|
+
class InlineTranslationTranslationServiceTest < UnitTest
|
6
|
+
setup_model :service_model
|
7
|
+
setup_translation
|
8
|
+
|
9
|
+
let(:translator_class) { InlineTranslation::Translators::Base }
|
10
|
+
let(:service) { InlineTranslation::Services::TranslationService.new(translator_class) }
|
11
|
+
let(:translatable) { ServiceModel.new column1: "translatable text", column2: "more text", language: :en }
|
12
|
+
|
13
|
+
before do
|
14
|
+
ServiceModel.class_eval "acts_as_translatable on: [:column1, :column2]"
|
15
|
+
include_translatable ServiceModel
|
16
|
+
translator_class.stubs(:ready?).returns(true)
|
17
|
+
service.translator.stubs(:can_translate?).returns(true)
|
18
|
+
service.translator.stubs(:translate).returns("translation")
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "initialize" do
|
22
|
+
it "sets a translator object on initialize" do
|
23
|
+
assert_equal service.translator.class, translator_class
|
24
|
+
end
|
25
|
+
it "raises an error on initialize if translator is not ready" do
|
26
|
+
translator_class.stubs(:ready?).returns(false)
|
27
|
+
->{ InlineTranslation::Services::TranslationService.new(translator_class) }.must_raise InlineTranslation::Services::InvalidTranslatorError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "invalid translator error" do
|
32
|
+
it "has an error message" do
|
33
|
+
assert_match /Unable to instantiate translator/, InlineTranslation::Services::InvalidTranslatorError.new.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "translations_for" do
|
38
|
+
it "returns the translations for a given translatable" do
|
39
|
+
translatable.save
|
40
|
+
translatable.translations.create field: :column1, translation: 'Bon oui!', language: :fr
|
41
|
+
translatable.translations.create field: :column2, translation: 'Mon ser!', language: :fr
|
42
|
+
result = service.translations_for(translatable, to: :fr)
|
43
|
+
translatable.translations.each do |translation|
|
44
|
+
assert_equal result[translation.field], translation.translation
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "translate" do
|
50
|
+
it "returns the results of translate!" do
|
51
|
+
service.stubs(:translate!).returns("translation result")
|
52
|
+
assert_equal service.translate(translatable), "translation result"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "translate!" do
|
57
|
+
it "builds all translations using translate_field" do
|
58
|
+
service.translate!(translatable)
|
59
|
+
assert_equal translatable.translations.size, 2
|
60
|
+
end
|
61
|
+
|
62
|
+
it "does not build an invalid translation" do
|
63
|
+
service.translator.stubs(:can_translate?).returns(false)
|
64
|
+
service.translate!(translatable)
|
65
|
+
assert_equal translatable.translations.size, 0
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "translate_field" do
|
70
|
+
it "builds a translation with translate_field" do
|
71
|
+
service.translate_field(translatable, :column1)
|
72
|
+
assert_equal translatable.translations.size, 1
|
73
|
+
end
|
74
|
+
it "does not build an invalid translation" do
|
75
|
+
service.translator.stubs(:can_translate?).returns(false)
|
76
|
+
service.translate_field(translatable, :column1)
|
77
|
+
assert_equal translatable.translations.size, 0
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'inline_translation/translators/base'
|
3
|
+
|
4
|
+
class BaseTest < UnitTest
|
5
|
+
describe InlineTranslation::Translators::Base do
|
6
|
+
setup_model :translator_model
|
7
|
+
|
8
|
+
let(:translator_class) { InlineTranslation::Translators::Base }
|
9
|
+
let(:translator) { translator_class.new }
|
10
|
+
let(:translatable) { TranslatorModel.new column1: "column one", language: :en }
|
11
|
+
|
12
|
+
setup do
|
13
|
+
TranslatorModel.acts_as_translatable on: :column1
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns false on ready?" do
|
17
|
+
refute translator_class.ready?
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".can_translate?" do
|
21
|
+
|
22
|
+
it "returns false if the translator is not ready" do
|
23
|
+
refute translator.can_translate? translatable, :column1, :fr
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns false if 'to' is not set" do
|
27
|
+
translator.stubs(:ready?).returns(:true)
|
28
|
+
refute translator.can_translate? translatable, :column1, nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns false if translatable's language is not set" do
|
32
|
+
translator.stubs(:ready?).returns(:true)
|
33
|
+
translatable.language = nil
|
34
|
+
refute translator.can_translate? translatable, :column1, :fr
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns false if the from and to language are the same" do
|
38
|
+
translator.stubs(:ready?).returns(:true)
|
39
|
+
refute translator.can_translate? translatable, :column1, :en
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns false if a translation already exists" do
|
43
|
+
translator.stubs(:ready?).returns(:true)
|
44
|
+
translatable.save
|
45
|
+
translatable.translations.create field: :column1, language: :fr
|
46
|
+
refute translator.can_translate? translatable, :column1, :fr
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns false if the field is not found on translatable" do
|
50
|
+
translator_class.stubs(:ready?).returns(:true)
|
51
|
+
refute translator.can_translate? translatable, :notacolumn, :fr
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns true otherwise" do
|
55
|
+
translator_class.stubs(:ready?).returns(:true)
|
56
|
+
assert translator.can_translate? translatable, :column1, :fr
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "raises an error on translate" do
|
61
|
+
assert_raises NotImplementedError do translator.translate(nil) end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'inline_translation/translators/base'
|
3
|
+
require 'inline_translation/translators/bing'
|
4
|
+
require 'bing_translator'
|
5
|
+
|
6
|
+
class BingTest < UnitTest
|
7
|
+
describe InlineTranslation::Translators::Bing do
|
8
|
+
|
9
|
+
let(:translator_class) { InlineTranslation:: Translators::Bing }
|
10
|
+
let(:translator) { translator_class.new }
|
11
|
+
|
12
|
+
before do
|
13
|
+
setup_bing_translator_env
|
14
|
+
end
|
15
|
+
|
16
|
+
it "initializes a BingTranslator" do
|
17
|
+
assert_instance_of BingTranslator, translator.translator
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns ready if ENV variables are set" do
|
21
|
+
assert translator_class.ready?
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns not ready if app id is not set" do
|
25
|
+
ENV['BING_TRANSLATOR_APP_ID'] = nil
|
26
|
+
refute translator_class.ready?
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns not ready if secret is not set" do
|
30
|
+
ENV['BING_TRANSLATOR_SECRET'] = nil
|
31
|
+
refute translator_class.ready?
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can translate a translatable" do
|
35
|
+
translator.translator.stubs(:translate).returns("translation")
|
36
|
+
assert_equal translator.translate("original"), "translation"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'inline_translation/translators/base'
|
3
|
+
require 'inline_translation/translators/null'
|
4
|
+
|
5
|
+
class NullTest < UnitTest
|
6
|
+
describe InlineTranslation::Translators::Null do
|
7
|
+
|
8
|
+
let(:translator_class) { InlineTranslation::Translators::Null }
|
9
|
+
let(:translator) { translator_class.new }
|
10
|
+
|
11
|
+
it "returns ready as true" do
|
12
|
+
assert translator_class.ready?
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns nil as a translation" do
|
16
|
+
assert_nil translator.translate("anything")
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require "codeclimate-test-reporter"
|
4
|
+
CodeClimate::TestReporter.start
|
5
|
+
|
6
|
+
require 'byebug'
|
7
|
+
require 'bundler/setup'
|
8
|
+
require 'active_record'
|
9
|
+
require 'temping'
|
10
|
+
ActiveRecord::Base.establish_connection adapter: :sqlite3, database: ':memory:'
|
11
|
+
|
12
|
+
require 'action_controller'
|
13
|
+
|
14
|
+
require 'fixtures/rails'
|
15
|
+
require 'fixtures/application_controller'
|
16
|
+
|
17
|
+
require 'minitest/autorun'
|
18
|
+
require 'minitest/pride'
|
19
|
+
require 'mocha/mini_test'
|
20
|
+
require 'rails/test_help'
|
21
|
+
|
22
|
+
require 'test_types/unit_test'
|
23
|
+
require 'test_types/controller_test'
|
24
|
+
require 'test_types/integration_test'
|
25
|
+
|
26
|
+
require 'inline_translation'
|
27
|
+
|
28
|
+
I18n.enforce_available_locales = false
|
29
|
+
|
30
|
+
def setup_destination
|
31
|
+
destination File.expand_path '../../../tmp', __FILE__
|
32
|
+
setup :prepare_destination
|
33
|
+
end
|
34
|
+
|
35
|
+
def setup_model(model = :test_model)
|
36
|
+
constantized = model.to_s.split("_").collect(&:capitalize).join
|
37
|
+
unless Object.const_defined?(constantized)
|
38
|
+
Temping.create model do
|
39
|
+
with_columns do |t|
|
40
|
+
t.integer :id_alt
|
41
|
+
t.string :column1, :column2, :language, :language_alt
|
42
|
+
end
|
43
|
+
end
|
44
|
+
include_acts_as_translatable Object.const_get(constantized)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def setup_translation
|
49
|
+
unless Object.const_defined?("Translation")
|
50
|
+
Temping.create :translation do
|
51
|
+
with_columns do |t|
|
52
|
+
t.integer :translatable_id
|
53
|
+
t.string :translatable_type
|
54
|
+
t.string :field
|
55
|
+
t.string :language
|
56
|
+
t.text :translation
|
57
|
+
t.timestamps
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def setup_bing_translator_env
|
64
|
+
ENV['BING_TRANSLATOR_APP_ID'] = 'set'
|
65
|
+
ENV['BING_TRANSLATOR_SECRET'] = 'set'
|
66
|
+
end
|
67
|
+
|
68
|
+
def include_acts_as_translatable(model)
|
69
|
+
model.class_eval "include InlineTranslation::Concerns::ActsAsTranslatable"
|
70
|
+
end
|
71
|
+
|
72
|
+
def include_translatable(model)
|
73
|
+
model.class_eval "include InlineTranslation::Concerns::Translatable"
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inline_translation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Kiesel (gdpelican)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bing_translator
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Sets up a framework for allowing inline translation of database content
|
70
|
+
email:
|
71
|
+
- james.kiesel@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- test/babbel_integration_test.rb
|
77
|
+
- test/fixtures/application_controller.rb
|
78
|
+
- test/fixtures/rails.rb
|
79
|
+
- test/lib/babbel_test.rb
|
80
|
+
- test/lib/concerns/acts_as_translatable_test.rb
|
81
|
+
- test/lib/concerns/translatable_test.rb
|
82
|
+
- test/lib/controllers/translations_controller_test.rb
|
83
|
+
- test/lib/generators/babbel_generator_install_test.rb
|
84
|
+
- test/lib/helpers/translations_helper_test.rb
|
85
|
+
- test/lib/models/translation_test.rb
|
86
|
+
- test/lib/services/translation_service_test.rb
|
87
|
+
- test/lib/translators/base_test.rb
|
88
|
+
- test/lib/translators/bing_test.rb
|
89
|
+
- test/lib/translators/null_test.rb
|
90
|
+
- test/test_helper.rb
|
91
|
+
- test/test_types/controller_test.rb
|
92
|
+
- test/test_types/integration_test.rb
|
93
|
+
- test/test_types/unit_test.rb
|
94
|
+
homepage: http://www.github.com/gdpelican/InlineTranslation
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- app
|
102
|
+
- lib
|
103
|
+
- test
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.4.6
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Store on-the-fly translations using Bing (or others!)
|
120
|
+
test_files:
|
121
|
+
- test/babbel_integration_test.rb
|
122
|
+
- test/fixtures/application_controller.rb
|
123
|
+
- test/fixtures/rails.rb
|
124
|
+
- test/lib/babbel_test.rb
|
125
|
+
- test/lib/concerns/acts_as_translatable_test.rb
|
126
|
+
- test/lib/concerns/translatable_test.rb
|
127
|
+
- test/lib/controllers/translations_controller_test.rb
|
128
|
+
- test/lib/generators/babbel_generator_install_test.rb
|
129
|
+
- test/lib/helpers/translations_helper_test.rb
|
130
|
+
- test/lib/models/translation_test.rb
|
131
|
+
- test/lib/services/translation_service_test.rb
|
132
|
+
- test/lib/translators/base_test.rb
|
133
|
+
- test/lib/translators/bing_test.rb
|
134
|
+
- test/lib/translators/null_test.rb
|
135
|
+
- test/test_helper.rb
|
136
|
+
- test/test_types/controller_test.rb
|
137
|
+
- test/test_types/integration_test.rb
|
138
|
+
- test/test_types/unit_test.rb
|