gist: 4837 Download_button fork
public
Public Clone URL: git://gist.github.com/4837.git
sample_of_ar_enhance_for_irc_discussion.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  module ClassMethods
    # Declare in ActiveRecord or ActiveResource class with localized attributes
    # You must include the module "manually" in your own classes (ie. put <tt>include HasLocalizedAttributes</tt> in your class)
    # See README for example
    def has_localized_atributes
      include InstanceMethods unless included_modules.include?(InstanceMethods)
    end
  end
    
  module InstanceMethods
    
    # Define <tt>method missing</tt> to intercept calls to non-localized methods (eg. +name+ instead of +name_cz+)
    def method_missing(method_name, *arguments)
      language = (defined?(Gibberish) ? Gibberish.current_language.to_s : HasLocalizedAttributes.default_language).sub(/cz/, 'cs')
      # puts "Trying to send '#{method_name}_#{language}' to #{self}" # uncomment for easy debugging in script/console
      return self.send(:"#{method_name}_#{language}") if self.respond_to?(:"#{method_name}_#{language}")
      super
    end
    
  end
 
 

Owner

karmi

Revisions

  • f0ed83 karmi Mon Aug 11 03:06:11 -0700 2008