Skip to content

Commit

Permalink
Handle column specific searching
Browse files Browse the repository at this point in the history
  • Loading branch information
voltechs committed Jan 6, 2017
1 parent 5026b9f commit 0c76d50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion data_tables-responder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'http://rubygems.org'
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
else
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
end
Expand Down
23 changes: 13 additions & 10 deletions lib/data_tables/modules/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(collection, adapter_options)
end

def search
return @collection unless (default_search = request_parameters.dig(:search, :value)).present?
default_search = request_parameters.dig(:search, :value)

model = @collection.try(:model) || @collection
arel_table = model.arel_table
Expand All @@ -45,6 +45,8 @@ def search
case column.type
when :string
model.arel_table[k].matches("%#{query}%")
when :integer
model.arel_table[k].eq(query)
else
nil
end
Expand All @@ -58,16 +60,17 @@ def search
protected

def searchable_columns(default_search)
@searchable_columns ||= Hash[
request_parameters[:columns].collect do |c|
if c[:searchable] && c[:data]
value = default_search unless (value = c.dig(:search, :value)).present?
[c[:data], value]
else
nil
@searchable_columns = {}
request_parameters[:columns]&.inject(@searchable_columns) do |a, b|
if (b[:searchable] && b[:data].present?)
if ((value = b.dig(:search, :value).present? ? b.dig(:search, :value) : default_search).present?)
a[b[:data]] = value
end
end.compact
]
end
a
end

@searchable_columns
end

private
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.1'
VERSION = '0.2.0'
end
end

0 comments on commit 0c76d50

Please sign in to comment.