Module: Globalize::Model::ActiveRecord::Translated::ClassMethods

Defined in:
lib/globalize/model/active_record/translated.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/globalize/model/active_record/translated.rb', line 70

def method_missing(method, *args)
  if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym)
    find(:first, :joins => :globalize_translations,
         :conditions => [ "#{i18n_attr($1)} = ? AND #{i18n_attr('locale')} IN (?)",
                         args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}])
  else
    super
  end
end

Instance Method Details

#create_translation_table!(fields) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/globalize/model/active_record/translated.rb', line 80

def create_translation_table!(fields)
  translated_fields = self.globalize_options[:translated_attributes]
  translated_fields.each do |f|
    raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
  end
  fields.each do |name, type|
    unless translated_fields.member? name 
      raise UntranslatedMigrationField, "Can't migrate untranslated field: #{name}"
    end              
    unless [ :string, :text ].member? type
      raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
    end 
  end
  translation_table_name = self.name.underscore.gsub('/', '_') + '_translations'
  self.connection.create_table(translation_table_name) do |t|
    t.references self.table_name.singularize
    t.string :locale
    fields.each do |name, type|
      t.column name, type
    end
    t.timestamps              
  end
end

#drop_translation_table!Object



104
105
106
107
# File 'lib/globalize/model/active_record/translated.rb', line 104

def drop_translation_table!
  translation_table_name = self.name.underscore.gsub('/', '_') + '_translations'
  self.connection.drop_table translation_table_name
end