Skip to content

Commit

Permalink
feat: Add config option to ignore keys (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilka2 authored Jun 14, 2024
1 parent e6e3fbf commit 657b9f3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
config.account_id = "<%= options[:account_id] %>"
config.datacenter = "eu"

# Configure an array of key names that should not be handled
# by the In-Context-Editor.
# config.ignored_keys = ["number.*", "foo.bar"]

# Phrase uses decorators to generate a unique identification key
# in context of your document. However, this might result in conflicts
# with other libraries (e.g. client-side template engines) that use a similar syntax.
Expand Down
4 changes: 4 additions & 0 deletions lib/phraseapp-in-context-editor-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def datacenter
config.datacenter
end

def ignored_keys
config.ignored_keys
end

def enabled=(value)
config.enabled = value
end
Expand Down
11 changes: 8 additions & 3 deletions lib/phraseapp-in-context-editor-ruby/backend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module PhraseApp
module InContextEditor
class BackendService
attr_accessor :blacklisted_keys

def initialize(args = {})
self
end
Expand All @@ -20,7 +18,14 @@ def translate(...)
protected

def to_be_translated_without_phraseapp?(...)
PhraseApp::InContextEditor.disabled? || has_been_forced_to_resolve_with_phraseapp?(...)
PhraseApp::InContextEditor.disabled? || ignored_key?(...) || has_been_forced_to_resolve_with_phraseapp?(...)
end

def ignored_key?(*args)
key = given_key_from_args(args)
PhraseApp::InContextEditor.ignored_keys.any? do |ignored_key|
key.to_s[/\A#{ignored_key.gsub("*", ".*")}\Z/]
end
end

def has_been_forced_to_resolve_with_phraseapp?(*args)
Expand Down
3 changes: 2 additions & 1 deletion lib/phraseapp-in-context-editor-ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Config
backend: PhraseApp::InContextEditor::BackendService.new,
prefix: "{{__",
suffix: "__}}",
origin: "in-context-editor-ruby"
origin: "in-context-editor-ruby",
ignored_keys: []
}.freeze

CONFIG_OPTIONS.each do |option, default_value|
Expand Down
33 changes: 33 additions & 0 deletions spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@
end
end

describe "#ignored_key?" do
let(:key_name) { "bar" }
subject { phraseapp_service.send(:ignored_key?, key_name, scope: "foo") }

before do
PhraseApp::InContextEditor.config.ignored_keys = ignored_keys
end

context "exact key is ignored" do
let(:ignored_keys) { %w[foo.bar baz] }

it { is_expected.to be_truthy }
end

context "key with wildcard is ignored" do
let(:ignored_keys) { %w[foo.* baz] }

it { is_expected.to be_truthy }
end

context "no keys are ignored" do
let(:ignored_keys) { [] }

it { is_expected.to be_falsey }
end

context "different keys are ignored" do
let(:ignored_keys) { %w[baz baz.foo bar.* foo] }

it { is_expected.to be_falsey }
end
end

describe "#normalized_key" do
subject { phraseapp_service.send(:normalized_key, args) }

Expand Down

0 comments on commit 657b9f3

Please sign in to comment.