Module: Globalize::ActiveRecord::ActMacro

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

Instance Method Summary collapse

Instance Method Details

#class_nameObject



49
50
51
52
53
54
# File 'lib/globalize/active_record/act_macro.rb', line 49

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_columns) ⇒ Object

Translated attributes Example:

class Post < ActiveRecord::Base
  translates "title:string", "content:text", :versioning => true, :table_name => "..."
end


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
# File 'lib/globalize/active_record/act_macro.rb', line 10

def translates(*attr_columns)
  return if translates?

  options = attr_columns.extract_options!
  options[:table_name] ||= "#{table_name.singularize}_translations"
  
  attrs_hash = Utils.convert_columns(attr_columns)
  attr_names = attrs_hash.keys
  
  class_attribute :translated_attribute_names, :translation_options, 
                  :fallbacks_for_empty_translations, :translated_columns_hash
                  
  self.translated_attribute_names = attr_names.map(&:to_sym)
  self.translation_options        = options
  self.translated_columns_hash    = attrs_hash
  self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]

  include InstanceMethods, Accessors
  extend  ClassMethods, Migration

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

  before_save :update_checkers!
  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

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

#translates?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/globalize/active_record/act_macro.rb', line 56

def translates?
  included_modules.include?(InstanceMethods)
end