Module: Globalize::ActiveRecord::ActMacro

Defined in:
lib/globalize/active_record/act_macro.rb

Instance Method Summary collapse

Instance Method Details

#class_nameObject



63
64
65
66
67
68
# File 'lib/globalize/active_record/act_macro.rb', line 63

def class_name
  @class_name ||= begin
    class_name = table_name[table_name_prefix.length..-(table_name_suffix.length + 1)].downcase.camelize
    pluralize_table_names ? class_name.singularize : class_name
  end
end

#translates(*attr_names) ⇒ Object



4
5
6
7
8
9
10
11
12
13
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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/globalize/active_record/act_macro.rb', line 4

def translates(*attr_names)

  options = attr_names.extract_options!
  attr_names = attr_names.map(&:to_sym)

  unless translates?
    options[:table_name] ||= "#{table_name.singularize}_translations"
    options[:foreign_key] ||= class_name.foreign_key

    class_attribute :translated_attribute_names, :translation_options, :fallbacks_for_empty_translations
    self.translated_attribute_names = []
    self.translation_options        = options
    self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]

    include InstanceMethods
    extend  ClassMethods, Migration

    translation_class.table_name = options[:table_name] if translation_class.table_name.blank?

    has_many :translations, :class_name  => translation_class.name,
                            :foreign_key => options[:foreign_key],
                            :dependent   => :destroy,
                            :extend      => HasManyExtensions

    after_create :save_translations!
    after_update :save_translations!

    if options[:versioning]
      ::ActiveRecord::Base.extend(Globalize::Versioning::PaperTrail)

      translation_class.has_paper_trail
      delegate :version, :versions, :to => :translation
    end

    translation_class.instance_eval %{
      attr_accessible :#{(attr_names | [:locale]).join(', :')}
    }

    # detect and apply serialization
    attr_names.each do |attr_name|
      serializer = self.serialized_attributes[attr_name.to_s]

      if serializer.present?
        if defined?(::ActiveRecord::Coders::YAMLColumn) &&
           serializer.is_a?(::ActiveRecord::Coders::YAMLColumn)

          serializer = serializer.object_class
        end

        translation_class.send :serialize, attr_name, serializer
      end
    end
  end

  new_attr_names = attr_names - translated_attribute_names
  new_attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
  self.translated_attribute_names += new_attr_names
end

#translates?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/globalize/active_record/act_macro.rb', line 70

def translates?
  included_modules.include?(InstanceMethods)
end