Skip to content

Commit

Permalink
refactor transmitted_sql to with_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Jun 11, 2013
1 parent f1b5679 commit e9732c6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ mva_attribute :rubrics do |product|
end
```

### Изменение sql запроса, получающего атрибуты из базы при обновлении записи
### Изменение sql запроса, получающего атрибуты из базы
При массовой индексации
```ruby
transmitted_sql do |sql|
with_sql :on => :select do |sql|
sql.sub(" AND products.state != 'deleted'", '')
end

При обновлении записи
```ruby
with_sql :on => :update do |sql|
sql.sub(" AND products.state != 'deleted'", '')
end
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def limit(value)

# Блок, который будет вызываться при сохранении модели перед получением атрибутов из базы
# С помощью него, можно подправить конечный sql
def transmitted_sql(&block)
@index.local_options[:transmitted_sql] = block
def with_sql(options = {}, &block)
@index.local_options[:with_sql] ||= {}
@index.local_options[:with_sql][options.fetch(:on, :select)] = block
end

# Блок, который будет вызывается при сохранении модели
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Sphinx::Integration::Extensions::ThinkingSphinx::Source::SQL
alias_method_chain :to_sql, :joins
alias_method_chain :to_sql, :nogrouping_options
alias_method_chain :to_sql, :limit
alias_method_chain :to_sql, :custom_sql

alias_method_chain :to_sql_query_range, :prepare
alias_method_chain :to_sql_query_info, :prepared_table_name
Expand Down Expand Up @@ -84,6 +85,14 @@ def to_sql_with_limit(*args)
sql
end

def to_sql_with_custom_sql(*args)
sql = to_sql_without_custom_sql(*args)
if @index.local_options[:with_sql] && @index.local_options[:with_sql][:select]
sql = @index.local_options[:with_sql][:select].call(sql)
end
sql
end

def to_sql_query_range_with_prepare(options = {})
return '' if @index.options[:disable_range] || (delta? && options[:delta] && @index.local_options[:disable_delta_range])
return @index.local_options[:sql_query_range] if @index.local_options[:sql_query_range]
Expand Down
4 changes: 3 additions & 1 deletion lib/sphinx/integration/transmitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def self.execute(query)
# Returns Hash
def transmitted_data(index)
sql = index.single_query_sql.gsub('%{ID}', record.id.to_s)
sql = index.local_options[:transmitted_sql].call(sql) if index.local_options.key?(:transmitted_sql)
if index.local_options[:with_sql] && index.local_options[:with_sql][:update]
sql = index.local_options[:with_sql][:update].call(sql)
end
row = record.class.connection.execute(sql).first
return unless row
row.merge!(mva_attributes(index))
Expand Down

0 comments on commit e9732c6

Please sign in to comment.