Sha256: 334e1e45eddad7fc5c9400ea6b05d4845ce4b133211df6ad4b42e1ca5d38fc1c

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

class ThinkingSphinx::ActiveRecord::Polymorpher
  def initialize(source, column, class_names)
    @source, @column, @class_names = source, column, class_names
  end

  def morph!
    append_reflections
    morph_properties
  end

  private

  attr_reader :source, :column, :class_names

  def append_reflections
    mappings.each do |class_name, name|
      next if klass.reflect_on_association(name)

      reflection = clone_with name, class_name
      if ActiveRecord::Reflection.respond_to?(:add_reflection)
        ActiveRecord::Reflection.add_reflection klass, name, reflection
      else
        klass.reflections[name] = reflection
      end
    end
  end

  def clone_with(name, class_name)
    ThinkingSphinx::ActiveRecord::FilterReflection.call(
      reflection, name, class_name
    )
  end

  def mappings
    @mappings ||= class_names.inject({}) do |hash, class_name|
      hash[class_name] = "#{column.__name}_#{class_name.downcase}".to_sym
      hash
    end
  end

  def morphed_stacks
    @morphed_stacks ||= mappings.values.collect { |key|
      column.__stack + [key]
    }
  end

  def morph_properties
    (source.fields + source.attributes).each do |property|
      property.rebase column.__path, :to => morphed_stacks
    end
  end

  def reflection
    @reflection ||= klass.reflect_on_association column.__name
  end

  def klass
    @klass ||= column.__stack.inject(source.model) { |parent, key|
      parent.reflect_on_association(key).klass
    }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thinking-sphinx-3.4.2 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.4.1 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.4.0 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.3.0 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.2.0 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.1.4 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.1.3 lib/thinking_sphinx/active_record/polymorpher.rb