Skip to content

Commit

Permalink
Refactor to do "outer joins" which is better for more lenient filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
voltechs committed Aug 11, 2016
1 parent fc9f3f8 commit 5026b9f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/data_tables/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def serializable_hash(options)
end

@results = search(@results)
# search_by = search(@results)

# Rails.logger.warn "SEARCH BY: #{search_by}"
@results = order_by.inject(@results) { |r, o| r.order(o) }
Expand Down
14 changes: 11 additions & 3 deletions lib/data_tables/modules/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ def search
return @collection unless (default_search = request_parameters.dig(:search, :value)).present?

model = @collection.try(:model) || @collection
arel_table = model.arel_table
columns = searchable_columns(default_search)

searches = DataTables.flat_keys_to_nested columns

or_clause = nil
search_by = searches.collect do |k, query|
if query.is_a? Hash
klass = model.reflect_on_association(k).klass
assoc = model.reflect_on_association(k)
assoc_klass = assoc.klass
assoc_arel_table = assoc_klass.arel_table

@collection = @collection.joins(k)
klass.arel_table[query.first.first].matches(query.first.last)
if model.respond_to? :left_outer_join
@collection = @collection.left_outer_join(k)
else
@collection = @collection.includes(k).references(k)
end

assoc_arel_table[query.first.first].matches(query.first.last)
else
if (column = model.columns.find { |c| c.name == k.to_s })
case column.type
Expand Down
2 changes: 1 addition & 1 deletion lib/data_tables/responder/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DataTables
module Responder
VERSION = "0.1.0"
VERSION = '0.1.1'
end
end

0 comments on commit 5026b9f

Please sign in to comment.