data_table 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +14 -2
- data/lib/data_table/active_record.rb +28 -13
- data/lib/data_table/version.rb +1 -1
- data/spec/active_record_data_table_spec.rb +10 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -47,8 +47,20 @@ in your view (assuming HAML):
|
|
47
47
|
|
48
48
|
%tbody
|
49
49
|
|
50
|
-
|
50
|
+
== Advanced Features
|
51
|
+
|
52
|
+
=== Date fields
|
53
|
+
|
54
|
+
In order to handle date fields properly, enclose the field name in an array along with a hash like so:
|
55
|
+
|
56
|
+
Provider.for_data_table(self, %w(name fein opened_on), ["name", "fein", ["opened_on", {:date => true}]]) { ... }
|
57
|
+
|
58
|
+
=== Split fields
|
59
|
+
|
60
|
+
To handle split fields, that is to handle searching for "x-y", add a hash with a split key:
|
61
|
+
|
62
|
+
Provider.for_data_table(self, %w(name fein suffix), ["name", ["fein", "suffix", {:split => "-"}]]) { ... }
|
51
63
|
|
52
64
|
== Copyright
|
53
65
|
|
54
|
-
Copyright (c) 2010 Jason Dew. See LICENSE for details.
|
66
|
+
Copyright (c) 2010-2011 Jason Dew. See LICENSE for details.
|
@@ -58,19 +58,9 @@ module DataTable
|
|
58
58
|
options = field.extract_options!
|
59
59
|
|
60
60
|
if options[:split]
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
if split_query.size == field.size
|
66
|
-
field.map do |f|
|
67
|
-
conditions << "UPPER(#{f}) LIKE ?"
|
68
|
-
parameters << "%#{split_query.shift.upcase}%"
|
69
|
-
end
|
70
|
-
["(" + conditions.join(" AND ") + ")", *parameters]
|
71
|
-
else
|
72
|
-
[]
|
73
|
-
end
|
61
|
+
_split_where_condition query, field, options[:split]
|
62
|
+
elsif options[:date]
|
63
|
+
_date_where_condition query, field.first
|
74
64
|
else
|
75
65
|
_where_conditions(query, field, "AND")
|
76
66
|
end
|
@@ -79,6 +69,31 @@ module DataTable
|
|
79
69
|
end
|
80
70
|
end
|
81
71
|
|
72
|
+
def _date_where_condition query, field
|
73
|
+
begin
|
74
|
+
["#{field} = ?", Date.parse(query)]
|
75
|
+
rescue ArgumentError
|
76
|
+
[]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def _split_where_condition query, fields, splitter
|
81
|
+
conditions = []
|
82
|
+
parameters = []
|
83
|
+
split_query = query.split splitter
|
84
|
+
|
85
|
+
if split_query.size == fields.size
|
86
|
+
fields.map do |f|
|
87
|
+
conditions << "UPPER(#{f}) LIKE ?"
|
88
|
+
parameters << "%#{split_query.shift.upcase}%"
|
89
|
+
end
|
90
|
+
|
91
|
+
["(" + conditions.join(" AND ") + ")", *parameters]
|
92
|
+
else
|
93
|
+
[]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
82
97
|
def _order_fields params, fields
|
83
98
|
direction = params[:sSortDir_0] == "asc" ? "ASC" : "DESC"
|
84
99
|
%{#{fields[params[:iSortCol_0].to_i]} #{direction}}
|
data/lib/data_table/version.rb
CHANGED
@@ -38,6 +38,16 @@ describe DataTable do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
context "with a date field" do
|
42
|
+
it "should return an AR array using equality and converting to a date" do
|
43
|
+
send(:_where_conditions, "2011/09/03", [["f1", {:date => true}]]).should == ["(f1 = ?)", Date.new(2011, 9, 3)]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return an AR array properly not search date fields with non-dates" do
|
47
|
+
send(:_where_conditions, "foo", ["f1", ["f2", {:date => true}]]).should == ["(UPPER(f1) LIKE ?)", "%FOO%"]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
41
51
|
context "with complex conditions" do
|
42
52
|
it "should return an AR array with an entry for each search field" do
|
43
53
|
send(:_where_conditions, "query", [%w(foo bar)]).should == ["((UPPER(foo) LIKE ? AND UPPER(bar) LIKE ?))", "%QUERY%", "%QUERY%"]
|