Skip to content

Support for anonymous model classes #1172

@kalsan

Description

@kalsan

Fore more fine-grained model control, I instantiate the model in an anonymous class that inherits from the original model class. However thinking-sphinx is unable to handle this situation since model.class is nil.

A concrete error scenario is using thinking-sphinx together with the gem RailsOps (https://github.com/sitrox/rails_ops).

To support this, two changes are necessary to thinking-sphinx. They can be monkey-patched in the the gem as follows:

# EDIT: DO NOT USE, SEE BELOW
# Monkey patch no. 1 for supporting anonymous classes. Otherwise incompatible with rails_ops
class ThinkingSphinx::IndexSet
  def self.reference_name(klass)
    @cached_results ||= {}
    result = @cached_results[klass.model_name.name] ||= klass.model_name.name.underscore.to_sym
    return result
  end
end

# Monkey patch no. 2 for supporting anonymous classes. Otherwise incompatible with rails_ops
class ThinkingSphinx::RealTime::Translator
  def result
    @result ||= owner.try(name)
    @result ||= owner.model_name.name if name == :name && stack == [:class]
    return @result
  end
end

It would be great if this or a better solution could be implemented in the upcoming release of thinking-sphinx. It should be no trouble to implement it and the change would allow support for patterns with anonymous classes.

Edit: Monkey patch 1 is flawed, use this instead:

class ThinkingSphinx::IndexSet
  def self.reference_name(klass)
    @cached_results ||= {}
    if klass.name
      @cached_results[klass.name] = klass.name.underscore.to_sym
      return @cached_results[klass.name]
    else
      @cached_results[klass.model_name.name] ||= klass.model_name.name.underscore.to_sym
      return @cached_results[klass.model_name.name]
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions