awesome_translations 0.0.58 → 0.0.62

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -1
  3. data/app/controllers/awesome_translations/clean_ups_controller.rb +1 -1
  4. data/app/controllers/awesome_translations/groups_controller.rb +1 -0
  5. data/app/controllers/awesome_translations/handlers_controller.rb +1 -0
  6. data/app/helpers/awesome_translations/application_helper.rb +0 -1
  7. data/app/helpers/awesome_translations/views_helper.rb +2 -2
  8. data/app/models/awesome_translations/group.rb +2 -2
  9. data/app/models/awesome_translations/translated_value.rb +6 -3
  10. data/app/models/awesome_translations/translation.rb +9 -4
  11. data/app/views/awesome_translations/duplicates/index.html.erb +2 -2
  12. data/app/views/awesome_translations/groups/show.html.erb +9 -4
  13. data/app/views/awesome_translations/handler_translations/index.html.erb +21 -6
  14. data/app/views/awesome_translations/handlers/show.html.erb +29 -6
  15. data/app/views/awesome_translations/movals/index.html.erb +2 -2
  16. data/lib/awesome_translations/cache_database_generator/group.rb +10 -3
  17. data/lib/awesome_translations/cache_database_generator/handler.rb +9 -2
  18. data/lib/awesome_translations/cache_database_generator/handler_translation.rb +13 -3
  19. data/lib/awesome_translations/cache_database_generator/translation_key.rb +4 -4
  20. data/lib/awesome_translations/cache_database_generator/translation_value.rb +3 -1
  21. data/lib/awesome_translations/cache_database_generator.rb +2 -2
  22. data/lib/awesome_translations/config.rb +3 -3
  23. data/lib/awesome_translations/controller_translate_functionality.rb +2 -2
  24. data/lib/awesome_translations/engine.rb +0 -4
  25. data/lib/awesome_translations/erb_inspector/file_inspector.rb +4 -4
  26. data/lib/awesome_translations/erb_inspector/translation_inspector.rb +8 -8
  27. data/lib/awesome_translations/erb_inspector.rb +1 -0
  28. data/lib/awesome_translations/global_translator.rb +24 -38
  29. data/lib/awesome_translations/handlers/file_handler.rb +1 -0
  30. data/lib/awesome_translations/handlers/model_handler.rb +1 -1
  31. data/lib/awesome_translations/handlers/validations_handler.rb +4 -4
  32. data/lib/awesome_translations/handlers.rb +1 -0
  33. data/lib/awesome_translations/model_inspector.rb +18 -18
  34. data/lib/awesome_translations/object_extensions.rb +2 -2
  35. data/lib/awesome_translations/translate_functionality.rb +2 -2
  36. data/lib/awesome_translations/translation_migrator.rb +3 -3
  37. data/lib/awesome_translations/version.rb +1 -1
  38. data/lib/awesome_translations.rb +1 -1
  39. data/lib/tasks/awesome_translations_tasks.rake +1 -4
  40. data/spec/controllers/groups_controller_spec.rb +5 -5
  41. data/spec/controllers/handlers_controller_spec.rb +4 -4
  42. data/spec/controllers/users_controller_spec.rb +4 -2
  43. data/spec/dummy/Rakefile +1 -1
  44. data/spec/dummy/app/assets/config/manifest.js +3 -0
  45. data/spec/dummy/app/controllers/application_controller.rb +3 -7
  46. data/spec/dummy/app/controllers/users_controller.rb +1 -1
  47. data/spec/dummy/app/mailers/my_mailer.rb +1 -1
  48. data/spec/dummy/app/models/application_record.rb +3 -0
  49. data/spec/dummy/app/models/role.rb +2 -5
  50. data/spec/dummy/app/models/user.rb +1 -1
  51. data/spec/dummy/app/services/application_service.rb +3 -0
  52. data/spec/dummy/app/services/test_translate_service.rb +9 -0
  53. data/spec/dummy/bin/bundle +1 -1
  54. data/spec/dummy/bin/rails +1 -1
  55. data/spec/dummy/config/application.rb +7 -2
  56. data/spec/dummy/config/boot.rb +2 -2
  57. data/spec/dummy/config/environment.rb +1 -1
  58. data/spec/dummy/config.ru +1 -1
  59. data/spec/dummy/db/awesome_translations.sqlite3 +0 -0
  60. data/spec/dummy/db/development.sqlite3 +0 -0
  61. data/spec/dummy/db/test.sqlite3 +0 -0
  62. data/spec/dummy/log/development.log +52 -0
  63. data/spec/dummy/log/test.log +1188 -0
  64. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/2m/2mnPY9uUnBNkGHLxtFaWv5hB3oA7sY2K1ZtwQlZLnAY.cache +2 -0
  65. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/3N/3NnKgR2TsTI1qCXKZpts0uJzp2dRJcoYp3nnFU6YE4o.cache +2 -0
  66. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/5r/5rpKlqLSFsRCibev7g6MQkIOhWuOah-1KAN3aqV_fYI.cache +0 -0
  67. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/6E/6ExTs_VkmUe-fovt99Kw8rq-xCfx0_qGsSoldSWScZI.cache +1 -0
  68. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/8O/8OrG8phjC9s5-_7GAI2TEmRjsrGsuvq1gnJQPdMm8v8.cache +0 -0
  69. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/8f/8f4REKYOWkNfHexyAVqGXnbOP3vky_ANapWzlRMDeQ4.cache +3 -0
  70. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/8n/8n_viDw_qMQaQmqHM0ObPADed5uRyEYo3gDZIiTqUyA.cache +1 -0
  71. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/9e/9euY4B26kaHeiqfd1Qc0bsfRjpzmq36X6uqUfaRNtIs.cache +1 -0
  72. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/AB/AB5PxrhEqi8qOm7AmSf-sHDs9ffj_iTsytaHas2ryPA.cache +1 -0
  73. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/AI/AICJO1oM9dzlQ_N1QlHcCN9e8PC0wjtYk7-VmuNdfwg.cache +0 -0
  74. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Ca/CaGQBiCuFgkHQ5gw8ZnGQqO8iP1md3GcllDsmaKBFdQ.cache +2 -0
  75. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Ch/ChWez9UVXMuuBpLJMDRGuDmMuXy8gIuFo6794-wBgXc.cache +1 -0
  76. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/D9/D9eOwgl8pLlC7LJu-LL59UeHm9d-CH1mPgsA8j6xkMg.cache +1 -0
  77. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Do/Do83AoOat5W-c1g7piDGy2GMmffsY6JY0Qfuh5PY3GM.cache +2 -0
  78. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Hn/Hn53895nYKzu-g1FJKYS_yszYqCADXasTmN615R_qMQ.cache +1 -0
  79. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/It/ItV7jxV0bfHndR-H3cidDwpTFGhvs0ETLRqCTmZMXB8.cache +1 -0
  80. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/LL/LL0EenAcTXP-UNCqfcKtMF4Z8oDeWvdlwLQfuOVRPdw.cache +0 -0
  81. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Mj/MjwYGG9GkHD1fwwH2bg66EBYl3zCA_mqeDF9otey4-U.cache +3 -0
  82. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/NU/NUN9oO9gyoo-sY32AOLHdDACjLIxisCxXdKd84ou6cs.cache +2 -0
  83. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Ob/ObuKkBd-camcT52YpgeuyheU-GDFLZCJnoVtq2plgW0.cache +0 -0
  84. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Og/Og_HPfE6734sqkQ0_h4EkOOx3Aoq1LABLH9ovjl6Bvw.cache +0 -0
  85. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Pw/Pwuhz_6So_JcSrxT0Uf4VaLhh_FFSJMKxLZFMbS0LNQ.cache +0 -0
  86. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/R-/R-nohuDiORrLlJJtKc5bQsjIRS8yTQFVR3EIxJx3JZw.cache +1 -0
  87. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/SX/SXA6qKBlsAQ-MLPi7-KPCPcSx9Gg2VR1xa-lCmgipgA.cache +1 -0
  88. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Sb/Sb1Vug319YfPL2Rci1PLMLXJjeIFYcNbHb5hKOvO1eo.cache +2 -0
  89. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/_f/_f0DjbdXTMEPb52THCCF5qbKkDs28tHHnap5ag4jsdU.cache +0 -0
  90. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/dT/dTb1O_9BwFdhA3VT1UTxY-XIIoAGDHGHVO9bzRUNIfc.cache +0 -0
  91. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/dV/dVTKOfd8qDIHT3VD-4emnl0yAIxcSF1pY0CUESPHwdM.cache +0 -0
  92. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/i0/i0TQIUsntlYrsibtn9YfB3qbI6bj8hLPom8KUfxR078.cache +1 -0
  93. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/jF/jFFXBsyFdv_TiNykX219AHs3Q7dQInYRP_80cAWZsgE.cache +0 -0
  94. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/mX/mXfx3VRudxpXrRx1aejtNA5YHOwND3kwWUfvdXHSors.cache +2 -0
  95. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/nG/nGQXdOLWsP4c926DJJBNXFUE0cKfZynnqN5aQ4T1I48.cache +1 -0
  96. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/oX/oXeqOS7RZb4eFbKrraeQtr6TUrXPTueLN_Crp7S0RLE.cache +1 -0
  97. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/p2/p27Ube4R6tciXRc0F8lpz6wUzmXIPxwqz7dKaUYVVuc.cache +1 -0
  98. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/pv/pv6tV3CDkIAmLXdU8EPLlNEkXAKJPufVD4VP30o4fWo.cache +2 -0
  99. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/q6/q6BYa32YJF11eGVapO4ouNl6gayPIsARgMavlzZmoi0.cache +2 -0
  100. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/r5/r5draRYO72lGMZZKpqYbscAWxAawJVG7uO0uyx3iw24.cache +1 -0
  101. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/r7/r7kGQJTsdrqs3i-prYAVnngsoc6uAtrjLTULTJdgy9g.cache +1 -0
  102. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/rI/rITk_65QtUwq61bjRPAksGeJxbLUFfz-sFPnQiZPHjw.cache +3 -0
  103. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/rR/rRS9OkVGJ9Wvs1fwZH-raqD4sXkbV8WmlLGIibYpZmI.cache +0 -0
  104. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/u9/u9nsXMBU9hA4UnH4SDHgk19ev_JNeeiS1-EZDrgStCI.cache +1 -0
  105. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/un/unZXcRqhGJwHvVWJmJ1rTizBZq6H5jF28E5-FHyTYL4.cache +1 -0
  106. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/wP/wP-1NwhQC0Sp_5PHpwAI7N0_N99vLNy0TA2FcMd6zgE.cache +0 -0
  107. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/x7/x7KkTV3ibfIEysLB_ug5bfmnn2VLV_BldukPR3EoPBk.cache +2 -0
  108. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/xr/xrDdkyCzfXr-pIE8gfvD8iFiWPuq9EwuZIl-uU9s0_o.cache +1 -0
  109. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/yc/yc3EXb1cvVLmwnk8aQN89bgVFeuwsVz2k2p0mvC2c3c.cache +1 -0
  110. data/spec/factories/group.rb +2 -2
  111. data/spec/factories/handler.rb +4 -4
  112. data/spec/factories/handler_translation.rb +5 -5
  113. data/spec/factories/translation_key.rb +1 -1
  114. data/spec/factories/translation_value.rb +3 -3
  115. data/spec/features/clean_ups_spec.rb +2 -5
  116. data/spec/features/duplicates_spec.rb +5 -7
  117. data/spec/features/movals_spec.rb +8 -10
  118. data/spec/handlers/file_handler_spec.rb +8 -8
  119. data/spec/handlers/model_handler_spec.rb +4 -4
  120. data/spec/handlers/validations_handler_spec.rb +3 -3
  121. data/spec/handlers_spec.rb +1 -1
  122. data/spec/lib/cache_database_generator_spec.rb +4 -4
  123. data/spec/lib/erb_inspector/file_inspector_spec.rb +2 -2
  124. data/spec/lib/erb_inspector/translation_inspector_spec.rb +2 -2
  125. data/spec/lib/erb_inspector_spec.rb +5 -5
  126. data/spec/lib/model_inspector_spec.rb +4 -4
  127. data/spec/lib/translation_migrator_spec.rb +3 -3
  128. data/spec/models/awesome_translations/handler_spec.rb +1 -1
  129. data/spec/models/awesome_translations/translated_value_spec.rb +1 -3
  130. data/spec/models/awesome_translations/translation_spec.rb +7 -2
  131. data/spec/services/test_translate_service_spec.rb +11 -0
  132. data/spec/spec_helper.rb +7 -8
  133. metadata +182 -91
  134. data/config/initializers/simple_form_ransack.rb +0 -1
@@ -1,28 +1,13 @@
1
1
  class AwesomeTranslations::GlobalTranslator
2
- RUBY_2 = RUBY_VERSION.starts_with?("2")
3
-
4
2
  def self.call_information(caller_number)
5
- if RUBY_2
6
- # This is much faster than the other solution
7
- call = caller_locations(caller_number + 2, caller_number + 2).first
8
-
9
- {
10
- method: call.label,
11
- path: call.absolute_path,
12
- line_no: call.lineno
13
- }
14
- else
15
- call = caller[caller_number + 1]
16
- file_info = call.match(/\A(.+):(\d+):in `(.+?)'/)
17
-
18
- raise "Could not get previous file name from: #{caller[0]}" if file_info[1].blank?
19
-
20
- {
21
- method: file_info[3],
22
- path: file_info[1],
23
- line_no: file_info[2]
24
- }
25
- end
3
+ # This is much faster than the other solution
4
+ call = caller_locations(caller_number + 2, caller_number + 2).first
5
+
6
+ {
7
+ method: call.label,
8
+ path: call.absolute_path,
9
+ line_no: call.lineno
10
+ }
26
11
  end
27
12
 
28
13
  def self.translate(key, args, &blk)
@@ -37,22 +22,22 @@ class AwesomeTranslations::GlobalTranslator
37
22
  new(key: key, args: args, blk: blk, call: call).translation
38
23
  end
39
24
 
40
- attr_reader :key, :args, :blk, :call
25
+ attr_reader :args, :blk, :call, :key, :translation_args, :translation_opts
41
26
 
42
- def initialize(init_args)
43
- @key = init_args.fetch(:key)
44
- @args = init_args.fetch(:args)
45
- @blk = init_args.fetch(:blk)
46
- @call = init_args.fetch(:call)
27
+ def initialize(args:, blk:, call:, key:)
28
+ @args = args
29
+ @blk = blk
30
+ @call = call
31
+ @key = key
32
+ @translation_args = args[:translation_args] || []
33
+ @translation_opts = args[:translation_opts] || {}
47
34
  end
48
35
 
49
36
  def translation
50
- if @key.is_a?(String) && @key.start_with?(".")
51
- # Change key to full path.
52
- @key = translation_key
53
- end
37
+ # Change key to full path.
38
+ @key = translation_key if @key.is_a?(String) && @key.start_with?(".")
54
39
 
55
- I18n.t(@key, *args[:translation_args], &blk)
40
+ I18n.t(@key, *translation_args, **translation_opts, &blk)
56
41
  end
57
42
 
58
43
  private
@@ -60,8 +45,8 @@ private
60
45
  def dir
61
46
  if @_dir.nil?
62
47
  @_dir = File.dirname(previous_file)
63
- @_dir = @_dir.gsub(/\A#{Regexp.escape(Rails.root.to_s)}\//, "")
64
- @_dir = @_dir.gsub(/\Aspec\/dummy\//, "")
48
+ @_dir.delete_prefix!("#{Rails.root}/") # rubocop:disable Rails/FilePath
49
+ @_dir.delete_prefix!("spec/dummy/")
65
50
 
66
51
  if @_dir.starts_with?("app/controllers")
67
52
  @_dir = @_dir.gsub(/\Aapp\/controllers(\/?)/, "")
@@ -69,7 +54,7 @@ private
69
54
  elsif @_dir.starts_with?("app/views")
70
55
  @_dir = @_dir.gsub(/\Aapp\/views(\/?)/, "")
71
56
  elsif @_dir.starts_with?("app/")
72
- @_dir = @_dir.gsub(/\Aapp\//, "")
57
+ @_dir.delete_prefix!("app/")
73
58
  end
74
59
  end
75
60
 
@@ -99,6 +84,7 @@ private
99
84
  root = engine.root.to_s
100
85
 
101
86
  next unless @_previous_file.starts_with?(root)
87
+
102
88
  @_previous_file = @_previous_file.gsub(/\A#{Regexp.escape(root)}\//, "")
103
89
  break
104
90
  end
@@ -109,7 +95,7 @@ private
109
95
 
110
96
  def translation_key
111
97
  translation_key = dir
112
- translation_key = translation_key.gsub(/\Aapp\//, "")
98
+ translation_key = translation_key.delete_prefix("app/")
113
99
  translation_key << "/#{file}"
114
100
 
115
101
  key_parts = translation_key.split("/")
@@ -27,6 +27,7 @@ class AwesomeTranslations::Handlers::FileHandler < AwesomeTranslations::Handlers
27
27
  file = erb_inspector.file(group.data.fetch(:root_path), group.data.fetch(:file_path))
28
28
  file.translations.each do |translation|
29
29
  next if translations_found.key?(translation.full_key)
30
+
30
31
  translations_found[translation.full_key] = true
31
32
  yielder << translation.model
32
33
  end
@@ -12,7 +12,7 @@ class AwesomeTranslations::Handlers::ModelHandler < AwesomeTranslations::Handler
12
12
 
13
13
  def translations_for_group(group)
14
14
  ArrayEnumerator.new do |yielder|
15
- model_inspector = AwesomeTranslations::ModelInspector.model_classes.find { |model_inspector| model_inspector.clazz.name == group.name }
15
+ model_inspector = AwesomeTranslations::ModelInspector.model_classes.find { |model_inspector_i| model_inspector_i.clazz.name == group.name }
16
16
  raise "No inspector by that name: #{group.name}" unless model_inspector
17
17
 
18
18
  model_names(model_inspector).each { |translation| yielder << translation }
@@ -12,7 +12,7 @@ class AwesomeTranslations::Handlers::ValidationsHandler < AwesomeTranslations::H
12
12
 
13
13
  def translations_for_group(group)
14
14
  ArrayEnumerator.new do |yielder|
15
- model_inspector = AwesomeTranslations::ModelInspector.model_classes.find { |model_inspector| model_inspector.clazz.name == group.name }
15
+ model_inspector = AwesomeTranslations::ModelInspector.model_classes.find { |model_inspector_i| model_inspector_i.clazz.name == group.name }
16
16
  raise "No inspector by that name: #{group.name}" unless model_inspector
17
17
 
18
18
  model_inspector.clazz._validators.each do |attribute_name, validators|
@@ -23,11 +23,11 @@ class AwesomeTranslations::Handlers::ValidationsHandler < AwesomeTranslations::H
23
23
  translations_for_format_validator(validator, model_inspector, attribute_name, yielder)
24
24
  elsif validator.is_a?(ActiveRecord::Validations::UniquenessValidator)
25
25
  translations_for_uniqueness_validator(validator, model_inspector, attribute_name, yielder)
26
- elsif validator.class.name == "ActiveRecord::Validations::PresenceValidator"
26
+ elsif validator.class.name == "ActiveRecord::Validations::PresenceValidator" # rubocop:disable Style/ClassEqualityComparison:
27
27
  translations_for_presence_validator(validator, model_inspector, attribute_name, yielder)
28
- elsif validator.class.name == "EmailValidator"
28
+ elsif validator.class.name == "EmailValidator" # rubocop:disable Style/ClassEqualityComparison:
29
29
  translations_for_email_validator(validator, model_inspector, attribute_name, yielder)
30
- elsif validator.class.name == "ActiveModel::Validations::ConfirmationValidator"
30
+ elsif validator.class.name == "ActiveModel::Validations::ConfirmationValidator" # rubocop:disable Style/ClassEqualityComparison:
31
31
  translations_for_confirmation_validator(validator, model_inspector, attribute_name, yielder)
32
32
  else
33
33
  Rails.logger.error "Unhandeled validator: #{validator.class.name}"
@@ -11,6 +11,7 @@ class AwesomeTranslations::Handlers
11
11
 
12
12
  const_name_snake = "#{match[1]}_handler"
13
13
  next if const_name_snake == "base_handler"
14
+
14
15
  const_name_camel = StringCases.snake_to_camel(const_name_snake)
15
16
 
16
17
  handler = AwesomeTranslations::Handler.new(
@@ -2,6 +2,7 @@ class AwesomeTranslations::ModelInspector
2
2
  autoload :Attribute, "#{File.dirname(__FILE__)}/model_inspector/attribute"
3
3
 
4
4
  attr_reader :clazz
5
+
5
6
  cattr_accessor :models_loaded
6
7
 
7
8
  # Yields a model-inspector for each model found in the application.
@@ -16,6 +17,7 @@ class AwesomeTranslations::ModelInspector
16
17
  ArrayEnumerator.new do |yielder|
17
18
  find_subclasses(ActiveRecord::Base) do |model_inspector|
18
19
  next if !model_inspector.clazz.name || @skip.include?(model_inspector.clazz.name)
20
+
19
21
  yielder << model_inspector
20
22
  end
21
23
  end
@@ -33,15 +35,14 @@ class AwesomeTranslations::ModelInspector
33
35
  end
34
36
  end
35
37
 
36
- def paperclip_attachments
37
- return unless ::Kernel.const_defined?("Paperclip")
38
- Paperclip::AttachmentRegistry.names_for(@clazz).each do |name|
39
- yield name
40
- end
38
+ def paperclip_attachments(&blk)
39
+ return unless ::Kernel.const_defined?(:Paperclip)
40
+
41
+ Paperclip::AttachmentRegistry.names_for(@clazz, &blk)
41
42
  end
42
43
 
43
44
  def money_attributes
44
- return if !::Kernel.const_defined?("Money") || !@clazz.respond_to?(:monetized_attributes)
45
+ return if !::Kernel.const_defined?(:Money) || !@clazz.respond_to?(:monetized_attributes)
45
46
 
46
47
  @clazz.monetized_attributes.each do |attribute|
47
48
  yield attribute[0].to_s
@@ -49,7 +50,7 @@ class AwesomeTranslations::ModelInspector
49
50
  end
50
51
 
51
52
  def globalize_attributes
52
- return if !::Kernel.const_defined?("Globalize") || !@clazz.respond_to?(:translated_attribute_names)
53
+ return if !::Kernel.const_defined?(:Globalize) || !@clazz.respond_to?(:translated_attribute_names)
53
54
 
54
55
  @clazz.translated_attribute_names.each do |attribute|
55
56
  yield attribute.to_s
@@ -73,10 +74,8 @@ class AwesomeTranslations::ModelInspector
73
74
  end
74
75
 
75
76
  # TODO: Maybe this should yield a ModelInspector::Relationship instead?
76
- def relationships
77
- @clazz.reflections.each do |key, reflection|
78
- yield key, reflection
79
- end
77
+ def relationships(&blk)
78
+ @clazz.reflections.each(&blk)
80
79
  end
81
80
 
82
81
  def attribute_key(attribute_name)
@@ -93,10 +92,11 @@ class AwesomeTranslations::ModelInspector
93
92
 
94
93
  def self.find_subclasses(clazz, &blk)
95
94
  return if @scanned[clazz.name]
95
+
96
96
  @scanned[clazz.name] = true
97
97
 
98
98
  clazz.subclasses.each do |subclass|
99
- blk.call ::AwesomeTranslations::ModelInspector.new(subclass)
99
+ yield ::AwesomeTranslations::ModelInspector.new(subclass)
100
100
  find_subclasses(subclass, &blk)
101
101
  end
102
102
  end
@@ -121,20 +121,20 @@ class AwesomeTranslations::ModelInspector
121
121
 
122
122
  # Loads models for the given app-directory (Rails-root or engine).
123
123
  def self.load_models_for(root)
124
- Dir.glob("#{root}/app/models/**/*.rb") do |model_path|
124
+ Dir.glob("#{root}/app/models/**/*.rb").sort.each do |model_path|
125
125
  next if active_storage_path?(model_path)
126
126
 
127
127
  begin
128
128
  require model_path
129
- rescue => e
130
- $stderr.puts "Could not load model in #{model_path}"
131
- $stderr.puts e.inspect
132
- $stderr.puts e.backtrace
129
+ rescue StandardError => e
130
+ warn "Could not load model in #{model_path}"
131
+ warn e.inspect
132
+ warn e.backtrace
133
133
  end
134
134
  end
135
135
  end
136
136
 
137
137
  def self.active_storage_path?(model_path)
138
- model_path.match?(/\/gems\/activestorage-([\d\.]+)\//)
138
+ model_path.match?(/\/gems\/activestorage-([\d.]+)\//)
139
139
  end
140
140
  end
@@ -1,5 +1,5 @@
1
1
  module AwesomeTranslations::ObjectExtensions
2
- def t(key, *args, &blk)
3
- AwesomeTranslations::GlobalTranslator.translate(key, caller_number: 1, translation_args: args, &blk)
2
+ def t(key, *args, **opts, &blk)
3
+ AwesomeTranslations::GlobalTranslator.translate(key, caller_number: 1, translation_args: args, translation_opts: opts, &blk)
4
4
  end
5
5
  end
@@ -7,8 +7,8 @@ module AwesomeTranslations::TranslateFunctionality
7
7
  end
8
8
 
9
9
  module Methods
10
- def t(key, args = {}, &blk)
11
- AwesomeTranslations::GlobalTranslator.translate(key, translation_args: [args], caller_number: 1, &blk)
10
+ def t(key, *args, **opts, &blk)
11
+ AwesomeTranslations::GlobalTranslator.translate(key, caller_number: 1, translation_args: args, translation_opts: opts, &blk)
12
12
  end
13
13
  end
14
14
  end
@@ -34,17 +34,17 @@ class AwesomeTranslations::TranslationMigrator
34
34
 
35
35
  if @new_path
36
36
  FileUtils.mkdir_p(File.dirname(@new_path))
37
- File.open(@new_path, "w") { |fp| fp.write(YAML.dump(new_translations_hash)) }
37
+ File.write(@new_path, YAML.dump(new_translations_hash))
38
38
  end
39
39
 
40
40
  if translations_hash.empty?
41
41
  I18n.load_path.delete(@old_path)
42
42
  File.unlink(@old_path)
43
43
  else
44
- File.open(@old_path, "w") { |fp| fp.write(YAML.dump(translations_hash)) }
44
+ File.write(@old_path, YAML.dump(translations_hash))
45
45
  end
46
46
 
47
- @translation_value.update_attributes!(file_path: @new_path) if @new_path
47
+ @translation_value.update!(file_path: @new_path) if @new_path
48
48
  end
49
49
 
50
50
  private
@@ -1,3 +1,3 @@
1
1
  module AwesomeTranslations
2
- VERSION = "0.0.58".freeze
2
+ VERSION = "0.0.62".freeze
3
3
  end
@@ -11,6 +11,6 @@ module AwesomeTranslations
11
11
  end
12
12
 
13
13
  def self.load_object_extensions
14
- ::Object.__send__(:include, AwesomeTranslations::ObjectExtensions)
14
+ ::Object.include AwesomeTranslations::ObjectExtensions
15
15
  end
16
16
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  namespace :awesome_translations do
7
7
  task "install" => :environment do
8
- yml_root = "#{Rails.root}/config/locales/awesome_translations"
8
+ yml_root = Rails.root.join("config/locales/awesome_translations")
9
9
 
10
10
  dirs = [
11
11
  yml_root,
@@ -22,7 +22,4 @@ namespace :awesome_translations do
22
22
  end
23
23
  end
24
24
  end
25
-
26
- task "update" => :environment do
27
- end
28
25
  end
@@ -3,10 +3,10 @@ require "spec_helper"
3
3
  describe AwesomeTranslations::GroupsController do
4
4
  routes { AwesomeTranslations::Engine.routes }
5
5
 
6
- let(:model_locales_path) { Rails.root.join("config", "locales", "awesome_translations", "models").to_s }
6
+ let(:model_locales_path) { Rails.root.join("config/locales/awesome_translations/models").to_s }
7
7
  let(:user_yml_path) { "#{model_locales_path}/user/da.yml" }
8
8
  let(:role_yml_path) { "#{model_locales_path}/role/da.yml" }
9
- let(:date_time_path) { Rails.root.join("config", "locales", "awesome_translations", "date_time", "da.yml").to_s }
9
+ let(:date_time_path) { Rails.root.join("config/locales/awesome_translations/date_time/da.yml").to_s }
10
10
 
11
11
  before do
12
12
  AwesomeTranslations::CacheDatabaseGenerator.current.cache_translations
@@ -22,7 +22,7 @@ describe AwesomeTranslations::GroupsController do
22
22
 
23
23
  describe "#update" do
24
24
  it "updates translations" do
25
- expect(I18n.load_path).to_not include model_locales_path
25
+ expect(I18n.load_path).not_to include model_locales_path
26
26
 
27
27
  put :update, params: {handler_id: "model_handler", id: "User", t: {
28
28
  "activerecord.attributes.user.password" => {"da" => "Adgangskode", "de" => "Kenwort", "en" => "Password"}
@@ -47,10 +47,10 @@ describe AwesomeTranslations::GroupsController do
47
47
  .joins(:translation_key)
48
48
  .find_by(translation_keys: {key: key_to_update}, locale: "de")
49
49
 
50
- expect(da_translation_value).to_not eq nil
50
+ expect(da_translation_value).not_to eq nil
51
51
  expect(da_translation_value.value).to eq "Rolle"
52
52
 
53
- expect(de_translation_value).to_not eq nil
53
+ expect(de_translation_value).not_to eq nil
54
54
  expect(de_translation_value.value).to eq "Die type"
55
55
 
56
56
  expect(I18n.load_path).to include role_yml_path
@@ -12,7 +12,7 @@ describe AwesomeTranslations::HandlersController do
12
12
  describe "#index" do
13
13
  it "renders the page" do
14
14
  get :index
15
- expect(response).to be_success
15
+ expect(response).to be_successful
16
16
  end
17
17
  end
18
18
 
@@ -20,7 +20,7 @@ describe AwesomeTranslations::HandlersController do
20
20
  it "renders the page" do
21
21
  AwesomeTranslations::CacheDatabaseGenerator.current.cache_translations
22
22
  get :show, params: {id: "model_handler"}
23
- expect(response).to be_success
23
+ expect(response).to be_successful
24
24
  end
25
25
 
26
26
  it "filters with missing translations" do
@@ -31,8 +31,8 @@ describe AwesomeTranslations::HandlersController do
31
31
  groups = assigns(:groups)
32
32
 
33
33
  expect(all_groups.length).to be > groups.length
34
- expect(groups.length).to eq 4
35
- expect(response).to be_success
34
+ expect(groups.length).to eq 5
35
+ expect(response).to be_successful
36
36
  end
37
37
  end
38
38
 
@@ -5,8 +5,10 @@ describe UsersController do
5
5
 
6
6
  render_views
7
7
 
8
- before do
9
- I18n.locale = :en
8
+ around do |example|
9
+ I18n.with_locale(:en) do
10
+ example.call
11
+ end
10
12
  end
11
13
 
12
14
  it "#update" do
data/spec/dummy/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  # Add your own tasks in files placed in lib/tasks ending in .rake,
2
2
  # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
3
 
4
- require File.expand_path("../config/application", __FILE__)
4
+ require File.expand_path("config/application", __dir__)
5
5
 
6
6
  Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
@@ -5,15 +5,11 @@ class ApplicationController < ActionController::Base
5
5
  # For APIs, you may want to use :null_session instead.
6
6
  protect_from_forgery with: :exception
7
7
 
8
- before_action :set_locale
8
+ around_action :with_locale
9
9
 
10
10
  private
11
11
 
12
- def set_locale
13
- if session[:locale]
14
- I18n.locale = session[:locale]
15
- else
16
- I18n.locale = :en
17
- end
12
+ def with_locale(&blk)
13
+ I18n.with_locale(session[:locale] || :en, &blk)
18
14
  end
19
15
  end
@@ -22,7 +22,7 @@ class UsersController < ApplicationController
22
22
  def edit; end
23
23
 
24
24
  def update
25
- if @user.update_attributes(user_params)
25
+ if @user.update(user_params)
26
26
  flash[:notice] = controller_t(".user_was_updated")
27
27
  redirect_to user_path(@user)
28
28
  else
@@ -1,4 +1,4 @@
1
- class MyMailer < ActionMailer::Base
1
+ class MyMailer < ActionMailer::Base # rubocop:disable Rails/ApplicationMailer
2
2
  def mailer_action(_user_id)
3
3
  mail(subject: t(".custom_subject"))
4
4
  end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -1,10 +1,7 @@
1
- class Role < ActiveRecord::Base
1
+ class Role < ApplicationRecord
2
2
  translates :name
3
-
4
3
  belongs_to :user
5
-
6
- validates :user, :role, presence: true
7
-
4
+ validates :role, presence: true
8
5
  monetize :price_cents, allow_nil: true
9
6
 
10
7
  def self.roles
@@ -1,4 +1,4 @@
1
- class User < ActiveRecord::Base
1
+ class User < ApplicationRecord
2
2
  validates :email, presence: true
3
3
  validates :email, uniqueness: true
4
4
  validates :email, length: {in: 2..255}, format: {with: /\A.+@.+\Z/}
@@ -0,0 +1,3 @@
1
+ class ApplicationService
2
+ include AwesomeTranslations::TranslateFunctionality
3
+ end
@@ -0,0 +1,9 @@
1
+ class TestTranslateService < ApplicationService
2
+ def self.static_method
3
+ t(".hello_static_method")
4
+ end
5
+
6
+ def perform
7
+ t(".hello_world")
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
2
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
3
3
  load Gem.bin_path("bundler", "bundle")
data/spec/dummy/bin/rails CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- APP_PATH = File.expand_path("../../config/application", __FILE__)
2
+ APP_PATH = File.expand_path("../config/application", __dir__)
3
3
  require_relative "../config/boot"
4
4
  require "rails/commands"
@@ -1,4 +1,4 @@
1
- require File.expand_path("../boot", __FILE__)
1
+ require File.expand_path("boot", __dir__)
2
2
 
3
3
  # Pick the frameworks you want:
4
4
  require "active_record/railtie"
@@ -24,7 +24,12 @@ class Dummy::Application < Rails::Application
24
24
  # config.time_zone = 'Central Time (US & Canada)'
25
25
 
26
26
  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
27
- config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}").to_s]
27
+ config.i18n.load_path += Dir[Rails.root.join("config/locales/**/*.{rb,yml}").to_s]
28
28
  config.i18n.available_locales = [:da, :de, :en]
29
29
  config.i18n.default_locale = :en
30
+
31
+ config.assets.precompile += %w[
32
+ awesome_translations/application.css
33
+ awesome_translations/application.js
34
+ ]
30
35
  end
@@ -1,5 +1,5 @@
1
1
  # Set up gems listed in the Gemfile.
2
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../../Gemfile", __FILE__)
2
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__)
3
3
 
4
4
  require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
5
- $LOAD_PATH.unshift File.expand_path("../../../../lib", __FILE__)
5
+ $LOAD_PATH.unshift File.expand_path("../../../lib", __dir__)
@@ -1,5 +1,5 @@
1
1
  # Load the Rails application.
2
- require File.expand_path("../application", __FILE__)
2
+ require File.expand_path("application", __dir__)
3
3
 
4
4
  # Initialize the Rails application.
5
5
  Dummy::Application.initialize!
data/spec/dummy/config.ru CHANGED
@@ -1,4 +1,4 @@
1
1
  # This file is used by Rack-based servers to start the application.
2
2
 
3
- require ::File.expand_path("../config/environment", __FILE__)
3
+ require ::File.expand_path("config/environment", __dir__)
4
4
  run Rails.application
Binary file
Binary file
@@ -0,0 +1,52 @@
1
+  (1.0ms) SELECT sqlite_version(*)
2
+  (0.1ms) SELECT sqlite_version(*)
3
+  (0.1ms) DROP TABLE IF EXISTS "roles"
4
+  (5.0ms) CREATE TABLE "roles" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "role" varchar, "price_cents" integer)
5
+  (2.9ms) CREATE INDEX "index_roles_on_user_id" ON "roles" ("user_id")
6
+  (0.1ms) DROP TABLE IF EXISTS "users"
7
+  (2.8ms) CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar, "password" varchar, "age" integer)
8
+  (2.8ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY)
9
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
10
+  (2.4ms) INSERT INTO "schema_migrations" (version) VALUES (20150607110245)
11
+  (1.9ms) INSERT INTO "schema_migrations" (version) VALUES
12
+ (20141018081559),
13
+ (20141018081640);
14
+
15
+ 
16
+  (2.4ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
17
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
18
+ TRANSACTION (0.0ms) begin transaction
19
+ ActiveRecord::InternalMetadata Create (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", "2021-12-30 16:01:09.444330"], ["updated_at", "2021-12-30 16:01:09.444330"]]
20
+ TRANSACTION (1.9ms) commit transaction
21
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
22
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "schema_sha1"], ["LIMIT", 1]]
23
+ TRANSACTION (0.0ms) begin transaction
24
+ ActiveRecord::InternalMetadata Create (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "schema_sha1"], ["value", "2fb15a187a8db3fd34877067601f82a165001b33"], ["created_at", "2021-12-30 16:01:09.448757"], ["updated_at", "2021-12-30 16:01:09.448757"]]
25
+ TRANSACTION (1.8ms) commit transaction
26
+  (0.1ms) SELECT sqlite_version(*)
27
+  (0.1ms) DROP TABLE IF EXISTS "roles"
28
+  (2.5ms) CREATE TABLE "roles" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "role" varchar, "price_cents" integer)
29
+  (2.3ms) CREATE INDEX "index_roles_on_user_id" ON "roles" ("user_id")
30
+  (0.1ms) DROP TABLE IF EXISTS "users"
31
+  (2.6ms) CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar, "password" varchar, "age" integer)
32
+  (2.3ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY)
33
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
34
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES (20150607110245)
35
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES
36
+ (20141018081559),
37
+ (20141018081640);
38
+
39
+ 
40
+  (2.4ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
41
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
42
+ TRANSACTION (0.0ms) begin transaction
43
+ ActiveRecord::InternalMetadata Create (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", "2021-12-30 16:01:09.471093"], ["updated_at", "2021-12-30 16:01:09.471093"]]
44
+ TRANSACTION (1.7ms) commit transaction
45
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
46
+ TRANSACTION (0.0ms) begin transaction
47
+ ActiveRecord::InternalMetadata Update (0.2ms) UPDATE "ar_internal_metadata" SET "value" = ?, "updated_at" = ? WHERE "ar_internal_metadata"."key" = ? [["value", "test"], ["updated_at", "2021-12-30 16:01:09.474305"], ["key", "environment"]]
48
+ TRANSACTION (1.7ms) commit transaction
49
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "schema_sha1"], ["LIMIT", 1]]
50
+ TRANSACTION (0.0ms) begin transaction
51
+ ActiveRecord::InternalMetadata Create (0.2ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "schema_sha1"], ["value", "2fb15a187a8db3fd34877067601f82a165001b33"], ["created_at", "2021-12-30 16:01:09.477396"], ["updated_at", "2021-12-30 16:01:09.477396"]]
52
+ TRANSACTION (1.8ms) commit transaction