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



73
74
75
76
77
78
79
80
81
# File 'lib/globalize/model/active_record/translated.rb', line 73

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

#construct_finder_sql_with_globalize2(options) ⇒ Object



122
123
124
125
126
# File 'lib/globalize/model/active_record/translated.rb', line 122

def construct_finder_sql_with_globalize2(options)
  sql = construct_finder_sql_without_globalize2(options)
  sql.sub! /SELECT(\s+DISTINCT)?/, 'SELECT DISTINCT'
  sql
end

#create_translation_table!(fields) ⇒ Object



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

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.integer self.name.gsub(/.+::/, '').foreign_key
    t.string :locale
    fields.each do |name, type|
      t.column name, type
    end
    t.timestamps              
  end
end

#drop_translation_table!Object



107
108
109
110
# File 'lib/globalize/model/active_record/translated.rb', line 107

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

#find_every_with_globalize2(options) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/globalize/model/active_record/translated.rb', line 112

def find_every_with_globalize2(options)
  locale  = I18n.locale
  locales = I18n.fallbacks[locale].map{ |tag| tag.to_s }
  scope_options = { :include => :globalize_translations,
    :conditions => [ "#{i18n_attr('locale')} IN (?)", locales ] }
  with_scope(:find => scope_options) do
    find_every_without_globalize2(options)
  end
end