module DataMapper::Is::Translatable::ClassMethods

Protected Instance Methods

translatable_define_hash() click to toggle source

Define hash that contains all the translations

# File lib/dm-translatable.rb, line 152
def translatable_define_hash
  @translatable = {}
end
translatable_locale(locale_attr) click to toggle source

Define the key that the translation will be used for belongs_to association, to communicate with original model Example:

translatable_origin :language

Default: :locale

# File lib/dm-translatable.rb, line 129
def translatable_locale locale_attr
  @translatable[:locale] = translatable_locale_prepared locale_attr
end
translatable_locale_prepared(locale = nil) click to toggle source
# File lib/dm-translatable.rb, line 146
def translatable_locale_prepared locale = nil
  locale || "locale"
end
translatable_model(model_name) click to toggle source

Defines model that will be treated as translation handler. Model can be defined as String, Symbol or Constant. Examples:

translated_model TranslatedNews
translated_model "TranslatedNews"
translated_model :TranslatedNews

Default: Translatable<ModelName>

# File lib/dm-translatable.rb, line 103
def translatable_model model_name
  @translatable[:model] = translatable_model_prepared model_name
end
translatable_model_prepared(model_name = nil) click to toggle source

Returns Model as a constant that deals with translations

# File lib/dm-translatable.rb, line 135
def translatable_model_prepared model_name = nil
  model_constant = model_name
  model_constant ||= "Translatable#{self.name}"
  model_constant.to_s.constantize
end
translatable_origin(origin_key) click to toggle source

Define the key that the translation will be used for belongs_to association, to communicate with original model Example:

translatable_origin :news

Default: :origin

# File lib/dm-translatable.rb, line 116
def translatable_origin origin_key
  @translatable[:origin] = translatable_origin_prepared origin_key
end
translatable_origin_prepared(origin_key = nil) click to toggle source
# File lib/dm-translatable.rb, line 142
def translatable_origin_prepared origin_key = nil
  origin_key || "origin"
end
translatable_property(*args) click to toggle source

Fields that are translatable. Those fields should be defined in the original model including all the related params. Examples:

translatable_property  :title,    String,   required: true, unique: true
translatable_property  :content,  Text

NB! Will raise an error if there was no fields specified

# File lib/dm-translatable.rb, line 88
def translatable_property *args
  (@translatable[:properties] ||= [])  << args
end
translatable_register() click to toggle source

Handles all the registring routine, defining methods, properties, and everything else

# File lib/dm-translatable.rb, line 159
def translatable_register
  raise ArgumentError.new("At least one property should be defined") if [nil, []].include?(@translatable[:properties])
  [:model,:origin,:locale].each { |hash_key| @translatable[hash_key] ||= send "translatable_#{hash_key}_prepared" }

  translatable_register_properties_for_origin
  translatable_register_properties_for_translatable
end
translatable_register_properties_for_origin() click to toggle source

Handle the routine to define all th required stuff on the original maodel

# File lib/dm-translatable.rb, line 169
        def translatable_register_properties_for_origin
          has Infinity, :translations, @translatable[:model].name, :child_key => [ :"#{@translatable[:origin]}_id" ]

          @translatable[:properties].each do |p|
            self.module_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{p.first}
                current_translation && current_translation.#{p.first}
              end
            RUBY
          end
        end
translatable_register_properties_for_translatable() click to toggle source
# File lib/dm-translatable.rb, line 181
        def translatable_register_properties_for_translatable
          @translatable[:properties].each do |p|
            @translatable[:model].__send__(:property, *p)
          end

          @translatable[:model].module_eval <<-RUBY, __FILE__, __LINE__ + 1
            property :#{@translatable[:locale]}, String, :required => true
            property :#{@translatable[:origin]}_id, Integer, :required => true

            belongs_to :#{@translatable[:origin]}, "#{self.name}"

            before :valid? do
              # Small hack to go around form submition problem
              # Without it it whould complai ther the original_id should be type of Integer
              self.__send__("#{@translatable[:origin]}_id=", nil) if self.__send__("#{@translatable[:origin]}_id") == ''
            end
          RUBY
        end