class ThinkingSphinx::ActiveRecord::PropertyQuery
Attributes
property[R]
source[R]
type[R]
Public Class Methods
new(property, source, type = nil)
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 4 def initialize(property, source, type = nil) @property, @source, @type = property, source, type end
Public Instance Methods
to_s()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 8 def to_s if unsafe_habtm_column? raise <<-MESSAGE Source queries cannot be used with HABTM joins if they use anything beyond the primary key. MESSAGE end if safe_habtm_column? ThinkingSphinx::ActiveRecord::SimpleManyQuery.new( property, source, type ).to_s else "#{identifier} from #{source_type}; #{queries.join('; ')}" end end
Private Instance Methods
base_association()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 32 def base_association reflections.first end
base_association_class()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 36 def base_association_class base_association.klass end
column()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 40 def column @column ||= property.columns.first end
extend_reflection(reflection)
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 44 def extend_reflection(reflection) return [reflection] unless reflection.through_reflection [reflection.through_reflection, reflection.source_reflection] end
identifier()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 50 def identifier [type, property.name].compact.join(' ') end
joins()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 54 def joins @joins ||= begin remainder = reflections.collect(&:name)[1..-1] return nil if remainder.empty? return remainder.first if remainder.length == 1 remainder[0..-2].reverse.inject(remainder.last) { |value, key| {key => value} } end end
macros()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 66 def macros reflections.collect &:macro end
offset()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 70 def offset "* #{ThinkingSphinx::Configuration.instance.indices.count} + #{source.offset}" end
queries()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 74 def queries queries = [] if column.string? queries << column.__name.strip.gsub(/\n/, "\\\n") else queries << to_sql queries << range_sql if ranged? end queries end
quote_column(column)
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 97 def quote_column(column) ActiveRecord::Base.connection.quote_column_name(column) end
quote_with_table(table, column)
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 93 def quote_with_table(table, column) "#{quote_column(table)}.#{quote_column(column)}" end
quoted_foreign_key()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 85 def quoted_foreign_key quote_with_table(base_association_class.table_name, base_association.foreign_key) end
quoted_primary_key()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 89 def quoted_primary_key quote_with_table(reflections.last.klass.table_name, column.__name) end
range_sql()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 105 def range_sql base_association_class_unscoped.select( "MIN(#{quoted_foreign_key}), MAX(#{quoted_foreign_key})" ).to_sql end
ranged?()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 101 def ranged? property.source_type == :ranged_query end
reflections()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 111 def reflections @reflections ||= begin base = source.model column.__stack.collect { |key| reflection = base.reflect_on_association key base = reflection.klass extend_reflection reflection }.flatten end end
safe_habtm_column?()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 124 def safe_habtm_column? macros == [:has_and_belongs_to_many] && column.__name == :id end
source_type()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 128 def source_type property.source_type.to_s.dasherize end
to_sql()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 132 def to_sql raise "Could not determine SQL for MVA" if reflections.empty? relation = base_association_class_unscoped.select("#{quoted_foreign_key} #{offset} AS #{quote_column('id')}, #{quoted_primary_key} AS #{quote_column(property.name)}") relation = relation.joins(joins) if joins.present? relation = relation.where("#{quoted_foreign_key} BETWEEN $start AND $end") if ranged? relation = relation.where("#{quoted_foreign_key} IS NOT NULL") relation = relation.order(sql("#{quoted_foreign_key} ASC")) if type.nil? relation.to_sql end
unsafe_habtm_column?()
click to toggle source
# File lib/thinking_sphinx/active_record/property_query.rb, line 144 def unsafe_habtm_column? macros.include?(:has_and_belongs_to_many) && ( macros.length > 1 || column.__name != :id ) end