copy_tuner_client 1.3.0 → 1.5.0

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: d298d7f59a4cacd30c925e2a0af2b6026fbd3610d4564fae70e5b6efbad4ad0c
4
- data.tar.gz: 0f7e09019505a1b49353550b696a2f4a0a4a40eaf74573e4a4e916ec547bd1e9
3
+ metadata.gz: 238593f77e71df10a71b7710228cf56cf930a320a141a74e573e9ff015b1f730
4
+ data.tar.gz: acd4453041d32c2e43b81207953fca9b68c1bbec5f4cc3dafafc4722fcaada54
5
5
  SHA512:
6
- metadata.gz: 934b99a1624d8dd517c9f2df68c37ffb667128beca73222db458b5f973534398eda88f01c6550c48219e54205fb14b6e47d9731f9556ec0c55d06ce5b47b3729
7
- data.tar.gz: 11e6f16e6792603ee6d5c74f4888f293895cf483d10f88d57298bc80bd54a05cb50da5dcdba2c9ade055bf6d57afe66a398b355de806a87a0528dea9e19efaae
6
+ metadata.gz: bea767cacf8232f89e3fdd6f1b6b0caee1957b7306cbf018cadfcccee49a7ce22d74640f4319771cb4dda000b61142d45190791cb46aa0b2ee4f4e1f216d7576
7
+ data.tar.gz: 8f4c0d08195e35de0918ca7b1380146770242867f681166a39d8fc206cf16965f316e532bef59baa0525fe26f069b99b876f8ba38de49f6b88d25e2d2a820edc
@@ -0,0 +1,18 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ cooldown:
6
+ default-days: 5
7
+ ignore:
8
+ - dependency-name: "ruby/setup-ruby"
9
+ update-types:
10
+ - "version-update:semver-patch"
11
+ - "version-update:semver-minor"
12
+ - dependency-name: "actions/checkout"
13
+ update-types:
14
+ - "version-update:semver-patch"
15
+ - "version-update:semver-minor"
16
+ schedule:
17
+ # Check for updates to GitHub Actions every weekday
18
+ interval: "weekly"
@@ -22,14 +22,12 @@ jobs:
22
22
  strategy:
23
23
  fail-fast: false
24
24
  matrix:
25
- ruby: ["3.1", "3.2", "3.3", "3.4"]
26
- gemfile: ["7.1", "7.2", "8.0", "main"]
25
+ ruby: ["3.3", "3.4", "4.0"]
26
+ gemfile: ["8.0", "8.1", "main"]
27
27
  exclude:
28
- - ruby: "3.1"
29
- gemfile: "8.0"
30
- - ruby: "3.1"
28
+ - ruby: "3.3"
31
29
  gemfile: "main"
32
- - ruby: "3.2"
30
+ - ruby: "3.4"
33
31
  gemfile: "main"
34
32
  steps:
35
33
  - name: Install packages
@@ -37,7 +35,7 @@ jobs:
37
35
  sudo apt update -y
38
36
  sudo apt install -y libsqlite3-dev
39
37
 
40
- - uses: actions/checkout@v2
38
+ - uses: actions/checkout@v6
41
39
 
42
40
  - name: Set up Ruby
43
41
  uses: ruby/setup-ruby@v1
data/.rubocop.yml CHANGED
@@ -1,2 +1,7 @@
1
1
  inherit_gem:
2
2
  sgcop: ruby/rubocop.yml
3
+
4
+ # .ruby-version が RuboCop 未対応の新しい Ruby を指すと解析が落ちるため、
5
+ # サポート最低バージョンを明示する
6
+ AllCops:
7
+ TargetRubyVersion: 3.3
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.4.5
1
+ 4.0.5
@@ -2,11 +2,11 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)
2
2
  require 'copy_tuner_client/version'
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.required_ruby_version = '>= 3.1.0'
5
+ s.required_ruby_version = '>= 3.3.0'
6
6
  s.add_dependency 'i18n', '>= 0.5.0'
7
7
  s.add_dependency 'json'
8
8
  s.add_dependency 'nokogiri'
9
- s.add_development_dependency 'rails', '~> 7.1'
9
+ s.add_development_dependency 'rails', '~> 8.0'
10
10
  s.add_development_dependency 'rake'
11
11
  s.add_development_dependency 'rspec'
12
12
  s.add_development_dependency 'sham_rack'
@@ -1,5 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "~> 7.1"
3
+ gem "rails", "~> 8.1"
4
4
 
5
5
  gemspec :path => "../"
@@ -3,9 +3,7 @@ module CopyTunerClient
3
3
  def to_h(dotted_hash)
4
4
  hash = {}
5
5
  dotted_hash.to_h.transform_keys(&:to_s).sort.each do |key, value|
6
- # Rails i18n標準との互換性のため、特定のキーを適切な型に変換
7
- converted_value = convert_value_type(key, value)
8
- _hash = key.split('.').reverse.inject(converted_value) { |memo, _key| { _key => memo } }
6
+ _hash = key.split('.').reverse.inject(value) { |memo, _key| { _key => memo } }
9
7
  hash.deep_merge!(_hash)
10
8
  end
11
9
  hash
@@ -29,22 +27,6 @@ module CopyTunerClient
29
27
  results
30
28
  end
31
29
 
32
- private
33
-
34
- def convert_value_type(key, value)
35
- return value unless value.is_a?(String)
36
-
37
- # Rails i18n標準で数値型として扱われるキー
38
- if key.end_with?('.precision')
39
- value.to_i
40
- # Rails i18n標準で真偽値として扱われるキー
41
- elsif key.end_with?('.significant', '.strip_insignificant_zeros')
42
- value == 'true'
43
- else
44
- value
45
- end
46
- end
47
-
48
- module_function :to_h, :conflict_keys, :convert_value_type # rubocop:disable Style/AccessModifierDeclarations
30
+ module_function :to_h, :conflict_keys # rubocop:disable Style/AccessModifierDeclarations
49
31
  end
50
32
  end
@@ -17,6 +17,12 @@ module CopyTunerClient
17
17
  I18n.t(key.to_s.first == '.' ? scope_key_by_partial(key) : key, **options)
18
18
  end
19
19
 
20
+ # NOTE: マーカーは HTML コメントとしてブラウザに無視されつつ Copyray オーバーレイのキー特定に
21
+ # 使われる。HTML 以外の経路(メール本文・render :json・CSV/PDF など)ではコメントが文字列として
22
+ # 出力に混入してしまうため、それらの経路には注入しない。default 引数による初期値登録は維持する
23
+ # 必要があるため、このガードは初期値登録(上の I18n.t 呼び出し)より後に置く。
24
+ return source unless copyray_injectable?
25
+
20
26
  if CopyTunerClient.configuration.disable_copyray_comment_injection
21
27
  source
22
28
  else
@@ -32,6 +38,24 @@ module CopyTunerClient
32
38
  CopyTunerClient::Copyray.augment_template(source, scope_key)
33
39
  end
34
40
  end
41
+
42
+ # NOTE: HTML 以外の経路(メール本文・render :json・CSV/PDF など)ではマーカーが文字列として
43
+ # 出力に混入するため注入しない。判定には controller.request.format を使い、@current_template.format /
44
+ # lookup_context.formats のような ActionView の内部実装には依存させない(Rails バージョン間で壊れうるため)。
45
+ def copyray_injectable?
46
+ current_controller = controller
47
+ return false if current_controller.nil?
48
+
49
+ # NOTE: mailer は request を持たず request.format で判定できない。かつメール本文への
50
+ # マーカー混入は実害が大きいため、controller の型で明示除外する。
51
+ return false if defined?(ActionMailer::Base) && current_controller.is_a?(ActionMailer::Base)
52
+
53
+ # NOTE: request が無い/format が html でない経路には注入しない。&. と || false で
54
+ # request 不在時も安全に false を返す。
55
+ current_controller.request&.format&.html? || false
56
+ end
57
+ private :copyray_injectable?
58
+
35
59
  if middleware_enabled
36
60
  alias_method :translate_without_copyray_comment, :translate
37
61
  alias_method :translate, :translate_with_copyray_comment
@@ -1,6 +1,6 @@
1
1
  module CopyTunerClient
2
2
  # Client version
3
- VERSION = '1.3.0'.freeze
3
+ VERSION = '1.5.0'.freeze
4
4
 
5
5
  # API version being used to communicate with the server
6
6
  API_VERSION = '2.0'.freeze
@@ -18,13 +18,19 @@ license: MIT
18
18
  | 判別 | 管理場所 | 操作 |
19
19
  |---|---|---|
20
20
  | マッチする | config/locales(copy_tuner と完全分離) | `config/locales/*.yml` を Read / Edit |
21
- | マッチしない・未設定 | copy_tuner | MCP ツール(下記) |
21
+ | マッチしない・未設定 | copy_tuner | **MCP ツール経由でのみ探す**(下記) |
22
+
23
+ **copy_tuner 管理のキーは必ず MCP ツール経由で探すこと。** ローカルの `.yml` を grep して見つけたつもりになってはならない。
22
24
 
23
25
  **例外(gem 組み込み・設定不要で常に config/locales 管理):** Rails 標準の `number.format` /
24
26
  `number.currency.format` / `number.percentage.format` / `number.human.format`(およびその配下)。
25
27
  `precision` 等の非文字列値を含み copy_tuner では正しく扱えないため、gem が常にバイパスする。
26
28
  これらは `config/locales/*.yml` を見る・編集すること(アプリ独自の `number.gift_amount` 等は対象外で通常どおり copy_tuner)。
27
29
 
30
+ **`config/locales` 以外の export 済み yml は参照禁止:**
31
+ 環境によっては copy_tuner から export したキャッシュ用の `.yml`(`config/locales` 配下以外、例: アプリ直下や `tmp/`、`db/` などに置かれた export ファイル)が存在することがある。
32
+ これらは古いスナップショットであり権威ある情報源ではない。**読んで判断材料にしてはならない。** copy_tuner 管理キーは常に MCP ツールで最新を確認する。
33
+
28
34
  **config/locales 管理キーの落とし穴:**
29
35
  - copy_tuner には存在しないので、MCP ツールで探しても見つからない。`config/locales` を見ること。
30
36
  - copy_tuner に登録しても lookup でバイパスされ無効。必ず `.yml` 側に書く。
@@ -38,7 +44,8 @@ license: MIT
38
44
  | キー名(一部)から翻訳を調べる | `search_key`(引数は英語キーワード) |
39
45
  | 画面テキストからキーを逆引き | `search_translations` |
40
46
  | 新しいキーを登録する | `create_i18n_key` |
41
- | 既存キーの翻訳を更新する | `get_edit_url`(ブラウザ編集) |
47
+ | 既存キーのドラフト訳を更新する | `update_i18n_key` |
48
+ | 既存キーの翻訳を更新する(ブラウザ) | `get_edit_url` |
42
49
  | 使用中ロケールを確認 | `get_locales` |
43
50
 
44
51
  ## 新規キー登録
@@ -46,6 +53,19 @@ license: MIT
46
53
  1. `search_key` で関連キーを検索し、既存の命名パターンに合わせる(例: `search_key("move_out")`)。
47
54
  2. ja は必須。他は `get_locales` で確認し、存在するロケール分を登録する。
48
55
 
56
+ ## 既存キーの訳を更新する
57
+
58
+ `update_i18n_key` を使う。`key` と `translations`(ロケール・値のペア配列)は両方必須。
59
+
60
+ **制約**: ドラフト状態または未発行のキーのみ更新可能。発行済み(published)かどうかを事前に知る手段はなく、API にリクエストしてエラーが返るかどうかで初めてわかる。
61
+
62
+ **published キーへの対応方針**: 値を直接書き換えるのはリスクが高いため、原則として以下の手順を推奨する:
63
+ 1. 新しいキーにバージョン番号を付けて登録する(例: `views.foo_v2`)→ **新規キー登録の手順に従う**
64
+ 2. コード側の参照を新キーに差し替える
65
+ 3. 古いキーを `config/initializers/copy_tuner.rb` の `ignored_keys` に追加する
66
+
67
+ どうしても既存キーを書き換えたい場合は `get_edit_url` でブラウザから編集する。
68
+
49
69
  ## 不要なキーの処理
50
70
 
51
71
  削除せず `config/initializers/copy_tuner.rb` の `ignored_keys` に追加する。
@@ -59,75 +59,22 @@ describe CopyTunerClient::DottedHash do
59
59
  it { is_expected.to eq({ 'en' => { 'test' => { 'key' => 'en test value' } } }) }
60
60
  end
61
61
 
62
- context "Rails i18nの数値precisionキーの場合" do
62
+ # NOTE: number.*.format 配下のキー(precision 等)は I18nBackend の local_first_key? ガードで
63
+ # tree_cache をバイパスしローカル YAML 優先になるため、ここで型変換せず文字列のまま保持する。
64
+ # (number_to_currency が壊れないことは i18n_backend_spec の number ローカル優先テストで担保)
65
+ context "number.*.format 配下の値を含む場合" do
63
66
  let(:dotted_hash) do
64
67
  {
65
68
  'en.number.currency.format.precision' => '2',
66
- 'en.number.format.precision' => '3',
67
- }
68
- end
69
-
70
- it "precision値を整数に変換する" do
71
- is_expected.to eq({
72
- 'en' => {
73
- 'number' => {
74
- 'currency' => {
75
- 'format' => {
76
- 'precision' => 2,
77
- },
78
- },
79
- 'format' => {
80
- 'precision' => 3,
81
- },
82
- },
83
- },
84
- })
85
- end
86
- end
87
-
88
- context "Rails i18nのbooleanキーの場合" do
89
- let(:dotted_hash) do
90
- {
91
- 'en.number.currency.format.significant' => 'false',
92
- 'en.number.format.strip_insignificant_zeros' => 'true',
93
- }
94
- end
95
-
96
- it "boolean値を実際の真偽値に変換する" do
97
- is_expected.to eq({
98
- 'en' => {
99
- 'number' => {
100
- 'currency' => {
101
- 'format' => {
102
- 'significant' => false,
103
- },
104
- },
105
- 'format' => {
106
- 'strip_insignificant_zeros' => true,
107
- },
108
- },
109
- },
110
- })
111
- end
112
- end
113
-
114
- context "Rails i18n以外で似たパターンを含むキーの場合" do
115
- let(:dotted_hash) do
116
- {
117
69
  'en.custom.precision' => 'custom_value',
118
- 'en.other.significant_value' => 'true',
119
70
  }
120
71
  end
121
72
 
122
- it "Rails i18nパターンで終わるキーのみ変換する" do
73
+ it "型変換せず値を文字列のまま保持する" do
123
74
  is_expected.to eq({
124
75
  'en' => {
125
- 'custom' => {
126
- 'precision' => 0, # .precision suffix triggers conversion
127
- },
128
- 'other' => {
129
- 'significant_value' => 'true', # no conversion for non-exact match
130
- },
76
+ 'number' => { 'currency' => { 'format' => { 'precision' => '2' } } },
77
+ 'custom' => { 'precision' => 'custom_value' },
131
78
  },
132
79
  })
133
80
  end
@@ -3,13 +3,39 @@ require 'copy_tuner_client/helper_extension'
3
3
  require 'copy_tuner_client/copyray'
4
4
 
5
5
  describe CopyTunerClient::HelperExtension do
6
+ # NOTE: helper_extension が参照する CopyTunerClient::Rails は engine への依存があり
7
+ # 単体 spec では require できないため、メソッドをスタブできる最小の入れ物だけ用意する。
8
+ module CopyTunerClient
9
+ module Rails
10
+ def self.controller_of_rails_engine?(_controller)
11
+ false
12
+ end
13
+ end
14
+ end
15
+
16
+ # NOTE: request.format で描画フォーマットを判定するため、format を差し替えられる
17
+ # 最小のフェイク request / controller を用意する。mailer 判定は controller の型で行う。
18
+ Format = Struct.new(:type) do
19
+ def html?
20
+ type == :html
21
+ end
22
+ end
23
+ Request = Struct.new(:format)
24
+ Controller = Struct.new(:request)
25
+
6
26
  module KeywordArgumentsHelper
27
+ attr_writer :controller
28
+
7
29
  def translate(key, **options)
8
30
  "Hello, #{options[:name]}"
9
31
  end
10
32
 
11
33
  def controller
12
- nil
34
+ return @controller if defined?(@controller)
35
+
36
+ # NOTE: 実 HTML 描画では controller が存在し request.format が html になるため、
37
+ # デフォルトはそれを再現した controller。
38
+ @controller = Controller.new(Request.new(Format.new(:html)))
13
39
  end
14
40
  end
15
41
 
@@ -19,14 +45,67 @@ describe CopyTunerClient::HelperExtension do
19
45
 
20
46
  CopyTunerClient::HelperExtension.hook_translation_helper(KeywordArgumentsHelper, middleware_enabled: true)
21
47
 
48
+ let(:view) { KeywordArgumentsView.new }
49
+
50
+ before do
51
+ # NOTE: controller_of_rails_engine? は ::Rails::Engine への依存があり単体 spec では評価できないため、
52
+ # この spec の関心(注入ガード)に絞って常に false を返すようスタブする。
53
+ allow(CopyTunerClient::Rails).to receive(:controller_of_rails_engine?).and_return(false)
54
+ end
55
+
22
56
  it 'works with keyword argument method' do
23
- view = KeywordArgumentsView.new
24
57
  expect(view.translate('some.key', name: 'World')).to eq '<!--COPYRAY some.key-->Hello, World'
25
58
  end
26
59
 
27
60
  it 'does not inject the overlay marker for a local_first key' do
28
61
  CopyTunerClient.configuration.local_first_key_regexp = /\Aviews\./
29
- view = KeywordArgumentsView.new
30
62
  expect(view.translate('views.foo', name: 'World')).to eq 'Hello, World'
31
63
  end
64
+
65
+ context 'injection guard by request format' do
66
+ it 'injects the marker when request.format is html' do
67
+ expect(view.translate('some.key', name: 'World')).to eq '<!--COPYRAY some.key-->Hello, World'
68
+ end
69
+
70
+ %i[json text csv pdf].each do |format|
71
+ it "does not inject the marker when request.format is :#{format}" do
72
+ view.controller = Controller.new(Request.new(Format.new(format)))
73
+ expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'injection guard by controller' do
79
+ it 'does not inject the marker when rendered by a mailer' do
80
+ stub_const('ActionMailer::Base', Class.new)
81
+ view.controller = ActionMailer::Base.new
82
+ expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
83
+ end
84
+
85
+ it 'does not inject the marker when controller is nil' do
86
+ view.controller = nil
87
+ expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
88
+ end
89
+
90
+ it 'does not inject the marker when the controller has no request' do
91
+ view.controller = Controller.new(nil)
92
+ expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
93
+ end
94
+
95
+ it 'does not raise when ActionMailer is not loaded' do
96
+ hide_const('ActionMailer::Base') if defined?(ActionMailer::Base)
97
+ view.controller = Controller.new(Request.new(Format.new(:html)))
98
+ expect { view.translate('some.key', name: 'World') }.not_to raise_error
99
+ end
100
+ end
101
+
102
+ # NOTE: マーカー注入を抑止する非 HTML 経路でも、default 引数による初期値登録(I18n.t 呼び出し)は
103
+ # 維持されなければならない。注入ガードが初期値登録まで巻き添えで止めていないことを保証する。
104
+ context 'default value registration' do
105
+ it 'registers the default value even when the marker is not injected' do
106
+ view.controller = Controller.new(Request.new(Format.new(:json)))
107
+ expect(I18n).to receive(:t).with('some.key', hash_including(default: 'Default'))
108
+ view.translate('some.key', name: 'World', default: 'Default')
109
+ end
110
+ end
32
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copy_tuner_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SonicGarden
@@ -57,14 +57,14 @@ dependencies:
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '7.1'
60
+ version: '8.0'
61
61
  type: :development
62
62
  prerelease: false
63
63
  version_requirements: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '7.1'
67
+ version: '8.0'
68
68
  - !ruby/object:Gem::Dependency
69
69
  name: rake
70
70
  requirement: !ruby/object:Gem::Requirement
@@ -185,6 +185,7 @@ files:
185
185
  - ".babelrc"
186
186
  - ".eslintrc.js"
187
187
  - ".gitattributes"
188
+ - ".github/dependabot.yml"
188
189
  - ".github/workflows/rspec.yml"
189
190
  - ".gitignore"
190
191
  - ".rspec"
@@ -200,9 +201,8 @@ files:
200
201
  - app/assets/javascripts/copytuner.js
201
202
  - app/assets/stylesheets/copytuner.css
202
203
  - copy_tuner_client.gemspec
203
- - gemfiles/7.1.gemfile
204
- - gemfiles/7.2.gemfile
205
204
  - gemfiles/8.0.gemfile
205
+ - gemfiles/8.1.gemfile
206
206
  - gemfiles/main.gemfile
207
207
  - index.html
208
208
  - lib/copy_tuner_client.rb
@@ -288,14 +288,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
288
288
  requirements:
289
289
  - - ">="
290
290
  - !ruby/object:Gem::Version
291
- version: 3.1.0
291
+ version: 3.3.0
292
292
  required_rubygems_version: !ruby/object:Gem::Requirement
293
293
  requirements:
294
294
  - - ">="
295
295
  - !ruby/object:Gem::Version
296
296
  version: '0'
297
297
  requirements: []
298
- rubygems_version: 3.6.9
298
+ rubygems_version: 4.0.10
299
299
  specification_version: 4
300
300
  summary: Client for the CopyTuner copy management service
301
301
  test_files: []
data/gemfiles/7.2.gemfile DELETED
@@ -1,5 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 7.2"
4
-
5
- gemspec :path => "../"