Skip to content

Commit 9073658

Browse files
committed
fix tests, cover edge cases
1 parent 6ed821f commit 9073658

13 files changed

Lines changed: 97 additions & 116 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Use `LokaliseManager` v7.0.0.
6+
- Added support for syncing multiple Lokalise projects via `LokaliseRails::GlobalConfig.for_project`, generating scoped rake tasks: `lokalise_rails:<name>:import` and `lokalise_rails:<name>:export` (thanks, [@floriansr](https://github.com/floriansr))
7+
- Project-specific config keys are validated against `LokaliseManager` supported options to prevent typos/unknown settings.
8+
39
## 8.2.0 (01-May-2025)
410

511
* Added `disable_import_task` boolean option. When enabled, the import task is turned off in your project. Defaults to `false`.

Gemfile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ tz_platforms =
1616
group :test do
1717
gem 'dotenv', '~> 3.0'
1818
gem 'rails', '~> 8.1'
19-
gem 'rake', '~> 13.0'
20-
gem 'rspec', '~> 3.6'
21-
gem 'rubocop', '~> 1.0'
22-
gem 'rubocop-performance', '~> 1.5'
23-
gem 'rubocop-rake', '~> 0.6'
24-
gem 'rubocop-rspec', '~> 3.0'
25-
gem 'simplecov', '~> 0.22'
2619
gem 'tzinfo-data', platforms: tz_platforms
2720
gem 'webmock', '~> 3.14'
2821
end

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace :lokalise do
2727
task :install do
2828
puts 'Running bundle install'
2929
sh 'gem update --system'
30-
sh 'bundle'
30+
sh 'bundle install'
3131
end
3232

3333
desc 'Builds the gem'

gemfiles/Gemfile-rails-6-1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,5 @@ group :test do
1616
gem 'dotenv', '~> 2.5'
1717
gem 'mutex_m', '~> 0.3.0'
1818
gem 'rails', '~> 6.1'
19-
gem 'rake', '~> 13.0'
20-
gem 'rspec', '~> 3.6'
21-
gem 'rubocop', '~> 1.0'
22-
gem 'rubocop-performance', '~> 1.5'
23-
gem 'rubocop-rspec', '~> 2.6'
24-
gem 'rubocop-rake', '~> 0.6'
25-
gem 'simplecov', '~> 0.16'
26-
gem 'simplecov-lcov', '~> 0.8'
2719
gem 'webmock', '~> 3.14'
2820
end

gemfiles/Gemfile-rails-7-1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,5 @@ group :test do
1515
gem 'tzinfo-data', platforms: tz_platforms
1616
gem 'dotenv', '~> 3.0'
1717
gem 'rails', '~> 7.1', '< 7.2'
18-
gem 'rake', '~> 13.0'
19-
gem 'rspec', '~> 3.6'
20-
gem 'rubocop', '~> 1.0'
21-
gem 'rubocop-performance', '~> 1.5'
22-
gem 'rubocop-rspec', '~> 2.6'
23-
gem 'rubocop-rake', '~> 0.6'
24-
gem 'simplecov', '~> 0.22'
25-
gem 'simplecov-lcov', '~> 0.8'
2618
gem 'webmock', '~> 3.14'
2719
end

gemfiles/Gemfile-rails-7-2

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,5 @@ group :test do
1515
gem 'tzinfo-data', platforms: tz_platforms
1616
gem 'dotenv', '~> 3.0'
1717
gem 'rails', '~> 7.2'
18-
gem 'rake', '~> 13.0'
19-
gem 'rspec', '~> 3.6'
20-
gem 'rubocop', '~> 1.0'
21-
gem 'rubocop-performance', '~> 1.5'
22-
gem 'rubocop-rspec', '~> 2.6'
23-
gem 'rubocop-rake', '~> 0.6'
24-
gem 'simplecov', '~> 0.22'
25-
gem 'simplecov-lcov', '~> 0.8'
2618
gem 'webmock', '~> 3.14'
2719
end

lib/lokalise_rails/global_config.rb

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,44 @@ module LokaliseRails
44
# Extends `LokaliseManager::GlobalConfig` to provide a global configuration
55
# specific to the LokaliseRails gem in a Rails application.
66
class GlobalConfig < LokaliseManager::GlobalConfig
7+
# Manager-supported config keys (for inline overrides)
8+
MANAGER_CONFIG_KEYS = LokaliseManager::TaskDefinitions::Base::CONFIG_KEYS.freeze
9+
710
class << self
811
attr_writer :disable_export_task, :disable_import_task
912

10-
# Returns whether the export task should be disabled.
11-
#
12-
# Defaults to `false` if not explicitly set.
13-
#
14-
# @return [Boolean] `true` if the export task is disabled, otherwise `false`.
1513
def disable_export_task
1614
@disable_export_task.nil? ? false : @disable_export_task
1715
end
1816

19-
# Returns whether the import task should be disabled.
20-
#
21-
# Defaults to `false` if not explicitly set.
22-
#
23-
# @return [Boolean] `true` if the import task is disabled, otherwise `false`.
2417
def disable_import_task
2518
@disable_import_task.nil? ? false : @disable_import_task
2619
end
2720

28-
# Returns the path to the directory where translation files are stored.
29-
#
30-
# Defaults to `config/locales` under the Rails application root if not explicitly set.
31-
#
32-
# @return [String] Absolute path to the locales directory.
3321
def locales_path
3422
@locales_path || "#{LokaliseRails::Utils.root}/config/locales"
3523
end
3624

37-
# Registers a named project config and auto-generates scoped rake tasks
38-
# (lokalise_rails:<name>:import and lokalise_rails:<name>:export).
25+
# Registers a named project config.
3926
#
40-
# Settings not explicitly set in the block fall back to GlobalConfig
41-
# via LokaliseManager's inline override mechanism, so shared options
42-
# like api_token only need to be set once in the main config block.
43-
#
44-
# @param name [Symbol, String] identifier for the project
45-
# @yield [collector] block to configure project-specific settings
46-
#
47-
# @example
48-
# LokaliseRails::GlobalConfig.for_project(:mobile) do |c|
49-
# c.project_id = ENV['LOKALISE_MOBILE_PROJECT_ID']
50-
# c.locales_path = "#{Rails.root}/config/locales/mobile"
51-
# end
27+
# @param name [Symbol, String]
28+
# @yield [collector]
5229
def for_project(name, &block)
53-
collector = ProjectConfigCollector.new
30+
key = name.to_sym
31+
collector = ProjectConfigCollector.new(MANAGER_CONFIG_KEYS)
5432
block&.call(collector)
55-
projects[name.to_sym] = collector.to_h
33+
projects[key] = collector.to_h
5634
end
5735

58-
# Returns the registry of named project configs.
59-
#
60-
# @return [Hash{Symbol => Hash}]
6136
def projects
6237
@projects ||= {}
6338
end
6439
end
6540

6641
# Collects attribute assignments from a for_project block into a plain hash.
67-
# The hash is passed as inline overrides to LokaliseManager.importer/exporter,
68-
# so any attribute not explicitly set falls back to GlobalConfig automatically.
6942
class ProjectConfigCollector
70-
def initialize
43+
def initialize(allowed_keys)
44+
@allowed_keys = allowed_keys
7145
@settings = {}
7246
end
7347

@@ -76,16 +50,21 @@ def to_h
7650
end
7751

7852
def method_missing(name, *args)
79-
attr = name.to_s
80-
if attr.end_with?('=')
81-
@settings[attr.chomp('=').to_sym] = args.first
82-
else
83-
super
84-
end
53+
method = name.to_s
54+
return super unless method.end_with?('=')
55+
56+
key = method.chomp('=').to_sym
57+
raise ArgumentError, "Unknown config key for for_project: #{key}" unless @allowed_keys.include?(key)
58+
59+
@settings[key] = args.first
8560
end
8661

8762
def respond_to_missing?(name, include_private = false)
88-
name.to_s.end_with?('=') || super
63+
method = name.to_s
64+
return super unless method.end_with?('=')
65+
66+
key = method.chomp('=').to_sym
67+
@allowed_keys.include?(key) || super
8968
end
9069
end
9170

lib/tasks/lokalise_rails_tasks.rake

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,58 @@ require File.join(LokaliseRails::Utils.root, 'config', 'lokalise_rails')
66

77
# Rake tasks for syncing translation files between a Rails project and Lokalise.
88
namespace :lokalise_rails do
9-
##########################################################################
10-
# IMPORT
11-
##########################################################################
12-
desc 'Import translations from Lokalise into the Rails project'
13-
task :import do
9+
run_import = lambda do |opts, project_name: nil|
1410
if LokaliseRails::GlobalConfig.disable_import_task
1511
$stdout.puts 'Import task is disabled.'
1612
exit 0
1713
end
1814

19-
importer = LokaliseManager.importer({}, LokaliseRails::GlobalConfig)
20-
importer.import!
15+
LokaliseManager.importer(opts, LokaliseRails::GlobalConfig).import!
2116
rescue StandardError => e
22-
abort "Import failed: #{e.message}"
17+
name = project_name&.to_s
18+
prefix = name && !name.empty? ? "[#{name}] " : ''
19+
abort "#{prefix}Import failed: #{e.message}"
2320
end
2421

25-
##########################################################################
26-
# EXPORT
27-
##########################################################################
28-
desc 'Export translations from the Rails project to Lokalise'
29-
task :export do
22+
run_export = lambda do |opts, project_name: nil|
3023
if LokaliseRails::GlobalConfig.disable_export_task
3124
$stdout.puts 'Export task is disabled.'
3225
exit 0
3326
end
3427

35-
exporter = LokaliseManager.exporter({}, LokaliseRails::GlobalConfig)
36-
exporter.export!
28+
LokaliseManager.exporter(opts, LokaliseRails::GlobalConfig).export!
3729
rescue StandardError => e
38-
abort "Export failed: #{e.message}"
30+
name = project_name&.to_s
31+
prefix = name && !name.empty? ? "[#{name}] " : ''
32+
abort "#{prefix}Export failed: #{e.message}"
33+
end
34+
35+
##########################################################################
36+
# DEFAULT (GLOBAL) TASKS
37+
##########################################################################
38+
desc 'Import translations from Lokalise into the Rails project'
39+
task :import do
40+
run_import.call({})
41+
end
42+
43+
desc 'Export translations from the Rails project to Lokalise'
44+
task :export do
45+
run_export.call({})
3946
end
4047

41-
# Auto-generate scoped import/export tasks for each named project
42-
# registered via LokaliseRails::GlobalConfig.for_project.
48+
##########################################################################
49+
# SCOPED TASKS (PER PROJECT)
50+
##########################################################################
4351
LokaliseRails::GlobalConfig.projects.each do |project_name, project_opts|
44-
namespace project_name do
52+
namespace project_name.to_s do
4553
desc "Import translations from Lokalise (#{project_name})"
4654
task :import do
47-
importer = LokaliseManager.importer(project_opts, LokaliseRails::GlobalConfig)
48-
importer.import!
49-
rescue StandardError => e
50-
abort "Import failed: #{e.message}"
55+
run_import.call(project_opts, project_name: project_name)
5156
end
5257

5358
desc "Export translations to Lokalise (#{project_name})"
5459
task :export do
55-
exporter = LokaliseManager.exporter(project_opts, LokaliseRails::GlobalConfig)
56-
exporter.export!
57-
rescue StandardError => e
58-
abort "Export failed: #{e.message}"
60+
run_export.call(project_opts, project_name: project_name)
5961
end
6062
end
6163
end

lokalise_rails.gemspec

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@ Gem::Specification.new do |spec|
1717
spec.files = Dir['README.md', 'LICENSE.md',
1818
'CHANGELOG.md', 'lib/**/*.rb',
1919
'lib/**/*.rake',
20-
'lokalise_rails.gemspec', '.github/*.md',
21-
'Gemfile', 'Rakefile']
20+
'lokalise_rails.gemspec']
2221
spec.extra_rdoc_files = ['README.md']
2322
spec.require_paths = ['lib']
2423

25-
spec.add_dependency 'lokalise_manager', '~> 6.5'
24+
spec.add_dependency 'lokalise_manager', '~> 7.0'
2625
spec.add_dependency 'zeitwerk', '~> 2.4'
2726

27+
spec.add_development_dependency 'rake', '~> 13.0'
28+
spec.add_development_dependency 'rspec', '~> 3.13'
29+
spec.add_development_dependency 'rubocop', '~> 1.0'
30+
spec.add_development_dependency 'rubocop-performance', '~> 1.5'
31+
spec.add_development_dependency 'rubocop-rake', '~> 0.7'
32+
spec.add_development_dependency 'rubocop-rspec', '~> 3.0'
33+
spec.add_development_dependency 'simplecov', '~> 0.22'
34+
2835
spec.metadata = {
29-
'rubygems_mfa_required' => 'true'
36+
'rubygems_mfa_required' => 'true',
37+
'bug_tracker_uri' => 'https://github.com/bodrovis/lokalise_rails/issues',
38+
'changelog_uri' => 'https://github.com/bodrovis/lokalise_rails/blob/master/CHANGELOG.md',
39+
'documentation_uri' => 'https://github.com/bodrovis/lokalise_rails/blob/master/README.md',
40+
'homepage_uri' => spec.homepage
3041
}
3142
end

spec/dummy/config/application.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ class Application < Rails::Application
3636
config.load_defaults 6.1
3737
elsif Rails.gem_version < Gem::Version.new('8.0')
3838
config.load_defaults 7.0
39-
else
39+
elsif Rails.gem_version < Gem::Version.new('8.1')
4040
config.load_defaults 8.0
41+
else
42+
config.load_defaults 8.1
4143
end
4244
end
4345
end

0 commit comments

Comments
 (0)