Class: Globalize::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/globalize/utils.rb

Class Method Summary collapse

Class Method Details

.convert_columns(value) ⇒ Object

Convert string “title:string” to hash { :title => :string } Convert array [“title:string”, “content:text”] to hash { :title => :string, :content => :text }



133
134
135
136
137
138
139
# File 'lib/globalize/utils.rb', line 133

def convert_columns(value)
  [value].flatten.inject({}) do |hash, schema|
    arr = schema.to_s.split(':')
    hash[arr.first.to_sym] = (arr.size == 1 ? :text : arr.last.to_sym)
    hash
  end
end

.get_model_class(file) ⇒ Object

Retrieve the classes belonging to the model names we’re asked to process Check for namespaced models in subdirectories as well as models in subdirectories without namespacing.



48
49
50
51
52
53
54
55
56
57
# File 'lib/globalize/utils.rb', line 48

def get_model_class(file)
  require File.expand_path("#{model_dir}/#{file}") # this is for non-rails projects, which don't get Rails auto-require magic
  model = file.gsub(/\.rb$/, '').camelize
  parts = model.split('::')
  begin
    parts.inject(Object) {|klass, part| klass.const_get(part) }
  rescue LoadError, NameError
    Object.const_get(parts.last)
  end
end

.get_model_filesObject

Return a list of the model files to translate. If we have command line arguments, they’re assumed to be either the underscore or CamelCase versions of model names. Otherwise we take all the model files in the model_dir directory.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/globalize/utils.rb', line 28

def get_model_files
  models = ARGV.dup
  models.shift
  models.reject!{|m| m.match(/^(.*)=/)}
  if models.empty?
    begin
      Dir.chdir(model_dir) do
        models = Dir["**/*.rb"]
      end
    rescue SystemCallError
      puts "No models found in directory '#{model_dir}'."
      exit 1;
    end
  end
  models
end

.init(kind) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/globalize/utils.rb', line 115

def init(kind)
  get_model_files.each do |file|
    begin
      klass = get_model_class(file)
      if klass < ::ActiveRecord::Base && !klass.abstract_class? && klass.respond_to?(:translated_attribute_names)
        case kind
          when :up then make_up(klass)
          when :down then make_down(klass)
        end
      end
    rescue Exception => e
      puts "Unable to #{kind} #{file}: #{e.inspect}"
    end
  end
end

.make_down(klass) ⇒ Object



108
109
110
111
112
113
# File 'lib/globalize/utils.rb', line 108

def make_down(klass)
  if klass.connection.table_exists?(klass.translations_table_name)
    klass.drop_translation_table!
    show_log("drop table", klass.translations_table_name)
  end
end

.make_parent_checkers(klass, connect) ⇒ Object

create or delete parent model fields, such as is_locale_Globalize.locale



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/globalize/utils.rb', line 60

def make_parent_checkers(klass, connect)
  columns = connect.columns(klass.table_name)
  
  Globalize.available_locales.each do |locale|
    name = "is_locale_#{locale}"
    
    unless columns.map(&:name).include?(name)
      connect.add_column klass.table_name, name, :boolean, :default => false
      show_log("add column", klass.table_name, name, :boolean)
    end
  end
end

.make_up(klass) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/globalize/utils.rb', line 73

def make_up(klass)
  conn = klass.connection
  table_name = klass.translations_table_name
  
  make_parent_checkers(klass, conn)
      
  if conn.table_exists?(table_name) # translated table exits
    columns = conn.columns(table_name)
    
    klass.translated_columns_hash.each do |key, value|
      columns.each do |column|
        if column.name.to_sym == key && column.type != value
          conn.change_column table_name, key, value
          show_log("change column", table_name, key, value)
        end
      end
      
      unless columns.map(&:name).include?(key.to_s)
        conn.add_column table_name, key, value
        show_log("add column", table_name, key, value)
      end
    end
    
    columns.each do |column|
      if !klass.translated_attribute_names.include?(column.name.to_sym) && [:string, :text].include?(column.type) && column.name != "locale"
        conn.remove_column table_name, column.name
        show_log("remove column", table_name, column.name)
      end
    end
  else
    klass.create_translation_table!(klass.translated_columns_hash)
    show_log("create table", table_name)
  end
end

.model_dirObject



5
6
7
# File 'lib/globalize/utils.rb', line 5

def model_dir
  @model_dir || "app/models"
end

.model_dir=(dir) ⇒ Object



9
10
11
# File 'lib/globalize/utils.rb', line 9

def model_dir=(dir)
  @model_dir = dir
end

.show_log(action, table_name, name = false, type = false) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/globalize/utils.rb', line 13

def show_log(action, table_name, name=false, type=false)
  log = []
  log << action
  log << ' ' + table_name.to_s
  log << '.' + name.to_s if name
  log << ' as ' + type.to_s if type
  
  puts log.join
end