Sha256: 2a79bbef9e3681195bea17b65f820d3c79cb2b4e01d6b9e5d61187d69bcd12f1

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 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|
      klass.reflections[name] ||= ThinkingSphinx::ActiveRecord::
        FilteredReflection.clone_with_filter(reflection, name, class_name)
    end
  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.reflections[column.__name]
  end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.3 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.0.2 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.0.1 lib/thinking_sphinx/active_record/polymorpher.rb