Module: RailsI18nterface::Sourcefiles

Extended by:
Cache
Defined in:
lib/rails-i18nterface/sourcefiles.rb

Class Method Summary collapse

Methods included from Cache

cache_load, cache_save

Class Method Details

.extract_activerecords(root_dir) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails-i18nterface/sourcefiles.rb', line 54

def self.extract_activerecords(root_dir)
  files = {}
  schema = File.join(root_dir, 'db', 'schema.rb')
  if File.exists? schema
    regex = Regexp.new('\s*create_table\s"([^"]*)[^\n]*\n(.*?)\send\n', Regexp::MULTILINE)
    matchdata = regex.match(File.read(schema))
    while matchdata != nil
      model = matchdata[1].classify.underscore
      files["activerecord.models.#{model}"] = ['db/schema.rb']
      files.merge!(self.extract_attributes(model, matchdata[2]))
      matchdata = regex.match(matchdata.post_match)
    end
  end
  files
end

.extract_attributes(model, txt) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rails-i18nterface/sourcefiles.rb', line 70

def self.extract_attributes(model, txt)
  files = {}
  regex = Regexp.new('\s*t\.[-_0-9a-z]*\s*"([^"]*?)"')
  matchdata = regex.match(txt)
  while matchdata != nil
    attribute = matchdata[1]
    files["activerecord.attributes.#{model}.#{attribute}"] = ['db/schema.rb']
    matchdata = regex.match(matchdata.post_match)
  end
  files
end

.extract_files(root_dir) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails-i18nterface/sourcefiles.rb', line 21

def self.extract_files(root_dir)
  i18n_lookup_pattern = /\b
    (?:I18n\.t|I18n\.translate|t)
    (?:\s|\():?(?:'|")
      (\.?[a-z0-9_\.]+)
    (?:'|")
    /x
  f = {}
  self.files_to_scan(root_dir).reduce(HashWithIndifferentAccess.new) do |files, file|
    f = files.merge! self.populate_keys(root_dir, file, i18n_lookup_pattern)
  end
  f.merge self.extract_activerecords(root_dir)
end

.files_to_scan(root_dir) ⇒ Object



49
50
51
52
# File 'lib/rails-i18nterface/sourcefiles.rb', line 49

def self.files_to_scan(root_dir)
  Dir.glob(File.join(root_dir, '{app,config,lib}', '**', '*.{rb,erb,haml,slim,rhtml}')) +
    Dir.glob(File.join(root_dir, '{public,app/assets}', 'javascripts', '**', '*.{js,coffee}'))
end

.load_files(root_dir) ⇒ Object



8
9
10
11
12
13
# File 'lib/rails-i18nterface/sourcefiles.rb', line 8

def self.load_files(root_dir)
  cachefile = File.join(root_dir, 'tmp', 'translation_strings')
  cache_load cachefile, root_dir do |options|
    RailsI18nterface::Sourcefiles.extract_files options
  end
end

.populate_keys(root_dir, file, pattern) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rails-i18nterface/sourcefiles.rb', line 35

def self.populate_keys(root_dir, file, pattern)
  files = {}
  IO.read(file).scan(pattern).flatten.map(&:to_sym).each do |key|
    path = self.relative_path(file)
    files[key] ||= []
    files[key] << path unless files[key].include?(path)
  end
  files
end

.refresh(root_dir) ⇒ Object



15
16
17
18
19
# File 'lib/rails-i18nterface/sourcefiles.rb', line 15

def self.refresh(root_dir)
  cachefile = File.join(root_dir, 'tmp', 'translation_strings')
  FileUtils.rm cachefile if File.exists? cachefile
  self.load_files(root_dir)
end

.relative_path(file) ⇒ Object



45
46
47
# File 'lib/rails-i18nterface/sourcefiles.rb', line 45

def self.relative_path(file)
  Pathname.new(File.expand_path(file)).relative_path_from(Pathname.new(Rails.root)).to_s
end