diff options
author | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 17:01:08 +0200 |
---|---|---|
committer | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 18:40:38 +0200 |
commit | 22c322f056f42d95b0421e6608f404134463de13 (patch) | |
tree | c35a15a73d606750a8ac57f8f18c330bbb1e5e2b /activemodel/lib | |
parent | 776da539d72c98d077f97789a1265dd23a79711f (diff) | |
download | rails-22c322f056f42d95b0421e6608f404134463de13.tar.gz rails-22c322f056f42d95b0421e6608f404134463de13.tar.bz2 rails-22c322f056f42d95b0421e6608f404134463de13.zip |
Add support for overriding associations, mostly used for authorization
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/serializer.rb | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb index c5b433df51..6e89eec09c 100644 --- a/activemodel/lib/active_model/serializer.rb +++ b/activemodel/lib/active_model/serializer.rb @@ -41,6 +41,10 @@ module ActiveModel def associate(klass, attrs) options = attrs.extract_options! self._associations += attrs.map do |attr| + unless method_defined?(attr) + class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__ + end + options[:serializer] ||= const_get("#{attr.to_s.camelize}Serializer") klass.new(attr, options) end @@ -79,7 +83,7 @@ module ActiveModel hash = attributes _associations.each do |association| - associated_object = object.send(association.name) + associated_object = send(association.name) hash[association.name] = association.serialize(associated_object, scope) end |