Skip to content

Commit 0c76d50

Browse files
committed
Handle column specific searching
1 parent 5026b9f commit 0c76d50

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

data_tables-responder.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
1616
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
1717
# to allow pushing to a single host or delete this section to allow pushing to any host.
1818
if spec.respond_to?(:metadata)
19-
spec.metadata['allowed_push_host'] = 'http://rubygems.org'
19+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
2020
else
2121
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
2222
end

lib/data_tables/modules/search.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(collection, adapter_options)
1818
end
1919

2020
def search
21-
return @collection unless (default_search = request_parameters.dig(:search, :value)).present?
21+
default_search = request_parameters.dig(:search, :value)
2222

2323
model = @collection.try(:model) || @collection
2424
arel_table = model.arel_table
@@ -45,6 +45,8 @@ def search
4545
case column.type
4646
when :string
4747
model.arel_table[k].matches("%#{query}%")
48+
when :integer
49+
model.arel_table[k].eq(query)
4850
else
4951
nil
5052
end
@@ -58,16 +60,17 @@ def search
5860
protected
5961

6062
def searchable_columns(default_search)
61-
@searchable_columns ||= Hash[
62-
request_parameters[:columns].collect do |c|
63-
if c[:searchable] && c[:data]
64-
value = default_search unless (value = c.dig(:search, :value)).present?
65-
[c[:data], value]
66-
else
67-
nil
63+
@searchable_columns = {}
64+
request_parameters[:columns]&.inject(@searchable_columns) do |a, b|
65+
if (b[:searchable] && b[:data].present?)
66+
if ((value = b.dig(:search, :value).present? ? b.dig(:search, :value) : default_search).present?)
67+
a[b[:data]] = value
6868
end
69-
end.compact
70-
]
69+
end
70+
a
71+
end
72+
73+
@searchable_columns
7174
end
7275

7376
private
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module DataTables
22
module Responder
3-
VERSION = '0.1.1'
3+
VERSION = '0.2.0'
44
end
55
end

0 commit comments

Comments
 (0)