phraseapp-in-context-editor-ruby 1.4.0 → 3.2.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.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +18 -0
  3. data/.github/workflows/close_inactive_issues.yml +18 -0
  4. data/.github/workflows/release.yml +36 -0
  5. data/.github/workflows/test.yml +8 -9
  6. data/.gitignore +1 -1
  7. data/.tool-versions +1 -1
  8. data/CHANGELOG.md +28 -79
  9. data/CODEOWNERS +1 -0
  10. data/Gemfile +1 -1
  11. data/Gemfile.lock +244 -0
  12. data/Guardfile +13 -13
  13. data/README.md +96 -51
  14. data/RELEASING.md +3 -0
  15. data/docs/CHANGELOG.md +116 -0
  16. data/examples/demo/.gitattributes +7 -0
  17. data/examples/demo/.gitignore +35 -0
  18. data/examples/demo/Gemfile +74 -0
  19. data/examples/demo/Gemfile.lock +268 -0
  20. data/examples/demo/README.md +5 -0
  21. data/examples/demo/Rakefile +6 -0
  22. data/examples/demo/app/assets/config/manifest.js +4 -0
  23. data/examples/demo/app/assets/images/.keep +0 -0
  24. data/examples/demo/app/assets/stylesheets/application.css +15 -0
  25. data/examples/demo/app/channels/application_cable/channel.rb +4 -0
  26. data/examples/demo/app/channels/application_cable/connection.rb +4 -0
  27. data/examples/demo/app/controllers/application_controller.rb +2 -0
  28. data/examples/demo/app/controllers/concerns/.keep +0 -0
  29. data/examples/demo/app/controllers/main_controller.rb +4 -0
  30. data/examples/demo/app/helpers/application_helper.rb +2 -0
  31. data/examples/demo/app/helpers/main_helper.rb +2 -0
  32. data/examples/demo/app/javascript/application.js +3 -0
  33. data/examples/demo/app/javascript/controllers/application.js +9 -0
  34. data/examples/demo/app/javascript/controllers/hello_controller.js +7 -0
  35. data/examples/demo/app/javascript/controllers/index.js +11 -0
  36. data/examples/demo/app/jobs/application_job.rb +7 -0
  37. data/examples/demo/app/mailers/application_mailer.rb +4 -0
  38. data/examples/demo/app/models/application_record.rb +3 -0
  39. data/examples/demo/app/models/concerns/.keep +0 -0
  40. data/examples/demo/app/views/layouts/application.html.erb +17 -0
  41. data/examples/demo/app/views/layouts/mailer.html.erb +13 -0
  42. data/examples/demo/app/views/layouts/mailer.text.erb +1 -0
  43. data/examples/demo/app/views/main/index.html.erb +8 -0
  44. data/examples/demo/bin/bundle +109 -0
  45. data/examples/demo/bin/importmap +4 -0
  46. data/examples/demo/bin/rails +4 -0
  47. data/examples/demo/bin/rake +4 -0
  48. data/examples/demo/bin/setup +33 -0
  49. data/examples/demo/config/application.rb +22 -0
  50. data/examples/demo/config/boot.rb +4 -0
  51. data/examples/demo/config/cable.yml +10 -0
  52. data/examples/demo/config/credentials.yml.enc +1 -0
  53. data/examples/demo/config/database.yml +25 -0
  54. data/examples/demo/config/environment.rb +5 -0
  55. data/examples/demo/config/environments/development.rb +70 -0
  56. data/examples/demo/config/environments/production.rb +93 -0
  57. data/examples/demo/config/environments/test.rb +60 -0
  58. data/examples/demo/config/importmap.rb +7 -0
  59. data/examples/demo/config/initializers/assets.rb +12 -0
  60. data/examples/demo/config/initializers/content_security_policy.rb +25 -0
  61. data/examples/demo/config/initializers/filter_parameter_logging.rb +8 -0
  62. data/examples/demo/config/initializers/inflections.rb +16 -0
  63. data/examples/demo/config/initializers/permissions_policy.rb +11 -0
  64. data/examples/demo/config/initializers/phraseapp_in_context_editor.rb +20 -0
  65. data/examples/demo/config/locales/en.yml +38 -0
  66. data/examples/demo/config/puma.rb +43 -0
  67. data/examples/demo/config/routes.rb +6 -0
  68. data/examples/demo/config/storage.yml +34 -0
  69. data/examples/demo/config.ru +6 -0
  70. data/examples/demo/db/seeds.rb +7 -0
  71. data/examples/demo/lib/assets/.keep +0 -0
  72. data/examples/demo/lib/tasks/.keep +0 -0
  73. data/examples/demo/log/.keep +0 -0
  74. data/examples/demo/public/404.html +67 -0
  75. data/examples/demo/public/422.html +67 -0
  76. data/examples/demo/public/500.html +66 -0
  77. data/examples/demo/public/apple-touch-icon-precomposed.png +0 -0
  78. data/examples/demo/public/apple-touch-icon.png +0 -0
  79. data/examples/demo/public/favicon.ico +0 -0
  80. data/examples/demo/public/robots.txt +1 -0
  81. data/examples/demo/storage/.keep +0 -0
  82. data/examples/demo/test/application_system_test_case.rb +5 -0
  83. data/examples/demo/test/channels/application_cable/connection_test.rb +11 -0
  84. data/examples/demo/test/controllers/.keep +0 -0
  85. data/examples/demo/test/controllers/main_controller_test.rb +8 -0
  86. data/examples/demo/test/fixtures/files/.keep +0 -0
  87. data/examples/demo/test/helpers/.keep +0 -0
  88. data/examples/demo/test/integration/.keep +0 -0
  89. data/examples/demo/test/mailers/.keep +0 -0
  90. data/examples/demo/test/models/.keep +0 -0
  91. data/examples/demo/test/system/.keep +0 -0
  92. data/examples/demo/test/test_helper.rb +13 -0
  93. data/examples/demo/tmp/.keep +0 -0
  94. data/examples/demo/tmp/pids/.keep +0 -0
  95. data/examples/demo/tmp/storage/.keep +0 -0
  96. data/examples/demo/vendor/.keep +0 -0
  97. data/examples/demo/vendor/javascript/.keep +0 -0
  98. data/lib/generators/phraseapp_in_context_editor/install_generator.rb +3 -9
  99. data/lib/generators/phraseapp_in_context_editor/templates/phraseapp_in_context_editor.rb +10 -14
  100. data/lib/phraseapp-in-context-editor-ruby/adapters/fast_gettext.rb +20 -20
  101. data/lib/phraseapp-in-context-editor-ruby/adapters/i18n.rb +10 -10
  102. data/lib/phraseapp-in-context-editor-ruby/backend_service.rb +23 -125
  103. data/lib/phraseapp-in-context-editor-ruby/config.rb +14 -49
  104. data/lib/phraseapp-in-context-editor-ruby/delegate/fast_gettext.rb +13 -15
  105. data/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb +6 -52
  106. data/lib/phraseapp-in-context-editor-ruby/delegate.rb +13 -14
  107. data/lib/phraseapp-in-context-editor-ruby/engine.rb +5 -6
  108. data/lib/phraseapp-in-context-editor-ruby/version.rb +2 -4
  109. data/lib/phraseapp-in-context-editor-ruby/view_helpers.rb +26 -19
  110. data/lib/phraseapp-in-context-editor-ruby.rb +17 -48
  111. data/package-lock.json +6576 -0
  112. data/package.json +13 -0
  113. data/phraseapp-in-context-editor-ruby.gemspec +19 -32
  114. data/release.config.js +10 -0
  115. metadata +113 -97
  116. data/lib/generators/phraseapp_in_context_editor/templates/README +0 -10
  117. data/lib/phraseapp-in-context-editor-ruby/api_collection.rb +0 -41
  118. data/lib/phraseapp-in-context-editor-ruby/api_wrapper.rb +0 -59
  119. data/lib/phraseapp-in-context-editor-ruby/cache.rb +0 -37
  120. data/lib/phraseapp-in-context-editor-ruby/displayable_key_identifier.rb +0 -46
  121. data/lib/phraseapp-in-context-editor-ruby/fallback_keys_fetcher.rb +0 -44
  122. data/lib/phraseapp-in-context-editor-ruby/hash_flattener.rb +0 -53
  123. data/lib/phraseapp-in-context-editor-ruby/key_names_cache.rb +0 -53
@@ -1,24 +1,16 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
- require 'phraseapp-in-context-editor-ruby/cache'
4
- require 'phraseapp-in-context-editor-ruby/hash_flattener'
5
- require 'phraseapp-in-context-editor-ruby/delegate'
6
- require 'phraseapp-in-context-editor-ruby/api_wrapper'
7
- require 'phraseapp-in-context-editor-ruby/displayable_key_identifier'
8
-
9
- require 'set'
1
+ require_relative "../delegate"
10
2
 
11
3
  module PhraseApp
12
4
  module InContextEditor
13
5
  module Delegate
14
6
  class I18nDelegate < Base
15
- attr_accessor :key, :display_key, :options, :api_client, :fallback_keys, :original_args
7
+ attr_accessor :key, :display_key
16
8
 
17
- def initialize(key, options={}, original_args=nil)
9
+ def initialize(key, options = {}, original_args = nil)
18
10
  @key = key
19
11
  @options = options
20
12
  @original_args = original_args
21
- @display_key = DisplayableKeyIdentifier.new(api_wrapper).identify(@key, @options)
13
+ @display_key = @key
22
14
  super(decorated_key_name)
23
15
  end
24
16
 
@@ -27,54 +19,16 @@ module PhraseApp
27
19
  if @key.respond_to?(args.first)
28
20
  to_s.send(*args)
29
21
  else
30
- data = translation_or_subkeys
22
+ data = [@key]
31
23
  if data.respond_to?(args.first)
32
24
  data.send(*args, &block)
33
25
  else
34
26
  self.class.log "You tried to execute the missing method ##{args.first} on key #{@key} which is not supported. Please make sure you treat your translations as strings only."
35
- original_translation = ::I18n.translate_without_phraseapp(*@original_args)
27
+ original_translation = ::I18n.translate_without_phraseapp(*@original_args, **@options)
36
28
  original_translation.send(*args, &block)
37
29
  end
38
30
  end
39
31
  end
40
-
41
- private
42
-
43
- def translation_or_subkeys
44
- return nil if @key.nil? || @key.to_s == ""
45
-
46
- keys = api_wrapper.keys_with_prefix(@key)
47
- return nil unless keys.present?
48
-
49
- if keys.size == 1
50
- translation_content_for_key(keys.first)
51
- else
52
- translation_hash = keys.inject({}) do |hash, key|
53
- hash[key.name] = translation_content_for_key(key)
54
- hash
55
- end
56
-
57
- PhraseApp::InContextEditor::HashFlattener.expand_flat_hash(translation_hash, @key)
58
- end
59
- end
60
-
61
- def translation_content_for_key(key)
62
- default_translations = api_wrapper.default_translation(key)
63
- return unless default_translations.present?
64
-
65
- if key.plural
66
- default_translations.inject({}) do |hash, translation|
67
- hash[translation.plural_suffix.to_sym] = translation.content
68
- hash
69
- end
70
- else
71
- default_translations.first.content
72
- end
73
- end
74
-
75
- def api_wrapper
76
- @api_wrapper ||= InContextEditor::ApiWrapper.new
77
- end
78
32
  end
79
33
  end
80
34
  end
@@ -1,17 +1,15 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  module PhraseApp
4
2
  module InContextEditor
5
3
  module Delegate
6
4
  class Base < String
7
5
  def to_s
8
- "#{decorated_key_name}"
6
+ decorated_key_name.to_s
9
7
  end
10
- alias :camelize :to_s
11
- alias :underscore :to_s
12
- alias :classify :to_s
13
- alias :dasherize :to_s
14
- alias :tableize :to_s
8
+ alias_method :camelize, :to_s
9
+ alias_method :underscore, :to_s
10
+ alias_method :classify, :to_s
11
+ alias_method :dasherize, :to_s
12
+ alias_method :tableize, :to_s
15
13
 
16
14
  # Delegate .html_safe from accessing Delegate object as if it was a hash,
17
15
  # but instead perform it on the resulting string
@@ -19,23 +17,24 @@ module PhraseApp
19
17
 
20
18
  def self.log(message)
21
19
  message = "phrase: #{message}"
22
- if defined?(Rails) and Rails.respond_to?(:logger)
20
+ if defined?(Rails) && Rails.respond_to?(:logger)
23
21
  Rails.logger.warn(message)
24
22
  else
25
- $stderr.puts message
23
+ warn message
26
24
  end
27
25
  end
28
26
 
29
- protected
27
+ protected
28
+
30
29
  def decorated_key_name
31
30
  "#{PhraseApp::InContextEditor.prefix}phrase_#{normalized_display_key}#{PhraseApp::InContextEditor.suffix}"
32
31
  end
33
32
 
34
33
  def normalized_display_key
35
- unless @display_key.nil?
36
- @display_key.gsub("<", "[[[[[[html_open]]]]]]").gsub(">", "[[[[[[html_close]]]]]]")
37
- else
34
+ if @display_key.nil?
38
35
  @display_key
36
+ else
37
+ @display_key.gsub("<", "[[[[[[html_open]]]]]]").gsub(">", "[[[[[[html_close]]]]]]")
39
38
  end
40
39
  end
41
40
  end
@@ -1,14 +1,13 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'phraseapp-in-context-editor-ruby'
3
- require 'i18n'
1
+ require "phraseapp-in-context-editor-ruby"
2
+ require "i18n"
4
3
 
5
4
  if defined? Rails
6
5
  module PhraseApp
7
6
  module InContextEditor
8
7
  class Engine < Rails::Engine
9
- initializer 'phraseapp-in-context-editor-ruby', before: :disable_dependency_loading do |app|
10
- require 'phraseapp-in-context-editor-ruby/adapters/i18n'
11
- require 'phraseapp-in-context-editor-ruby/adapters/fast_gettext'
8
+ initializer "phraseapp-in-context-editor-ruby", before: :disable_dependency_loading do |app|
9
+ require "phraseapp-in-context-editor-ruby/adapters/i18n"
10
+ require "phraseapp-in-context-editor-ruby/adapters/fast_gettext"
12
11
 
13
12
  ActionView::Base.send :include, PhraseApp::InContextEditor::ViewHelpers
14
13
  end
@@ -1,5 +1,3 @@
1
- module PhraseApp
2
- module InContextEditor
3
- VERSION = "1.4.0"
4
- end
1
+ module PhraseappInContextEditor
2
+ VERSION = "3.2.0"
5
3
  end
@@ -1,35 +1,42 @@
1
- require 'json'
1
+ require "json"
2
2
 
3
3
  module PhraseApp
4
4
  module InContextEditor
5
5
  module ViewHelpers
6
- def phraseapp_in_context_editor_js(opts={})
6
+ def load_in_context_editor(opts = {})
7
7
  return "" unless PhraseApp::InContextEditor.enabled?
8
8
 
9
9
  # stringify to reduce possible errors when passing symbols
10
- js_default_options = PhraseApp::InContextEditor.js_options.inject({}) { |conf, (k,v)| conf[k.to_s] = v; conf}
11
- opts = opts.nil? ? {} : opts.inject({}) { |conf, (k,v)| conf[k.to_s] = v; conf}
10
+ opts = opts.nil? ? {} : opts.each_with_object({}) { |(k, v), conf|
11
+ conf[k.to_s] = v
12
+ }
12
13
 
13
14
  # js options
14
15
  configuration = {
15
- 'projectId' => PhraseApp::InContextEditor.project_id,
16
- 'prefix' => PhraseApp::InContextEditor.prefix,
17
- 'suffix' => PhraseApp::InContextEditor.suffix,
18
- 'apiBaseUrl' => "#{PhraseApp::InContextEditor.api_host}/api/v2",
19
- }.merge(js_default_options).merge(opts)
16
+ "projectId" => PhraseApp::InContextEditor.project_id,
17
+ "accountId" => PhraseApp::InContextEditor.account_id,
18
+ "datacenter" => PhraseApp::InContextEditor.datacenter,
19
+ "prefix" => PhraseApp::InContextEditor.prefix,
20
+ "suffix" => PhraseApp::InContextEditor.suffix,
21
+ "origin" => PhraseApp::InContextEditor.origin
22
+ }.merge(opts)
20
23
 
21
- snippet = <<-eos
22
- <script>
23
- window.PHRASEAPP_CONFIG = #{configuration.to_json};
24
- (function() {
25
- var phraseapp = document.createElement('script'); phraseapp.type = 'text/javascript'; phraseapp.async = true;
26
- phraseapp.src = ['#{PhraseApp::InContextEditor.js_use_ssl ? 'https' : 'http'}://', '#{PhraseApp::InContextEditor.js_host}#{PhraseApp::InContextEditor.js_path}?', new Date().getTime()].join('');
27
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(phraseapp, s);
28
- })();
29
- </script>
30
- eos
24
+ snippet = <<-EOS
25
+ <script nonce='#{content_security_policy_nonce}'>
26
+ window.PHRASEAPP_CONFIG = #{configuration.to_json};
27
+ (function() {
28
+ let phraseapp = document.createElement('script');
29
+ phraseapp.type = 'module';
30
+ phraseapp.async = true;
31
+ phraseapp.src = "https://d2bgdldl6xit7z.cloudfront.net/latest/ice/index.js";
32
+ let script = document.getElementsByTagName('script')[0];
33
+ script.parentNode.insertBefore(phraseapp, script);
34
+ })();
35
+ </script>
36
+ EOS
31
37
  snippet.respond_to?(:html_safe) ? snippet.html_safe : snippet
32
38
  end
39
+ alias_method :phraseapp_in_context_editor_js, :load_in_context_editor
33
40
  end
34
41
  end
35
42
  end
@@ -1,10 +1,8 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'phraseapp-ruby'
3
- require 'request_store'
1
+ require "request_store"
4
2
 
5
3
  module PhraseApp
6
4
  module InContextEditor
7
- autoload :Config, 'phraseapp-in-context-editor-ruby/config'
5
+ autoload :Config, "phraseapp-in-context-editor-ruby/config"
8
6
 
9
7
  class << self
10
8
  def config
@@ -31,20 +29,12 @@ module PhraseApp
31
29
  config.project_id
32
30
  end
33
31
 
34
- def access_token
35
- config.access_token
32
+ def account_id
33
+ config.account_id
36
34
  end
37
35
 
38
- def skip_ssl_verification
39
- config.skip_ssl_verification
40
- end
41
-
42
- def cache_key_segments_initial
43
- config.cache_key_segments_initial
44
- end
45
-
46
- def cache_lifetime
47
- config.cache_lifetime
36
+ def datacenter
37
+ config.datacenter
48
38
  end
49
39
 
50
40
  def ignored_keys
@@ -63,28 +53,8 @@ module PhraseApp
63
53
  !config.enabled
64
54
  end
65
55
 
66
- def js_use_ssl
67
- config.js_use_ssl
68
- end
69
-
70
- def js_host
71
- config.js_host
72
- end
73
-
74
- def js_path
75
- config.js_path
76
- end
77
-
78
- def js_options
79
- config.js_options
80
- end
81
-
82
- def api_host
83
- config.api_host
84
- end
85
-
86
- def api_client
87
- config.api_client
56
+ def origin
57
+ config.origin
88
58
  end
89
59
  end
90
60
 
@@ -92,8 +62,8 @@ module PhraseApp
92
62
  yield(PhraseApp::InContextEditor::Config)
93
63
  end
94
64
 
95
- def self.with_config(config_options={}, &block)
96
- original_config = self.config.dup
65
+ def self.with_config(config_options = {}, &block)
66
+ original_config = config.dup
97
67
  config.assign_values(config_options)
98
68
  yield
99
69
  ensure
@@ -101,17 +71,16 @@ module PhraseApp
101
71
  end
102
72
  end
103
73
 
104
- autoload :ViewHelpers, 'phraseapp-in-context-editor-ruby/view_helpers'
74
+ autoload :ViewHelpers, "phraseapp-in-context-editor-ruby/view_helpers"
105
75
 
106
- require 'phraseapp-in-context-editor-ruby/version'
107
- require 'phraseapp-in-context-editor-ruby/engine'
108
- require 'phraseapp-in-context-editor-ruby/delegate'
109
- require 'phraseapp-in-context-editor-ruby/backend_service'
110
- require 'phraseapp-in-context-editor-ruby/view_helpers'
76
+ require "phraseapp-in-context-editor-ruby/engine"
77
+ require "phraseapp-in-context-editor-ruby/delegate"
78
+ require "phraseapp-in-context-editor-ruby/backend_service"
79
+ require "phraseapp-in-context-editor-ruby/view_helpers"
111
80
  end
112
81
 
113
82
  # Only load adapters directly if non-rails app, otherwise use engine
114
83
  unless defined? Rails
115
- require 'phraseapp-in-context-editor-ruby/adapters/i18n'
116
- require 'phraseapp-in-context-editor-ruby/adapters/fast_gettext'
84
+ require "phraseapp-in-context-editor-ruby/adapters/i18n"
85
+ require "phraseapp-in-context-editor-ruby/adapters/fast_gettext"
117
86
  end