purizumu 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41586286c24fbf46df721821901d6a0ef19c30cc9b28f4e186202140dd84f8da
4
- data.tar.gz: 9c1e474e116fa9f63c817f64f4e00727ee59e69d4c70577292e8a0446f194934
3
+ metadata.gz: 2d408af4fde18f4121def0d7c973ded6e80fee0893a1fd2c9cfdfe46b1595dae
4
+ data.tar.gz: ca1fdc72ff34d5247d2507547ff6e012b41f4779da1a80131cfbc05acd91fb5e
5
5
  SHA512:
6
- metadata.gz: 92e7c2d4c42e313b0381ab3f6e327f897dfac150d41644d1e60ffc2d7b5a99948dd5c5679d61b0a03a4d4b79d2e8eb3490104af6a4930ab58e8a700d99699695
7
- data.tar.gz: b7d7b134a4c85de69272411df15a21674188a9cedb3d811b9b58923e37d91774d0e7f27c0e97ff95a0c281a183e134631f380a44807381d0846ed165263f1e3c
6
+ metadata.gz: 35e08731206e156bd2a9bf63cdf6329b87d715ceb4d5d1844c85995186f2bcfdbbbefebfc7aee15549e4e3b1af2c992359d6058baf6e8d3aeef37f1cbc427439
7
+ data.tar.gz: 64cecd53d1431269285e3d3908687522880cdfd3b4e17495da5debbd05269d35e1783794d723990e7387d03e98ea84f00d09fabfebbeee9e5a8a6d6b4bcbc794
@@ -5,17 +5,40 @@ module Purizumu
5
5
  module ModelExtensions
6
6
  def self.included(base)
7
7
  base.extend(ClassMethods)
8
+ base.class_attribute :purizumu_translated_attributes, instance_accessor: false, default: []
8
9
  end
9
10
 
10
11
  # Defines translated attribute readers on Active Record models.
11
12
  module ClassMethods
12
13
  def attribute_translation(*attribute_names)
13
- attribute_names.flatten.each do |attribute_name|
14
+ translated_attribute_names = attribute_names.flatten.map(&:to_s)
15
+ self.purizumu_translated_attributes = (
16
+ purizumu_translated_attributes + translated_attribute_names
17
+ ).uniq
18
+
19
+ after_commit :purizumu_clear_translations_after_commit, on: :update
20
+
21
+ translated_attribute_names.each do |attribute_name|
14
22
  define_method(attribute_name) do
15
23
  Purizumu.translate_attribute(self, attribute_name)
16
24
  end
17
25
  end
18
26
  end
19
27
  end
28
+
29
+ private
30
+
31
+ def purizumu_clear_translations_after_commit
32
+ changed_translated_attributes = (
33
+ previous_changes.keys & self.class.purizumu_translated_attributes
34
+ )
35
+ return if changed_translated_attributes.empty?
36
+
37
+ Purizumu::Translation.where(
38
+ model_class_name: self.class.name,
39
+ record_id: id,
40
+ attribute_name: changed_translated_attributes
41
+ ).delete_all
42
+ end
20
43
  end
21
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Purizumu
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -9,11 +9,11 @@ RSpec.describe Purizumu::Translator do
9
9
  stub_const('GemRecord', Class.new(ActiveRecord::Base) do
10
10
  self.table_name = 'gems'
11
11
 
12
- attribute_translation :human_name
12
+ attribute_translation :human_name, :tagline
13
13
  end)
14
14
  end
15
15
 
16
- let!(:record) { GemRecord.create!(human_name: 'Prism') }
16
+ let!(:record) { GemRecord.create!(human_name: 'Prism', tagline: 'Crystal clear', slug: 'prism') }
17
17
  let(:translation_scope) do
18
18
  Purizumu::Translation.where(
19
19
  model_class_name: 'GemRecord',
@@ -109,12 +109,70 @@ RSpec.describe Purizumu::Translator do
109
109
 
110
110
  expect(Purizumu::Translation.where(record_id: blank_record.id)).to be_empty
111
111
  end
112
+
113
+ it 'clears persisted translations for a changed translated attribute after commit' do
114
+ create_existing_translation
115
+
116
+ expect { record.update!(human_name: 'Politics') }
117
+ .to change { translation_scope.pluck(:attribute_name, :locale, :content) }
118
+ .from([%w[human_name jp プリズム]]).to([])
119
+ end
120
+
121
+ it 'preserves other translated attribute rows when one translated attribute changes' do
122
+ create_existing_translation
123
+ create_tagline_translation
124
+ record.update!(human_name: 'Politics')
125
+
126
+ expect(tagline_translation_rows).to eq([%w[tagline jp とても明快]])
127
+ end
128
+
129
+ it 'regenerates translations from the new source content after invalidation' do
130
+ prepare_regenerated_translation
131
+
132
+ expect(record.human_name).to eq('Политика')
133
+ end
134
+
135
+ it 'persists regenerated translations after invalidation' do
136
+ prepare_regenerated_translation
137
+ record.human_name
138
+
139
+ expect(
140
+ translation_scope.find_by!(locale: 'ru').content
141
+ ).to eq('Политика')
142
+ end
143
+
144
+ it 'does not clear translations when only an untranslated attribute changes' do
145
+ create_existing_translation
146
+
147
+ expect do
148
+ record.update!(slug: 'renamed-prism')
149
+ end.not_to(change { translation_scope.pluck(:attribute_name, :locale, :content) })
150
+ end
112
151
  end
113
152
 
114
153
  def create_existing_translation
115
154
  Purizumu::Translation.create!(existing_translation_attributes)
116
155
  end
117
156
 
157
+ def prepare_regenerated_translation
158
+ create_existing_translation
159
+ allow(Purizumu).to receive(:engine).and_return(
160
+ instance_double(Purizumu::Engines::Xai, translate: 'Политика')
161
+ )
162
+ record.update!(human_name: 'Politics')
163
+ I18n.locale = :ru
164
+ end
165
+
166
+ def create_tagline_translation
167
+ Purizumu::Translation.create!(
168
+ model_class_name: 'GemRecord',
169
+ record_id: record.id,
170
+ attribute_name: 'tagline',
171
+ locale: 'jp',
172
+ content: 'とても明快'
173
+ )
174
+ end
175
+
118
176
  def expected_engine_arguments
119
177
  {
120
178
  model_name: 'GemRecord',
@@ -124,4 +182,12 @@ RSpec.describe Purizumu::Translator do
124
182
  source_locale: 'en'
125
183
  }
126
184
  end
185
+
186
+ def tagline_translation_rows
187
+ Purizumu::Translation.where(
188
+ model_class_name: 'GemRecord',
189
+ record_id: record.id,
190
+ attribute_name: 'tagline'
191
+ ).pluck(:attribute_name, :locale, :content)
192
+ end
127
193
  end
data/spec/spec_helper.rb CHANGED
@@ -24,6 +24,8 @@ RSpec.configure do |config|
24
24
  suppress_messages do
25
25
  create_table :gems, force: true do |t|
26
26
  t.string :human_name
27
+ t.string :slug
28
+ t.string :tagline
27
29
  end
28
30
 
29
31
  create_table :purizumu_translations, force: true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purizumu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenAI Codex
@@ -79,7 +79,7 @@ files:
79
79
  - spec/purizumu/generators/install_generator_spec.rb
80
80
  - spec/purizumu/translator_spec.rb
81
81
  - spec/spec_helper.rb
82
- - spec/tmp/generator/db/migrate/20260412193408_create_purizumu_translations.rb
82
+ - spec/tmp/generator/db/migrate/20260412203610_create_purizumu_translations.rb
83
83
  homepage: https://example.com/purizumu
84
84
  licenses:
85
85
  - MIT