Module: Globalize::ActiveRecord::ActMacro

Defined in:
lib/globalize/active_record.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) locale



35
36
37
# File 'lib/globalize/active_record.rb', line 35

def locale
  (defined?(@@locale) && @@locale)
end

- (Object) locale=(locale)



39
40
41
# File 'lib/globalize/active_record.rb', line 39

def locale=(locale)
  @@locale = locale
end

- (Object) translates(*attr_names)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/globalize/active_record.rb', line 43

def translates(*attr_names)
  return if translates?
  options = attr_names.extract_options!

  class_inheritable_accessor :translation_class, :translated_attribute_names
  class_inheritable_writer :required_attributes
  self.translation_class = ActiveRecord.build_translation_class(self, options)
  self.translated_attribute_names = attr_names.map(&:to_sym)

  include InstanceMethods
  extend  ClassMethods, Migration

  after_save :save_translations!
  has_many :translations, :class_name  => translation_class.name,
                          :foreign_key => class_name.foreign_key,
                          :dependent   => :delete_all,
                          :extend      => HasManyExtensions

  named_scope :with_translations, lambda { |locale|
    conditions = required_attributes.map do |attribute|
      "#{quoted_translation_table_name}.#{attribute} IS NOT NULL"
    end
    conditions << "#{quoted_translation_table_name}.locale = ?"
    { :include => :translations, :conditions => [conditions.join(' AND '), locale] }
  }

  attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
end

- (Boolean) translates?

Returns:

  • (Boolean)


72
73
74
# File 'lib/globalize/active_record.rb', line 72

def translates?
  included_modules.include?(InstanceMethods)
end