Skip to content

Commit

Permalink
rt updates with typecasting
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Apr 22, 2013
1 parent 8231d18 commit fb5feee
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/sphinx/integration/extensions/riddle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
module Sphinx::Integration::Extensions
module Riddle
autoload :Configuration, 'sphinx/integration/extensions/riddle/configuration'
autoload :Query, 'sphinx/integration/extensions/riddle/query'
end
end
8 changes: 8 additions & 0 deletions lib/sphinx/integration/extensions/riddle/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding: utf-8
module Sphinx::Integration::Extensions
module Riddle
module Query
autoload :Insert, 'sphinx/integration/extensions/riddle/query/insert'
end
end
end
26 changes: 26 additions & 0 deletions lib/sphinx/integration/extensions/riddle/query/insert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
module Sphinx::Integration::Extensions
module Riddle
module Query
module Insert
extend ActiveSupport::Concern

included do
alias_method_chain :translated_value, :ext
end

# Riddle не совсем корректно обрабатывает все типы значений, например nil или Array
def translated_value_with_ext(value)
if value.nil?
"''"
elsif value.is_a?(Array)
"(#{value.join(',')})"
else
translated_value_without_ext(value)
end
end

end
end
end
end
8 changes: 8 additions & 0 deletions lib/sphinx/integration/extensions/thinking_sphinx/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def all_names_with_rt
names
end

# Карта атрибутов и их типов, нужна для типкастинга
#
# Returns Hash
def attributes_types_map
return @attributes_type_map if defined?(@attributes_type_map)
@attributes_type_map = attributes.inject({}){ |h, attr| h[attr.unique_name.to_s] = attr.type; h }
end

def single_query_sql
@single_query_sql ||= sources.
first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(index, name)

index.local_options[:source_joins] ||= {}
index.local_options[:source_joins][key] = {}
as(table_alias)
as(table_alias || table_name)

self
end
Expand Down
1 change: 1 addition & 0 deletions lib/sphinx/integration/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Railtie < Rails::Railtie

initializer 'sphinx_integration.extensions', :after => 'thinking_sphinx.set_app_root' do
Riddle::Configuration::RealtimeIndex.send :include, Sphinx::Integration::Extensions::Riddle::Configuration::RealtimeIndex
Riddle::Query::Insert.send :include, Sphinx::Integration::Extensions::Riddle::Query::Insert
ThinkingSphinx.send :include, Extensions::ThinkingSphinx
ThinkingSphinx::Attribute.send :include, Sphinx::Integration::Extensions::ThinkingSphinx::Attribute
ThinkingSphinx::Source.send :include, Sphinx::Integration::Extensions::ThinkingSphinx::Source
Expand Down
15 changes: 13 additions & 2 deletions lib/sphinx/integration/transmitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ def rt_indexes
def transmitted_data(index)
sql = index.single_query_sql.gsub('%{ID}', record.id.to_s)
row = record.class.connection.execute(sql).first
row.merge(mva_attributes(index))
row.merge!(mva_attributes(index))

row.each do |key, value|
row[key] = case index.attributes_types_map[key]
when :integer then value.to_i
when :float then value.to_f
when :multi then value.is_a?(String) ? value.split(',') : value
else value
end
end

row
end

# MVA data
Expand All @@ -71,7 +82,7 @@ def mva_attributes(index)
attrs = {}

index.mva_sources.each do |name, mva_proc|
attrs[name] = "(#{mva_proc.call(record).join(',')})"
attrs[name] = mva_proc.call(record)
end if index.mva_sources

attrs
Expand Down

0 comments on commit fb5feee

Please sign in to comment.