Module: GettextColumnMapping

Defined in:
lib/gettext_column_mapping.rb,
lib/gettext_column_mapping/tasks.rb,
lib/gettext_column_mapping/mapper.rb,
lib/gettext_column_mapping/parser.rb,
lib/gettext_column_mapping/railtie.rb,
lib/gettext_column_mapping/version.rb,
lib/gettext_column_mapping/initializer.rb,
lib/gettext_column_mapping/parser/yaml.rb,
lib/gettext_column_mapping/parent_level.rb,
lib/gettext_column_mapping/backends/base.rb,
lib/gettext_column_mapping/model_attributes_finder.rb,
lib/gettext_column_mapping/parent_level/attr_methods.rb,
lib/gettext_column_mapping/backends/gettext_i18n_rails.rb,
lib/gettext_column_mapping/backends/gettext_activerecord.rb

Defined Under Namespace

Modules: Backends, ParentLevel, Parser Classes: Initializer, Mapper, ModelAttributesFinder, Railtie, Tasks

Constant Summary collapse

VERSION =
File.read(File.expand_path("../../../VERSION",__FILE__)).chomp
@@mapper =
GettextColumnMapping::Mapper.new

Class Method Summary collapse

Class Method Details

.charsetObject



39
40
41
42
43
44
45
46
# File 'lib/gettext_column_mapping.rb', line 39

def self.charset
  case config.charset.to_s
  when /utf8|utf-8/i
    'UTF-8'
  else
    'UTF-8'
  end
end

.store_model_attributes(options) ⇒ Object

write all found models/columns to a file where GetTexts ruby parser can find them



5
6
7
8
9
10
11
12
# File 'lib/gettext_column_mapping/model_attributes_finder.rb', line 5

def self.store_model_attributes(options)
  file = options[:to] || 'data/model_attributes.rb'
  unless options[:separate_files]
    write_to_unique_file(file,options)
  else
    write_to_separate_files(file,options)
  end
end

.write_to_separate_files(dir, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gettext_column_mapping/model_attributes_finder.rb', line 51

def self.write_to_separate_files(dir,options)
  ModelAttributesFinder.new.find(options).each do |model,column_names|
    file = File.join(dir,model.name.underscore) + ".rb"
    FileUtils.mkdir_p(File.dirname(file))
    File.open(file,'w') do |f|
      f.puts "# coding: utf-8"
      f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
      #all columns namespaced under the model
      column_names.each do |attribute|
        translation = model.gettext_translation_for_attribute_name(attribute)
        f.puts("s_(\"#{translation}\")")
      end

      if GettextColumnMapping.config.use_parent_level
        # Select all classes with parent level
        GettextColumnMapping::ParentLevel.item_config(model.name) do |klass_name,columns,parent_association,parent_key,conditions|
          model = klass_name.constantize
          options_hash = {}
          if conditions
            options_hash.merge!(:conditions => conditions)
          end
          if parent_association
            options_hash.merge!( :include => parent_association)
          end
          model.find_each(options_hash) do |record|
            columns.each do |key|
              f.puts("s_(\"#{record.msgid_for_attribute(key)}\")")
            end
          end
        end
      end

      f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"

    end
  end
end

.write_to_unique_file(file, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gettext_column_mapping/model_attributes_finder.rb', line 14

def self.write_to_unique_file(file,options)
  File.open(file,'w') do |f|
    f.puts "# coding: utf-8"
    f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
    ModelAttributesFinder.new.find(options).each do |model,column_names|

      f.puts("s_(\"#{model.to_s_with_gettext}\")") #!Keep in sync with ActiveRecord::Base.human_name

      #all columns namespaced under the model
      column_names.each do |attribute|
        translation = model.gettext_translation_for_attribute_name(attribute)
        f.puts("s_(\"#{translation}\")")
      end
    end
    if GettextColumnMapping.config.use_parent_level
      # Select all classes with parent level
      GettextColumnMapping::ParentLevel.each_config do |klass_name,columns,parent_association,parent_key,conditions|
        model = klass_name.constantize
        options_hash = {}
        if conditions
          options_hash.merge!(:conditions => conditions)
        end
        if parent_association
          options_hash.merge!( :include => parent_association)
        end
        model.find_each(options_hash) do |record|
          columns.each do |column|
            f.puts("s_(\"#{record.msgid_for_attribute(column)}\")")
          end
        end
      end
    end

    f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
  end
end