Class: ThinkingSphinx::ActiveRecord::FilteredReflection::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/active_record/filtered_reflection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reflection, class_name) ⇒ Filter

Returns a new instance of Filter



9
10
11
12
# File 'lib/thinking_sphinx/active_record/filtered_reflection.rb', line 9

def initialize(reflection, class_name)
  @reflection, @class_name = reflection, class_name
  @options = reflection.options.clone
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name



5
6
7
# File 'lib/thinking_sphinx/active_record/filtered_reflection.rb', line 5

def class_name
  @class_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection



5
6
7
# File 'lib/thinking_sphinx/active_record/filtered_reflection.rb', line 5

def reflection
  @reflection
end

Instance Method Details

#optionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thinking_sphinx/active_record/filtered_reflection.rb', line 14

def options
  @options.delete :polymorphic
  @options[:class_name]    = class_name
  @options[:foreign_key] ||= "#{reflection.name}_id"
  @options[:foreign_type]  = reflection.foreign_type

  if reflection.respond_to?(:scope)
    @options[:sphinx_internal_filtered] = true
    return @options
  end

  case @options[:conditions]
  when nil
    @options[:conditions] = condition
  when Array
    @options[:conditions] << condition
  when Hash
    @options[:conditions].merge!(reflection.foreign_type => @options[:class_name])
  else
    @options[:conditions] << " AND #{condition}"
  end

  @options
end

#scopeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thinking_sphinx/active_record/filtered_reflection.rb', line 39

def scope
  if ::Joiner::Joins.instance_methods.include?(:join_association_class)
    return nil
  end

  lambda { |association|
    reflection = association.reflection
    klass      = reflection.class_name.constantize
    where(
      association.parent.aliased_table_name.to_sym =>
      {reflection.foreign_type => klass.base_class.name}
    )
  }
end