Class: I18n::Alchemy::Proxy

Inherits:
Object
  • Object
show all
Includes:
AttributesParsing
Defined in:
lib/i18n_alchemy/proxy.rb

Instance Method Summary collapse

Methods included from AttributesParsing

#assign_attributes, #attributes=, #update, #update!, #update_attribute, #update_attributes, #update_attributes!

Constructor Details

#initialize(target, attributes = nil) ⇒ Proxy

TODO: cannot assume _id is always a foreign key. Find a better way to find that and skip these columns.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/i18n_alchemy/proxy.rb', line 18

def initialize(target, attributes = nil)
  @target = target
  @target_class = target.class

  @localized_attributes = {}
  @localized_associations = []

  build_methods
  if active_record_compatible?
    build_attributes
    build_associations
  end

  assign_attributes(attributes) if attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



67
68
69
# File 'lib/i18n_alchemy/proxy.rb', line 67

def method_missing(*args, **kwargs, &block)
  @target.send(*args, **kwargs, &block)
end

Instance Method Details

#to_json(options = nil) ⇒ Object

Override to_json to always call to_json on the target object, instead of serializing the proxy object, that may issue circular references on Ruby 1.8.



42
43
44
# File 'lib/i18n_alchemy/proxy.rb', line 42

def to_json(options = nil)
  @target.to_json(options)
end

#to_modelObject

Override to_model to always return the proxy, otherwise it returns the target object. This allows us to integrate with action view.



48
49
50
# File 'lib/i18n_alchemy/proxy.rb', line 48

def to_model
  self
end

#to_paramObject

Override to_param to always return the proxy.to_param. This allow us to integrate with action view.



36
37
38
# File 'lib/i18n_alchemy/proxy.rb', line 36

def to_param
  @target.to_param
end

#try(*a, &b) ⇒ Object

Allow calling localized methods with :try. If the method is not declared here, it’ll be delegated to the target, losing localization capabilities.



59
60
61
# File 'lib/i18n_alchemy/proxy.rb', line 59

def try(*a, &b)
  __send__(*a, &b)
end