Commit 6607dc221143955c2b42f967c3e5d4de6948d1e9

Authored by Marcos Pereira
1 parent 5bd0d07e
Exists in stable-spb-1.5

Fixes ActiveRecord relations for descendants

Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: https://github.com/rails/rails/pull/24719
Issue: https://github.com/rails/rails/issues/20678

Signed-off-by: Joenio Costa <joenio@colivre.coop.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
(cherry picked from commit 3b60e7ca12eaa9492c7cf5ba74153ca243a25417)
lib/noosfero/core_ext/active_record/reflection.rb
1 -  
2 # on STI classes tike Article and Profile, plugins' extensions 1 # on STI classes tike Article and Profile, plugins' extensions
3 # on associations should be reflected on descendants 2 # on associations should be reflected on descendants
4 module ActiveRecord 3 module ActiveRecord
5 - module Reflection 4 + module Associations
  5 + def association(name) #:nodoc:
  6 + association = association_instance_get(name)
6 7
7 - class << self 8 + if association.nil?
  9 + reflection = self.class._reflect_on_association(name)
8 10
9 - def add_reflection_with_descendants(ar, name, reflection)  
10 - self.add_reflection_without_descendants ar, name, reflection  
11 - ar.descendants.each do |k|  
12 - k._reflections.merge!(name.to_s => reflection)  
13 - end if ar.base_class == ar  
14 - end 11 + # Check inheritance chain for possible upstream association
  12 + klass = self.class.superclass
  13 + while reflection.nil? && klass.respond_to?(:_reflect_on_association)
  14 + reflection = klass._reflect_on_association(name)
  15 + klass = klass.superclass
  16 + end
15 17
16 - alias_method_chain :add_reflection, :descendants 18 + unless reflection
  19 + raise AssociationNotFoundError.new(self, name)
  20 + end
  21 + association = reflection.association_class.new(self, reflection)
  22 + association_instance_set(name, association)
  23 + end
17 24
  25 + association
18 end 26 end
19 end 27 end
20 end 28 end