translated 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf06c93932fadce4e676acef5b3521c15bb62522a28bcec98fc92f9e94f5819e
4
- data.tar.gz: 8ebdbdf3b47b23b45c023cc23afc25036e52fd2595d156d58799d1d4099965a7
3
+ metadata.gz: 8b0fa395bf9d6140de846cf8198420887598969f58cbff6b2f8e76e34dd59849
4
+ data.tar.gz: f3540086ff6da0902232e25ab62f340c58f6a16b96b3eecfecabf665f7732eff
5
5
  SHA512:
6
- metadata.gz: add8b99d4d294c79d754388c8a5f6ccc07baec15b7fcb9708c69830e63b0dd73a1359d979a7a641bbe578ac726303b8ad5fe5764f642c73e104379569475625d
7
- data.tar.gz: 1cc585e85c0914c07d8c032830898467a94dffd40013763ac4aeba98dbad0c7432031c8331945bc888cac040fc19906474bbf2d068b0b4364c4c255cf64228c9
6
+ metadata.gz: 77eaf5e0c292f4b52a034ca2fb2ec492bd9dbf770dab1bd2f2b71c394ff98888a733c84fabcca7feb8984398f2d8e6c5a8d6cdf06d795336789e9d95d7437163
7
+ data.tar.gz: 7eab53083e0a4c1aa3b1edb1dd69536780220e45d377e70749681585a5f41c57ca7be4ffdb8bfd31c82ef39ac7f7247c3e8f52233d00b32b8420d8a4cf11be93
data/README.md CHANGED
@@ -54,5 +54,9 @@ Translated.api_key = 'API KEY from translatedrb.com'
54
54
  # Translated.environments = %w(development production)
55
55
  ```
56
56
 
57
+ ## Example Project
58
+
59
+ Check out an example Rails project with Translated already installed [here](https://github.com/getcomfortly/translated-example).
60
+
57
61
  ## License
58
62
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -4,8 +4,8 @@ module Translated
4
4
  class UpdateRichTranslationsJob < ApplicationJob
5
5
  queue_as :default
6
6
 
7
- def perform(record, attribute_name, from_locale, to_locale)
8
- record.public_send(:"generate_translation_for_#{attribute_name}", from_locale, to_locale)
7
+ def perform(record, attribute_name)
8
+ record.public_send(:"generate_translations_for_#{attribute_name}")
9
9
  end
10
10
  end
11
11
  end
@@ -23,6 +23,10 @@ module Translated
23
23
  content
24
24
  end
25
25
 
26
+ def #{name}_changed?
27
+ #{name}_translation_changed? || super
28
+ end
29
+
26
30
  private
27
31
 
28
32
  def #{name}_translation_changed?
@@ -40,7 +44,15 @@ module Translated
40
44
 
41
45
  validates name, validates if validates.present?
42
46
 
43
- scope :with_translations, -> { includes(:translated_text_fields) }
47
+ scope :"with_#{name}_translation", -> { includes(:"#{name}_translation") }
48
+ end
49
+
50
+ def with_translations
51
+ includes(translation_association_names)
52
+ end
53
+
54
+ def translation_association_names
55
+ reflect_on_all_associations(:has_one).map(&:name).select { |n| n.ends_with?('_translation') }
44
56
  end
45
57
 
46
58
  def has_translated_rich_text(name) # rubocop:disable Naming/PredicateName
@@ -55,32 +67,30 @@ module Translated
55
67
 
56
68
  def #{name}=(body)
57
69
  self.public_send(:"#{name}_\#{I18n.locale}=", body)
58
- @_needs_rich_translation ||= []
70
+ @_#{name}_translation_changed = true
71
+ body
72
+ end
59
73
 
74
+ def generate_translations_for_#{name}
60
75
  I18n.available_locales.each do |locale|
61
76
  next if locale == I18n.locale
62
77
 
63
- @_needs_rich_translation << [:#{name}, I18n.locale, locale]
78
+ from = I18n.locale
79
+ to = locale
80
+ content = public_send(:"#{name}_\#{from}").body&.to_html
81
+ translation = content.present? ? Translator.new.translate(content, from:, to:) : nil
82
+ update("#{name}_\#{to}" => translation)
64
83
  end
65
-
66
- body
67
- end
68
-
69
- def generate_translation_for_#{name}(from, to)
70
- self.public_send(:"#{name}_\#{to}=", Translator.new.translate(public_send(:"#{name}_\#{from}").body.to_html, from:, to:))
71
- save!
72
84
  end
73
85
 
74
86
  private
75
87
 
76
- def needs_rich_translations?
77
- defined?(:@_needs_rich_translation) && @_needs_rich_translation.present?
88
+ def #{name}_translation_changed?
89
+ defined?(:@_#{name}_translation_changed) && @_#{name}_translation_changed
78
90
  end
79
91
 
80
- def update_rich_translations_later
81
- @_needs_rich_translation.each do |args|
82
- UpdateRichTranslationsJob.perform_later(self, *args)
83
- end
92
+ def generate_translations_for_#{name}_later
93
+ UpdateRichTranslationsJob.perform_later(self, :#{name})
84
94
  end
85
95
  CODE
86
96
 
@@ -88,7 +98,7 @@ module Translated
88
98
  has_rich_text :"#{name}_#{locale}"
89
99
  end
90
100
 
91
- after_commit :update_rich_translations_later, if: :needs_rich_translations?
101
+ after_save :"generate_translations_for_#{name}_later", if: :"#{name}_translation_changed?"
92
102
 
93
103
  scope :"with_rich_text_#{name}", lambda {
94
104
  includes(I18n.available_locales.map do |locale|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Translated
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trae Robrock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-05-09 00:00:00.000000000 Z
12
+ date: 2024-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activejob