Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add config option to ignore keys #82

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out so it doesn't alter current behavior.


# 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet another attr_acessor that did nothing.


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
2 changes: 1 addition & 1 deletion lib/phraseapp-in-context-editor-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module PhraseApp
VERSION = "2.1.0"
VERSION = "3.1.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this isn't getting incremented during the release process.

end
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
Loading