Module: PickleI18n::Session

Defined in:
lib/pickle_i18n/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(m) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/pickle_i18n/session.rb', line 2

def self.included(m)
  m.module_eval do
    unless m.instance_methods.include?(:create_model_without_i18n_attribute_names)
      alias_method :create_model_without_i18n_attribute_names, :create_model
      alias_method :create_model, :create_model_with_i18n_attribute_names
    end
  end
end

Instance Method Details

#create_model_with_i18n_attribute_names(orig_pickle_ref, orig_fields = nil) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pickle_i18n/session.rb', line 11

def create_model_with_i18n_attribute_names(orig_pickle_ref, orig_fields = nil)
  pickle_ref = PickleI18n.model_translations[orig_pickle_ref] || orig_pickle_ref
  raise ArgumentError, "No model_translation found: #{orig_pickle_ref}" unless orig_pickle_ref

  if attr_trans = PickleI18n.model_attribute_translations[pickle_ref]
    case orig_fields
    when String then
      fields = orig_fields.dup
      attr_trans.each{|k,v| fields.gsub!(k, v)}
    when Hash then
      fields = orig_fields.inject({}){|d, (k, v)| d[ attr_trans[k] || k ] = v; d }
    else
      raise "Unsupported fields: #{fields.inspect}"
    end
  end
  fields ||= orig_fields

  create_model_without_i18n_attribute_names(pickle_ref, fields)
end