Module: DataMapper::Is::Localizable

Defined in:
lib/dm-is-localizable/is/localizable.rb,
lib/dm-is-localizable/storage/translation.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, Translation

Instance Method Summary collapse

Instance Method Details

#is_localizable(options = {}, &block) ⇒ Object



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
# File 'lib/dm-is-localizable/is/localizable.rb', line 7

def is_localizable(options = {}, &block)

  extend  ClassMethods
  include InstanceMethods

  options = {
    :as         => nil,
    :class_name => "#{self}Translation"
  }.merge(options)

  remixer_fk = Extlib::Inflection.foreign_key(self.name).to_sym
  remixer    = remixer_fk.to_s.gsub('_id', '').to_sym
  remixee    = Extlib::Inflection.tableize(options[:class_name]).to_sym

  remix n, Translation, :as => options[:as], :class_name => options[:class_name]

  @translation_class = Extlib::Inflection.constantize(options[:class_name])
  class_inheritable_accessor :translation_class

  enhance :translation, @translation_class do

    property remixer_fk,   Integer, :nullable => false, :unique_index => :unique_languages
    property :language_id, Integer, :nullable => false, :unique_index => :unique_languages

    belongs_to remixer
    belongs_to :language

    class_eval &block

    validates_is_unique :language_id, :scope => remixer_fk

  end

  has n, :languages, :through => remixee, :constraint => :destroy

  self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    alias :translations #{remixee}
  RUBY

  localizable_properties.each do |property_name|
    self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)

      def #{property_name}(language_code)
        translate(:#{property_name.to_sym}, language_code)
      end

    RUBY
  end

end