Sha256: 9d7e490e11d67ae0e204d7ab9b077bb0c3cc5e6e50029a1c086465e9f79d9acf

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 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] ||= clone_with name, class_name
    end
  end

  def clone_with(name, class_name)
    ThinkingSphinx::ActiveRecord::FilteredReflection.clone_with_filter(
      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.reflections[column.__name]
  end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
thinking-sphinx-3.1.1 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.1.0 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.0.6 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.0.5 lib/thinking_sphinx/active_record/polymorpher.rb
thinking-sphinx-3.0.4 lib/thinking_sphinx/active_record/polymorpher.rb