Sha256: 297375633040f7189c3a9fc4bb5bf9d5f82d8f853e90056378fff928a5a0cacf

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

require 'active_record'

module Ransack
  module Adapters
    module ActiveRecord
      class JoinAssociation < ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

        def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
          if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
            swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
              super(reflection, join_dependency, parent)
            end
          else
            super(reflection, join_dependency, parent)
          end
        end

        def swapping_reflection_klass(reflection, klass)
          reflection = reflection.clone
          original_polymorphic = reflection.options.delete(:polymorphic)
          reflection.instance_variable_set(:@klass, klass)
          yield reflection
        ensure
          reflection.options[:polymorphic] = original_polymorphic
        end

        def ==(other)
          super && active_record == other.active_record
        end

        def build_constraint(reflection, table, key, foreign_table, foreign_key)
          if reflection.options[:polymorphic]
            super.and(
              foreign_table[reflection.foreign_type].eq(reflection.klass.name)
            )
          else
            super
          end
        end

      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ransack-0.5.6 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.5 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.4 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.3 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.2 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.1 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.5.0 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.4.2 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.4.1 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.4.0 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.3.0 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.2.1 lib/ransack/adapters/active_record/3.0/join_association.rb
ransack-0.2.0 lib/ransack/adapters/active_record/3.0/join_association.rb