dugway 1.0.14 → 1.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/.bundle/config +2 -0
  3. data/.github/workflows/main.yml +1 -1
  4. data/.gitignore +1 -0
  5. data/README.md +9 -0
  6. data/lib/dugway/application.rb +5 -3
  7. data/lib/dugway/assets/big_cartel_logo.svg +4 -0
  8. data/lib/dugway/cli/build.rb +18 -1
  9. data/lib/dugway/cli/server.rb +2 -2
  10. data/lib/dugway/cli/templates/source/settings.json +8 -0
  11. data/lib/dugway/cli/validate.rb +20 -2
  12. data/lib/dugway/controller.rb +5 -1
  13. data/lib/dugway/liquid/drops/account_drop.rb +4 -0
  14. data/lib/dugway/liquid/drops/features_drop.rb +144 -0
  15. data/lib/dugway/liquid/drops/product_drop.rb +8 -0
  16. data/lib/dugway/liquid/drops/products_drop.rb +1 -1
  17. data/lib/dugway/liquid/drops/related_products_drop.rb +88 -0
  18. data/lib/dugway/liquid/drops/theme_drop.rb +23 -0
  19. data/lib/dugway/liquid/drops/translations_drop.rb +122 -0
  20. data/lib/dugway/liquifier.rb +44 -8
  21. data/lib/dugway/store.rb +7 -2
  22. data/lib/dugway/theme.rb +169 -3
  23. data/lib/dugway/version.rb +1 -1
  24. data/lib/dugway.rb +31 -1
  25. data/locales/storefront.de.yml +79 -0
  26. data/locales/storefront.en-CA.yml +79 -0
  27. data/locales/storefront.en-GB.yml +79 -0
  28. data/locales/storefront.en-US.yml +79 -0
  29. data/locales/storefront.es-ES.yml +79 -0
  30. data/locales/storefront.es-MX.yml +79 -0
  31. data/locales/storefront.fr-CA.yml +79 -0
  32. data/locales/storefront.fr-FR.yml +79 -0
  33. data/locales/storefront.id.yml +79 -0
  34. data/locales/storefront.it.yml +79 -0
  35. data/locales/storefront.ja.yml +79 -0
  36. data/locales/storefront.ko.yml +79 -0
  37. data/locales/storefront.nl.yml +79 -0
  38. data/locales/storefront.pl.yml +79 -0
  39. data/locales/storefront.pt-BR.yml +79 -0
  40. data/locales/storefront.pt-PT.yml +79 -0
  41. data/locales/storefront.ro.yml +79 -0
  42. data/locales/storefront.sv.yml +79 -0
  43. data/locales/storefront.tr.yml +79 -0
  44. data/locales/storefront.zh-CN.yml +79 -0
  45. data/locales/storefront.zh-TW.yml +79 -0
  46. data/log/dugway.log +1 -0
  47. data/spec/features/page_rendering_spec.rb +4 -4
  48. data/spec/fixtures/theme/layout.html +2 -0
  49. data/spec/fixtures/theme/settings.json +6 -0
  50. data/spec/spec_helper.rb +4 -0
  51. data/spec/units/dugway/liquid/drops/features_drop_spec.rb +182 -0
  52. data/spec/units/dugway/liquid/drops/product_drop_spec.rb +36 -0
  53. data/spec/units/dugway/liquid/drops/related_products_drop_spec.rb +80 -0
  54. data/spec/units/dugway/liquid/drops/theme_drop_spec.rb +45 -0
  55. data/spec/units/dugway/liquid/drops/translations_drop_spec.rb +292 -0
  56. data/spec/units/dugway/store_spec.rb +37 -0
  57. data/spec/units/dugway/theme_spec.rb +456 -0
  58. metadata +35 -2
@@ -0,0 +1,292 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dugway::Drops::TranslationsDrop do
4
+ let(:settings) { {} }
5
+ let(:definitions) { {} }
6
+ let(:drop) { Dugway::Drops::TranslationsDrop.new(settings, definitions) }
7
+
8
+ before(:all) do
9
+ # Store original I18n settings to restore them later
10
+ @original_i18n_load_path = I18n.load_path.dup
11
+ @original_i18n_available_locales = I18n.config.available_locales.dup
12
+ @original_i18n_default_locale = I18n.default_locale
13
+ @original_i18n_locale = I18n.locale
14
+
15
+ # Create a unique temporary directory for theme-specific locale files for this test run
16
+ @theme_parent_dir = File.expand_path("../../../../tmp/translations_drop_spec_#{Time.now.to_f}", __FILE__)
17
+ @theme_source_dir = File.join(@theme_parent_dir, 'source') # For Dugway.source_dir mock
18
+ @theme_locales_dir = File.join(@theme_parent_dir, '_dugway_locales') # Theme locale files go here
19
+
20
+ FileUtils.mkdir_p(@theme_source_dir)
21
+ FileUtils.mkdir_p(@theme_locales_dir)
22
+
23
+ # Write theme-specific test files. These will override or supplement gem locales.
24
+ # Using 'navigation.cart' as a key that exists in actual gem locales (e.g., storefront.en-US.yml)
25
+ # and 'farewell' as a theme-only key.
26
+ # We use 'en-US' for the theme file to directly override the gem's 'en-US' translations.
27
+ File.write(File.join(@theme_locales_dir, 'storefront.en-US.yml'), <<~YAML)
28
+ en-US:
29
+ storefront:
30
+ navigation:
31
+ cart: "Theme Cart" # Override gem's "Cart"
32
+ farewell: "Goodbye Theme" # Theme-only
33
+ YAML
34
+ File.write(File.join(@theme_locales_dir, 'storefront.fr.yml'), <<~YAML)
35
+ fr:
36
+ storefront:
37
+ navigation:
38
+ cart: "Panier du Thème"
39
+ YAML
40
+
41
+ # Force load gem locales first (mimicking initial gem load)
42
+ # The main lib/dugway.rb (required via spec_helper) should have already configured
43
+ # I18n.load_path and I18n.config.available_locales with the gem's actual files.
44
+ # We are capturing this initial state and then augmenting it for theme-specific test files.
45
+
46
+ @theme_locale_files = Dir.glob(File.join(@theme_locales_dir, 'storefront.*.yml'))
47
+
48
+ # Augment the load path: original gem paths + new theme paths.
49
+ # Theme files are added last so their definitions override gem's if keys conflict.
50
+ I18n.load_path = (@original_i18n_load_path + @theme_locale_files).uniq
51
+
52
+ # Augment available locales: original gem locales + new theme locales.
53
+ theme_locale_codes = @theme_locale_files.map do |file|
54
+ name = File.basename(file, '.yml')
55
+ locale_code = name.match(/^storefront\.(.+)$/)&.captures&.first
56
+ locale_code&.to_sym
57
+ end.compact
58
+
59
+ current_available_locales = (@original_i18n_available_locales.to_a + theme_locale_codes).uniq
60
+
61
+ if current_available_locales.any? { |loc| loc.to_s.start_with?('en-') } && !current_available_locales.include?(:en)
62
+ current_available_locales << :en
63
+ end
64
+
65
+ I18n.config.available_locales = current_available_locales
66
+
67
+ # Force a clean slate for translations before reloading.
68
+ # This ensures that the load path order is strictly respected for overrides.
69
+ I18n.backend.send(:init_translations) if I18n.backend.respond_to?(:init_translations, true)
70
+
71
+ # Reload the backend to process all files in the (now augmented) I18n.load_path
72
+ # and to recognize all locales in I18n.config.available_locales.
73
+ I18n.backend.reload!
74
+
75
+ # Verify that :'en-US' (which should come from the gem's original setup) is recognized.
76
+ # If this fails, it means :'en-US' was not in @original_i18n_available_locales or was lost.
77
+ unless I18n.config.available_locales.include?(:'en-US') && I18n.backend.available_locales.include?(:'en-US')
78
+ error_message = "I18n Setup Issue: :en-US is not available after setup.\n"
79
+ error_message += "I18n.config.available_locales: #{I18n.config.available_locales.inspect}\n"
80
+ error_message += "I18n.backend.available_locales: #{I18n.backend.available_locales.inspect}\n"
81
+ error_message += "I18n.load_path: #{I18n.load_path.inspect}\n"
82
+ error_message += "@original_i18n_available_locales: #{@original_i18n_available_locales.inspect}\n"
83
+ error_message += "@original_i18n_load_path: #{@original_i18n_load_path.inspect}"
84
+ raise error_message
85
+ end
86
+
87
+ I18n.default_locale = :'en-US'
88
+ I18n.locale = :'en-US'
89
+ end
90
+
91
+ after(:all) do
92
+ # Clean up temporary theme directory
93
+ FileUtils.rm_rf(@theme_parent_dir)
94
+
95
+ # Restore original I18n settings
96
+ I18n.load_path = @original_i18n_load_path
97
+ I18n.config.available_locales = @original_i18n_available_locales
98
+ I18n.default_locale = @original_i18n_default_locale
99
+ I18n.locale = @original_i18n_locale
100
+ I18n.backend.reload!
101
+ end
102
+ let(:logger_double) { double('Logger', warn: nil) }
103
+
104
+ before(:each) do
105
+ allow(Dugway).to receive(:gem_root).and_return(File.expand_path('../../../../../', __FILE__))
106
+ allow(Dugway).to receive(:source_dir).and_return(@theme_source_dir)
107
+
108
+ allow(Dugway).to receive(:logger).and_return(logger_double)
109
+ end
110
+
111
+ describe 'Automatic Mode (Default)' do
112
+ context "with default language ('en-US', falling back to 'en' for theme)" do
113
+ # These tests rely on I18n.locale being 'en-US' (set in before(:all))
114
+ # The theme provides 'storefront.en.yml'
115
+ it "returns theme override for a key also in gem locales" do
116
+ # Gem 'en-US' (locales/storefront.en-US.yml) has storefront.navigation.cart: "Cart"
117
+ # Theme 'en' (temporary file) has storefront.navigation.cart: "Theme Cart"
118
+ expect(drop['navigation.cart']).to eq('Theme Cart')
119
+ end
120
+
121
+ it "returns theme-only translation if present" do
122
+ # 'farewell' is only in the temporary theme 'en' file
123
+ expect(drop['farewell']).to eq('Goodbye Theme')
124
+ end
125
+
126
+ it "returns gem translation if not in theme" do
127
+ # 'navigation.all_products' is in gem 'en-US.yml', not in our test theme 'en.yml'
128
+ # The value from locales/storefront.en-US.yml is "All products"
129
+ expect(drop['navigation.all_products']).to eq('All products')
130
+ end
131
+
132
+ it "returns missing translation message for unknown key" do
133
+ expect(drop['unknown.key']).to eq('[MISSING TRANSLATION: unknown.key]')
134
+ end
135
+
136
+ it "logs a warning for missing translation with the correct locale" do
137
+ expect(Dugway.logger).to receive(:warn).with("Missing translation: key='unknown.key.again' locale='en-US' mode='automatic'")
138
+ drop['unknown.key.again']
139
+ end
140
+ end
141
+
142
+ context "with specified locale ('fr')" do
143
+ let(:settings) { { translation_locale: 'fr' } }
144
+
145
+ it "returns translation for the specified language from theme" do
146
+ # Theme 'fr' (temporary file) has storefront.navigation.cart: "Panier du Thème"
147
+ expect(drop['navigation.cart']).to eq('Panier du Thème')
148
+ end
149
+
150
+ it "returns missing translation message if key not found in specified language (and not in fallbacks)" do
151
+ # 'farewell' is not in theme 'fr.yml' or any 'fr' gem locale
152
+ expect(drop['farewell']).to eq('[MISSING TRANSLATION: farewell]')
153
+ end
154
+
155
+ it "returns gem translation if key not found in specified theme language but exists in gem's version of that language" do
156
+ # Assuming 'locales/storefront.fr-FR.yml' (loaded by gem) has 'navigation.all': 'Tous'
157
+ # and our temporary theme 'fr.yml' does not have 'navigation.all'
158
+ # This test depends on the content of the actual gem's fr-FR file.
159
+ # For a more robust test, ensure 'locales/storefront.fr-FR.yml' has 'navigation: { all: "Tous" }'
160
+ # Or, if 'fr-FR' is not a primary test target, this test could be more generic or skipped.
161
+ # For now, let's assume 'navigation.all' exists in a loaded 'fr' variant from the gem.
162
+ # If 'locales/storefront.fr-FR.yml' exists and is loaded:
163
+ # expect(drop['navigation.all']).to eq(I18n.t('storefront.navigation.all', locale: :fr))
164
+ # If not, it will be missing. Let's test for missing if not in theme 'fr'.
165
+ expect(drop['navigation.all_products']).to eq('[MISSING TRANSLATION: navigation.all_products]')
166
+ end
167
+
168
+ it "logs a warning with the correct locale" do
169
+ expect(Dugway.logger).to receive(:warn).with("Missing translation: key='farewell.again' locale='fr' mode='automatic'")
170
+ drop['farewell.again']
171
+ end
172
+ end
173
+
174
+ context "with specified non-existent locale ('xx')" do
175
+ let(:settings) { { translation_locale: 'xx' } }
176
+
177
+ it "returns missing translation message for a known key" do
178
+ expect(drop['navigation.cart']).to eq('[MISSING TRANSLATION: navigation.cart]')
179
+ end
180
+ end
181
+ end
182
+
183
+ describe 'Manual Mode' do
184
+ let(:settings) do
185
+ {
186
+ translation_mode: 'manual',
187
+ greeting_tr_text: 'Hello Manual',
188
+ 'nested_message_tr_text' => 'Manual Nested',
189
+ farewell_tr_text: '',
190
+ explicit_nil_tr_text: nil
191
+ }
192
+ end
193
+
194
+ it "returns translation from manual setting (symbol key)" do
195
+ expect(drop['greeting']).to eq('Hello Manual')
196
+ end
197
+
198
+ it "returns translation from manual setting (string key)" do
199
+ # Note: The drop converts the lookup key 'nested.message' to :nested_message_tr_text
200
+ # but the settings hash might have string keys from JSON parsing.
201
+ # The drop implementation checks both symbol and string keys in the settings hash.
202
+ expect(drop['nested.message']).to eq('Manual Nested')
203
+ end
204
+
205
+ it "falls back to automatic translation if manual setting is blank and allow_blank is false (default)" do
206
+ # No definition provided, so allow_blank defaults to false
207
+ # 'farewell' exists in theme 'en' locale (storefront.en.yml created by test)
208
+ # I18n.locale is 'en-US'. The drop will use this locale.
209
+ expect(I18n).to receive(:t).with('storefront.farewell', locale: 'en-US', default: nil).and_call_original
210
+ expect(drop['farewell']).to eq('Goodbye Theme')
211
+ end
212
+
213
+ it "returns missing translation message if manual setting is explicitly nil" do
214
+ # If the key exists in manual settings but the value is nil, treat it as missing.
215
+ expect(drop['explicit_nil']).to eq('[MISSING TRANSLATION: explicit_nil]')
216
+ end
217
+
218
+ it "returns missing translation message if manual setting is not present" do
219
+ expect(drop['unknown.key']).to eq('[MISSING TRANSLATION: unknown.key]')
220
+ end
221
+
222
+ it "logs a warning for missing translation (manual mode)" do
223
+ # Expect a call to the stubbed logger
224
+ expect(Dugway.logger).to receive(:warn).with("Missing translation: key='unknown.key.manual' mode='manual'")
225
+ drop['unknown.key.manual']
226
+ end
227
+
228
+ it "does not use I18n backend if a valid manual setting is found" do
229
+ expect(I18n).not_to receive(:t)
230
+ expect(drop['greeting']).to eq('Hello Manual') # Uses manual setting
231
+ end
232
+
233
+ it "uses I18n backend if manual setting is blank and allow_blank is false" do
234
+ expect(I18n).to receive(:t).with('storefront.farewell', locale: 'en-US', default: nil).and_call_original
235
+ drop['farewell'] # Trigger lookup for blank, disallowed value
236
+ end
237
+
238
+ it "uses I18n backend if manual setting key is not found" do
239
+ expect(I18n).to receive(:t).with('storefront.unknown.key', locale: 'en-US', default: nil).and_call_original
240
+ drop['unknown.key'] # Trigger lookup for missing key
241
+ end
242
+
243
+ context "with allow_blank definitions" do
244
+ let(:definitions) do
245
+ {
246
+ 'options' => [
247
+ { 'variable' => 'allow_blank_true_tr_text', 'allow_blank' => true },
248
+ { 'variable' => 'allow_blank_false_tr_text', 'allow_blank' => false },
249
+ { 'variable' => 'allow_blank_missing_tr_text' }, # allow_blank defaults to false
250
+ { 'variable' => 'allow_blank_non_blank_val_tr_text', 'allow_blank' => false }
251
+ ]
252
+ }
253
+ end
254
+ let(:settings) do
255
+ {
256
+ translation_mode: 'manual',
257
+ allow_blank_true_tr_text: '',
258
+ allow_blank_false_tr_text: '',
259
+ allow_blank_missing_tr_text: '',
260
+ allow_blank_non_blank_val_tr_text: 'Not Blank'
261
+ }
262
+ end
263
+
264
+ it "returns blank string when allow_blank is true" do
265
+ expect(drop['allow_blank_true']).to eq('')
266
+ end
267
+
268
+ it "falls back to automatic translation when allow_blank is false" do
269
+ # Assuming 'allow_blank_false' doesn't exist in automatic translations
270
+ expect(I18n).to receive(:t).with('storefront.allow_blank_false', locale: 'en-US', default: nil).and_return(nil)
271
+ expect(drop['allow_blank_false']).to eq('[MISSING TRANSLATION: allow_blank_false]')
272
+ end
273
+
274
+ it "falls back to automatic translation when allow_blank is missing (defaults to false)" do
275
+ # Assuming 'allow_blank_missing' doesn't exist in automatic translations
276
+ expect(I18n).to receive(:t).with('storefront.allow_blank_missing', locale: 'en-US', default: nil).and_return(nil)
277
+ expect(drop['allow_blank_missing']).to eq('[MISSING TRANSLATION: allow_blank_missing]')
278
+ end
279
+
280
+ it "returns non-blank value even when allow_blank is false" do
281
+ expect(drop['allow_blank_non_blank_val']).to eq('Not Blank')
282
+ end
283
+ end
284
+ end
285
+
286
+ describe '#derive_manual_setting_key' do
287
+ # Test the private method indirectly via its usage or directly if needed
288
+ it 'converts dot notation to underscore symbol notation' do
289
+ expect(drop.send(:derive_manual_setting_key, 'some.key.name')).to eq(:some_key_name_tr_text)
290
+ end
291
+ end
292
+ end
@@ -189,4 +189,41 @@ describe Dugway::Store do
189
189
  store.locale.should == 'en-US'
190
190
  end
191
191
  end
192
+
193
+ describe "#website" do
194
+ context "when website is provided in store_options" do
195
+ let(:store_options) { { website: 'https://my-custom-override.com' } }
196
+ let(:store) { Dugway::Store.new('dugway', store_options) }
197
+
198
+ it "returns the website from store_options" do
199
+ store.website.should == 'https://my-custom-override.com'
200
+ end
201
+ end
202
+
203
+ context "when website is not in store_options but in account data" do
204
+ let(:store) { Dugway::Store.new('dugway', {}) } # No override
205
+
206
+ before do
207
+ # Mock the account data to include a website
208
+ allow(store).to receive(:account).and_return({ 'website' => 'https://api-provided-site.com' })
209
+ end
210
+
211
+ it "returns the website from account data" do
212
+ store.website.should == 'https://api-provided-site.com'
213
+ end
214
+ end
215
+
216
+ context "when website is in neither store_options nor account data" do
217
+ let(:store) { Dugway::Store.new('dugway', {}) } # No override
218
+
219
+ before do
220
+ # Mock the account data to NOT include a website
221
+ allow(store).to receive(:account).and_return({})
222
+ end
223
+
224
+ it "returns nil" do
225
+ store.website.should be_nil
226
+ end
227
+ end
228
+ end
192
229
  end