gettext_i18n_rails 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -210,6 +210,7 @@ lib/tasks/gettext.rake:
210
210
  - [ed0h](http://github.com/ed0h)
211
211
  - [Nikos Dimitrakopoulos](http://blog.nikosd.com)
212
212
  - [Ben Tucker](http://btucker.net/)
213
+ - [Kamil Śliwak](https://github.com/cameel)
213
214
 
214
215
  [Michael Grosser](http://grosser.it)<br/>
215
216
  grosser.michael@gmail.com<br/>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gettext_i18n_rails}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-12-01}
12
+ s.date = %q{2011-12-04}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.files = [
15
15
  "Gemfile",
@@ -2,25 +2,31 @@ module GettextI18nRails
2
2
  #write all found models/columns to a file where GetTexts ruby parser can find them
3
3
  def store_model_attributes(options)
4
4
  file = options[:to] || 'locale/model_attributes.rb'
5
- File.open(file,'w') do |f|
6
- f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
7
- ModelAttributesFinder.new.find(options).each do |table_name,column_names|
8
- #model name
9
- begin
10
- model = table_name.singularize.camelcase.constantize
11
- rescue NameError
12
- # Some tables are not models, for example: translation tables created by globalize2.
13
- next
14
- end
15
- f.puts("_('#{model.human_name_without_translation}')")
16
-
17
- #all columns namespaced under the model
18
- column_names.each do |attribute|
19
- translation = model.gettext_translation_for_attribute_name(attribute)
20
- f.puts("_('#{translation}')")
5
+ begin
6
+ File.open(file,'w') do |f|
7
+ f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
8
+ ModelAttributesFinder.new.find(options).each do |table_name,column_names|
9
+ #model name
10
+ model = table_name_to_namespaced_model(table_name)
11
+ if model == nil
12
+ # Some tables are not models, for example: translation tables created by globalize2.
13
+ puts "[Warning] Model not found for table '#{table_name}'"
14
+ next
15
+ end
16
+ f.puts("_('#{model.human_name_without_translation}')")
17
+
18
+ #all columns namespaced under the model
19
+ column_names.each do |attribute|
20
+ translation = model.gettext_translation_for_attribute_name(attribute)
21
+ f.puts("_('#{translation}')")
22
+ end
21
23
  end
24
+ f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
22
25
  end
23
- f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
26
+ rescue
27
+ puts "[Error] Attribute extraction failed. Removing incomplete file (#{file})"
28
+ File.delete(file)
29
+ raise
24
30
  end
25
31
  end
26
32
  module_function :store_model_attributes
@@ -49,4 +55,54 @@ module GettextI18nRails
49
55
  patterns.detect{|p|p.to_s==name.to_s or (p.is_a?(Regexp) and name=~p)}
50
56
  end
51
57
  end
58
+
59
+ private
60
+ # Tries to find the model class corresponding to specified table name.
61
+ # Takes into account that the model can be defined in a namespace.
62
+ # Searches only up to one level deep - won't find models nested in two
63
+ # or more modules.
64
+ #
65
+ # Note that if we allow namespaces, the conversion can be ambiguous, i.e.
66
+ # if the table is named "aa_bb_cc" and AaBbCc, Aa::BbCc and AaBb::Cc are
67
+ # all defined there's no absolute rule that tells us which one to use.
68
+ # This method prefers the less nested one and, if there are two at
69
+ # the same level, the one with shorter module name.
70
+ def table_name_to_namespaced_model(table_name)
71
+ # First assume that there are no namespaces
72
+ model = to_class(table_name.singularize.camelcase)
73
+ return model if model != nil
74
+
75
+ # If you were wrong, assume that the model is in a namespace.
76
+ # Iterate over the underscores and try to substitute each of them
77
+ # for a slash that camelcase() replaces with the scope operator (::).
78
+ underscore_position = table_name.index('_')
79
+ while underscore_position != nil
80
+ namespaced_table_name = table_name.dup
81
+ namespaced_table_name[underscore_position] = '/'
82
+ model = to_class(namespaced_table_name.singularize.camelcase)
83
+ return model if model != nil
84
+
85
+ underscore_position = table_name.index('_', underscore_position + 1)
86
+ end
87
+
88
+ # The model either is not defined or is buried more than one level
89
+ # deep in a module hierarchy
90
+ return nil
91
+ end
92
+
93
+ # Checks if there is a class of specified name and if so, returns
94
+ # the class object. Otherwise returns nil.
95
+ def to_class(name)
96
+ # I wanted to use Module.const_defined?() here to avoid relying
97
+ # on exceptions for normal program flow but it's of no use.
98
+ # If class autoloading is enabled, the constant may be undefined
99
+ # but turn out to be present when we actually try to use it.
100
+ begin
101
+ constant = name.constantize
102
+ rescue NameError
103
+ return nil
104
+ end
105
+
106
+ return constant.is_a?(Class) ? constant : nil
107
+ end
52
108
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext_i18n_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-01 00:00:00 -08:00
18
+ date: 2011-12-04 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency