nulogy-gettext_i18n_rails 0.4.6.1 → 0.4.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,14 +5,7 @@ module GettextI18nRails
5
5
  begin
6
6
  File.open(file,'w') do |f|
7
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
8
+ ModelAttributesFinder.new.find(options).each do |model,column_names|
16
9
  f.puts("_('#{model.human_name_without_translation}')")
17
10
 
18
11
  #all columns namespaced under the model
@@ -37,72 +30,82 @@ module GettextI18nRails
37
30
  # :ignore_columns => ['id',/_id$/,...]
38
31
  # current connection ---> {'cars'=>['model_name','type'],...}
39
32
  def find(options)
40
- found = Hash.new([])
33
+ found = ActiveSupport::OrderedHash.new([])
41
34
 
42
- connection = ::ActiveRecord::Base.connection
43
- connection.tables.each do |table_name|
35
+ models.each do |model|
36
+ table_name = model.table_name
44
37
  next if ignored?(table_name,options[:ignore_tables])
45
- connection.columns(table_name).each do |column|
46
- found[table_name] += [column.name] unless ignored?(column.name,options[:ignore_columns])
38
+ model.columns.each do |column|
39
+ found[model] += [column.name] unless ignored?(column.name,options[:ignore_columns])
47
40
  end
41
+ found[model].sort!
48
42
  end
49
43
 
50
44
  found
51
45
  end
52
46
 
47
+ def models
48
+ if Rails.respond_to?(:application)
49
+ Rails.application.eager_load! # make sure that all models are loaded so that direct_descendants works
50
+ ::ActiveRecord::Base.direct_descendants
51
+ else
52
+ ::ActiveRecord::Base.connection.tables.map {|t| table_name_to_namespaced_model(t) }
53
+ end.compact.sort {|c1, c2| c1.name <=> c2.name}
54
+ end
55
+
53
56
  def ignored?(name,patterns)
54
57
  return false unless patterns
55
58
  patterns.detect{|p|p.to_s==name.to_s or (p.is_a?(Regexp) and name=~p)}
56
59
  end
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
60
 
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)
61
+ private
62
+ # Tries to find the model class corresponding to specified table name.
63
+ # Takes into account that the model can be defined in a namespace.
64
+ # Searches only up to one level deep - won't find models nested in two
65
+ # or more modules.
66
+ #
67
+ # Note that if we allow namespaces, the conversion can be ambiguous, i.e.
68
+ # if the table is named "aa_bb_cc" and AaBbCc, Aa::BbCc and AaBb::Cc are
69
+ # all defined there's no absolute rule that tells us which one to use.
70
+ # This method prefers the less nested one and, if there are two at
71
+ # the same level, the one with shorter module name.
72
+ def table_name_to_namespaced_model(table_name)
73
+ # First assume that there are no namespaces
74
+ model = to_class(table_name.singularize.camelcase)
83
75
  return model if model != nil
84
76
 
85
- underscore_position = table_name.index('_', underscore_position + 1)
86
- end
77
+ # If you were wrong, assume that the model is in a namespace.
78
+ # Iterate over the underscores and try to substitute each of them
79
+ # for a slash that camelcase() replaces with the scope operator (::).
80
+ underscore_position = table_name.index('_')
81
+ while underscore_position != nil
82
+ namespaced_table_name = table_name.dup
83
+ namespaced_table_name[underscore_position] = '/'
84
+ model = to_class(namespaced_table_name.singularize.camelcase)
85
+ return model if model != nil
87
86
 
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
87
+ underscore_position = table_name.index('_', underscore_position + 1)
88
+ end
92
89
 
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
90
+ # The model either is not defined or is buried more than one level
91
+ # deep in a module hierarchy
103
92
  return nil
104
93
  end
105
94
 
106
- return constant.is_a?(Class) ? constant : nil
95
+ # Checks if there is a class of specified name and if so, returns
96
+ # the class object. Otherwise returns nil.
97
+ def to_class(name)
98
+ # I wanted to use Module.const_defined?() here to avoid relying
99
+ # on exceptions for normal program flow but it's of no use.
100
+ # If class autoloading is enabled, the constant may be undefined
101
+ # but turn out to be present when we actually try to use it.
102
+ begin
103
+ constant = name.constantize
104
+ rescue NameError
105
+ return nil
106
+ end
107
+
108
+ return constant.is_a?(Class) ? constant : nil
109
+ end
107
110
  end
108
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nulogy-gettext_i18n_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 117
4
+ hash: 115
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
9
  - 6
10
- - 1
11
- version: 0.4.6.1
10
+ - 2
11
+ version: 0.4.6.2
12
12
  platform: ruby
13
13
  authors:
14
14
  - Michael Grosser