Skip to content

Commit 1fc6e39

Browse files
committed
Merge branch 'version-0-3-0' into newrelease
Conflicts: lib/ajax-datatables-rails/base.rb
2 parents c90c64c + c93e622 commit 1fc6e39

File tree

16 files changed

+312
-83
lines changed

16 files changed

+312
-83
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language: ruby
22
rvm:
3-
- 1.9.2
43
- 1.9.3
54
- 2.0.0
65
- 2.1.0
76
- 2.1.1
87
- 2.1.2
8+
- 2.1.3
9+
- 2.1.5

README.md

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ Take a look [here](#generator-syntax) for an explanation about the generator syn
7272
# include AjaxDatatablesRails::Extensions::SimplePaginator
7373

7474
def sortable_columns
75-
# list columns inside the Array in string dot notation.
76-
# Example: 'users.email'
75+
# Declare strings in this format: ModelName.column_name
7776
@sortable_columns ||= []
7877
end
7978

8079
def searchable_columns
81-
# list columns inside the Array in string dot notation.
82-
# Example: 'users.email'
80+
# Declare strings in this format: ModelName.column_name
8381
@searchable_columns ||= []
8482
end
8583
```
@@ -90,23 +88,28 @@ the gems bundled in your project. For example, if your models are using `Kaminar
9088

9189
* For `sortable_columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns.
9290

93-
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
91+
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. Suppose we need to sort and search users `:first_name`, `last_name` and `bio`.
9492

9593
This gives us:
9694

9795
```ruby
9896
include AjaxDatatablesRails::Extensions::Kaminari
9997

10098
def sortable_columns
101-
@sortable_columns ||= ['users.f_name', 'users.l_name', 'users.bio']
99+
@sortable_columns ||= %w(User.first_name User.last_name User.bio)
100+
# this is equal to:
101+
# @sortable_columns ||= ['User.first_name', 'User.last_name', 'User.bio']
102102
end
103103

104104
def searchable_columns
105-
@searchable_columns ||= ['users.f_name', 'users.l_name']
105+
@searchable_columns ||= %w(User.first_name User.last_name User.bio)
106+
# this is equal to:
107+
# @searchable_columns ||= ['User.first_name', 'User.last_name', 'User.bio']
106108
end
107109
```
108110

109-
[See here](#searching-on-non-text-based-columns) for notes regarding database config (if using something different from `postgre`).
111+
* [See here](#searching-on-non-text-based-columns) for notes about the `searchable_columns` settings (if using something different from `postgre`).
112+
* [Read these notes](#searchable-and-sortable-columns-syntax) about considerations for the `searchable_columns` and `sortable_columns` methods.
110113

111114
### Map data
112115
```ruby
@@ -126,8 +129,8 @@ This method builds a 2d array that is used by datatables to construct the html t
126129
def data
127130
records.map do |record|
128131
[
129-
record.f_name,
130-
record.l_name,
132+
record.first_name,
133+
record.last_name,
131134
record.bio
132135
]
133136
end
@@ -174,27 +177,27 @@ We want to sort and search on all columns of the list. The related definition wo
174177

175178
def sortable_columns
176179
@sortable_columns ||= [
177-
'coursetypes.name',
178-
'courses.name',
179-
'events.title',
180-
'events.event_start',
181-
'events.event_end',
182-
'contacts.last_name',
183-
'competency_types.name',
184-
'events.status'
180+
'Coursetype.name',
181+
'Course.name',
182+
'Event.title',
183+
'Event.event_start',
184+
'Event.event_end',
185+
'Contact.last_name',
186+
'CompetencyType.name',
187+
'Event.status'
185188
]
186189
end
187190

188191
def searchable_columns
189192
@searchable_columns ||= [
190-
'coursetypes.name',
191-
'courses.name',
192-
'events.title',
193-
'events.event_start',
194-
'events.event_end',
195-
'contacts.last_name',
196-
'competency_types.name',
197-
'events.status'
193+
'Coursetype.name',
194+
'Course.name',
195+
'Event.title',
196+
'Event.event_start',
197+
'Event.event_end',
198+
'Contact.last_name',
199+
'CompetencyType.name',
200+
'Event.status'
198201
]
199202
end
200203

@@ -230,6 +233,7 @@ So the query using the `.includes()` method is:
230233
end
231234
```
232235

236+
233237
### Controller
234238
Set up the controller to respond to JSON
235239

@@ -244,15 +248,17 @@ end
244248

245249
Don't forget to make sure the proper route has been added to `config/routes.rb`.
246250

251+
247252
### View
253+
248254
* Set up an html `<table>` with a `<thead>` and `<tbody>`
249255
* Add in your table headers if desired
250256
* Don't add any rows to the body of the table, datatables does this automatically
251257
* Add a data attribute to the `<table>` tag with the url of the JSON feed
252258

253259
The resulting view may look like this:
254260

255-
```erb
261+
```html
256262
<table id="users-table", data-source="<%= users_path(format: :json) %>">
257263
<thead>
258264
<tr>
@@ -267,9 +273,11 @@ The resulting view may look like this:
267273
```
268274

269275
### Javascript
270-
Finally, the javascript to tie this all together. In the appropriate `js.coffee` file:
276+
Finally, the javascript to tie this all together. In the appropriate `coffee` file:
271277

272278
```coffeescript
279+
# users.coffee
280+
273281
$ ->
274282
$('#users-table').dataTable
275283
processing: true
@@ -300,6 +308,65 @@ jQuery(document).ready(function() {
300308

301309
### Additional Notes
302310

311+
#### Searchable and Sortable columns syntax
312+
313+
Starting on version `0.3.0`, we are implementing a pseudo code way of declaring the array of both `searchable_columns` and `sortable_columns` method.
314+
315+
Example. Suppose we have the following models: `User`, `PurchaseOrder`, `Purchase::LineItem` and we need to have several columns from those models available in our datatable to search and sort by.
316+
317+
```ruby
318+
# we use the ModelName.column_name notation to declare our columns
319+
320+
def searchable_columns
321+
@searchable_columns ||= [
322+
'User.first_name',
323+
'User.last_name',
324+
'PurchaseOrder.number',
325+
'PurchaseOrder.created_at',
326+
'Purchase::LineItem.quantity',
327+
'Purchase::LineItem.unit_price',
328+
'Purchase::LineItem.item_total'
329+
]
330+
end
331+
332+
def sortable_columns
333+
@sortable_columns ||= [
334+
'User.first_name',
335+
'User.last_name',
336+
'PurchaseOrder.number',
337+
'PurchaseOrder.created_at'
338+
]
339+
end
340+
```
341+
342+
##### What if the datatable itself is namespaced?
343+
Example: what if the datatable is namespaced into an `Admin` module?
344+
345+
```ruby
346+
module Admin
347+
class PurchasesDatatable < AjaxDatatablesRails::Base
348+
end
349+
end
350+
```
351+
352+
Taking the same models and columns, we would define it like this:
353+
354+
```ruby
355+
def searchable_columns
356+
@searchable_columns ||= [
357+
'::User.first_name',
358+
'::User.last_name',
359+
'::PurchaseOrder.number',
360+
'::PurchaseOrder.created_at',
361+
'::Purchase::LineItem.quantity',
362+
'::Purchase::LineItem.unit_price',
363+
'::Purchase::LineItem.item_total'
364+
]
365+
end
366+
```
367+
368+
Pretty much like you would do it, if you were inside a namespaced controller.
369+
303370
#### Searching on non text-based columns
304371

305372
It always comes the time when you need to add a non-string/non-text based column to the `@searchable_columns` array, so you can perform searches against these column types (example: numeric, date, time).

ajax-datatables-rails.gemspec

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@ Gem::Specification.new do |gem|
1111
gem.description = %q{A gem that simplifies using datatables and hundreds of records via ajax}
1212
gem.summary = %q{A wrapper around datatable's ajax methods that allow synchronization with server-side pagination in a rails app}
1313
gem.homepage = ""
14-
gem.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
14+
gem.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
1515

1616
gem.files = Dir["{lib,spec}/**/*", "[A-Z]*"] - ["Gemfile.lock"]
1717
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
1818
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
1919
gem.require_path = "lib"
2020

2121
gem.add_dependency 'railties', '>= 3.1'
22-
22+
2323
gem.add_development_dependency "rspec"
2424
gem.add_development_dependency "generator_spec"
2525
gem.add_development_dependency "pry"
2626
gem.add_development_dependency "rake"
27-
28-
if RUBY_VERSION == '1.9.2'
29-
gem.add_development_dependency "rails", "3.1.0"
30-
else
31-
gem.add_development_dependency "rails", ">= 3.1.0"
32-
end
27+
gem.add_development_dependency "sqlite3"
28+
gem.add_development_dependency "rails", ">= 3.1.0"
29+
gem.add_development_dependency "activerecord", ">= 4.1.6"
3330
end

lib/ajax-datatables-rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'ajax-datatables-rails/version'
22
require 'ajax-datatables-rails/config'
3+
require 'ajax-datatables-rails/models'
34
require 'ajax-datatables-rails/base'
45
require 'ajax-datatables-rails/extensions/simple_paginator'
56
require 'ajax-datatables-rails/extensions/kaminari'

lib/ajax-datatables-rails/base.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ def as_json(options = {})
4646
}
4747
end
4848

49+
def self.deprecated(message, caller = Kernel.caller[1])
50+
warning = caller + ": " + message
51+
52+
if(respond_to?(:logger) && logger.present?)
53+
logger.warn(warning)
54+
else
55+
warn(warning)
56+
end
57+
end
58+
4959
private
5060

5161
def records
@@ -103,6 +113,22 @@ def build_conditions_for(query)
103113
end
104114

105115
def search_condition(column, value)
116+
if column[0] == column.downcase[0]
117+
::AjaxDatatablesRails::Base.deprecated '[DEPRECATED] Using table_name.column_name notation is deprecated. Please refer to: https://github.com/antillas21/ajax-datatables-rails#searchable-and-sortable-columns-syntax'
118+
return deprecated_search_condition(column, value)
119+
else
120+
return new_search_condition(column, value)
121+
end
122+
end
123+
124+
def new_search_condition(column, value)
125+
model, column = column.split('.')
126+
model = model.constantize
127+
casted_column = ::Arel::Nodes::NamedFunction.new('CAST', [model.arel_table[column.to_sym].as(typecast)])
128+
casted_column.matches("%#{value}%")
129+
end
130+
131+
def deprecated_search_condition(column, value)
106132
model, column = column.split('.')
107133
model = model.singularize.titleize.gsub( / /, '' ).constantize
108134

@@ -139,12 +165,24 @@ def per_page
139165
end
140166

141167
def sort_column(item)
168+
new_sort_column(item)
169+
rescue
170+
::AjaxDatatablesRails::Base.deprecated '[DEPRECATED] Using table_name.column_name notation is deprecated. Please refer to: https://github.com/antillas21/ajax-datatables-rails#searchable-and-sortable-columns-syntax'
171+
deprecated_sort_column(item)
172+
end
173+
174+
def deprecated_sort_column(item)
142175
sortable_columns[sortable_displayed_columns.index(item[:column])]
143176
end
144177

178+
def new_sort_column(item)
179+
model, column = sortable_columns[sortable_displayed_columns.index(item[:column])].split('.')
180+
col = [model.constantize.table_name, column].join('.')
181+
end
182+
145183
def sort_direction(item)
146184
options = %w(desc asc)
147-
options.include?(item['dir']) ? item['dir'].upcase : 'ASC'
185+
options.include?(item[:dir]) ? item[:dir].upcase : 'ASC'
148186
end
149187

150188
def sortable_displayed_columns

lib/ajax-datatables-rails/models.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'active_support/ordered_options'
2+
3+
module AjaxDatatablesRails
4+
class Models < ActiveSupport::OrderedOptions
5+
end
6+
end

lib/ajax-datatables-rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AjaxDatatablesRails
2-
VERSION = '0.2.1'
2+
VERSION = '0.3.0'
33
end

lib/generators/rails/templates/datatable.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ class <%= @datatable_name %>Datatable < AjaxDatatablesRails::Base
66
# include AjaxDatatablesRails::Extensions::SimplePaginator
77

88
def sortable_columns
9-
# list columns inside the Array in string dot notation.
10-
# Example: 'users.email'
9+
# Declare strings in this format: ModelName.column_name
1110
@sortable_columns ||= []
1211
end
1312

1413
def searchable_columns
15-
# list columns inside the Array in string dot notation.
16-
# Example: 'users.email'
14+
# Declare strings in this format: ModelName.column_name
1715
@searchable_columns ||= []
1816
end
1917

0 commit comments

Comments
 (0)