Skip to content

Commit

Permalink
Fix up relative controller keys PR #113
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Dec 17, 2014
1 parent 9349168 commit ef159b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 3 additions & 4 deletions lib/i18n/tasks/scanners/pattern_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ def controller_file?(path)
end

def closest_method(location)
path = location[:src_path]
line_index = (location[:line_num] - 1)
method = File.readlines(path)[0, line_index].reverse.grep(/def/).first
method.strip.sub(/^def\s*/,"")
method = File.readlines(location[:src_path]).first(location[:line_num] - 1).reverse_each.find { |x| x=~ /\bdef\b/ }
method &&= method.strip.sub(/^def\s*/, '')
method
end

def pattern
Expand Down
15 changes: 9 additions & 6 deletions lib/i18n/tasks/scanners/relative_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ def absolutize_key(key, path, roots = relative_roots, closest_method = "")

private

# Detect the appropriate relative path root
# @param [String] path /full/path
# @param [Array<String>] roots array of full paths
# @return [String] the closest ancestor root for path
def path_root(path, roots)
@path_root ||=

This comment has been minimized.

Copy link
@glebm

glebm Dec 17, 2014

Author Owner

Removed caching because the method has two arguments. This would be easier to cache if we refactored the scanner a bit. Added sort.reverse_each to always return the closest path root.

This comment has been minimized.

Copy link
@jessieay

jessieay Dec 18, 2014

Collaborator

🆒

expanded_relative_roots(roots).detect do |root|
path.start_with?(root + '/')
end
expanded_relative_roots(roots).sort.reverse_each.detect do |root|
path.start_with?(root + '/')
end
end

def expanded_relative_roots(roots)
Expand All @@ -38,8 +41,8 @@ def prefix_key_based_on_path(key, normalized_path, roots, options = {})
def prefix(normalized_path, roots, options = {})
file_name = normalized_path.gsub(%r(#{path_root(normalized_path, roots)}/|(\.[^/]+)*$), '')

if /controllers/.match(normalized_path)
"#{file_name.split("_").first}.#{options[:closest_method]}"
if options[:closest_method].present?
"#{file_name.split('_').first}.#{options[:closest_method]}"
else
file_name.tr('/', '.').gsub(%r(\._), '.')
end
Expand Down
7 changes: 3 additions & 4 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
describe 'missing' do
let (:expected_missing_keys) {
%w( en.used_but_missing.key
en.relative.index.missing
es.missing_in_es.a
en.present_in_es_but_not_en.a
en.hash.pattern_missing.a
Expand All @@ -37,10 +38,8 @@
}
it 'detects missing' do
capture_stderr do
# TODO - figure out why expectation below is failing. Has to do with
# changes in `absolutize_key` method
# expect(run_cmd :missing).to be_i18n_keys expected_missing_keys
es_keys = expected_missing_keys.grep(/^es\./)
expect(run_cmd :missing).to be_i18n_keys expected_missing_keys
# locale argument
expect(run_cmd :missing, locales: %w(es)).to be_i18n_keys es_keys
expect(run_cmd :missing, arguments: %w(es)).to be_i18n_keys es_keys
Expand All @@ -57,7 +56,7 @@
end

let(:expected_unused_keys) do
%w(unused.a unused.numeric unused.plural relative.index.title relative.index.description relative.index.summary).map do |k|
%w(unused.a unused.numeric unused.plural).map do |k|
%w(en es).map { |l| "#{l}.#{k}" }
end.reduce(:+)
end
Expand Down

0 comments on commit ef159b1

Please sign in to comment.