Skip to content

Commit 7d3588c

Browse files
author
Joel Quenneville
committed
Merge pull request jbox-web#1 from JoelQ/generators
Generators
2 parents 113ada7 + 2179479 commit 7d3588c

File tree

7 files changed

+125
-16
lines changed

7 files changed

+125
-16
lines changed

ajax-datatables-rails.gemspec

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# -*- encoding: utf-8 -*-
2-
require File.expand_path('../lib/ajax-datatables-rails/version', __FILE__)
2+
require File.expand_path('../lib/ajax-datatables-rails', __FILE__)
33

44
Gem::Specification.new do |gem|
55
gem.authors = ["Joel Quenneville"]
66
gem.email = ["joel.quenneville@collegeplus.org"]
7-
gem.description = %q{TODO: Write a gem description}
8-
gem.summary = %q{TODO: Write a gem summary}
7+
gem.description = %q{A gem that simplifies using datatables and hundreds of records via ajax}
8+
gem.summary = %q{A wrapper around datatable's ajax methods that allow synchronization with server-side pagination in a rails app}
99
gem.homepage = ""
1010

1111
gem.files = `git ls-files`.split($\)
1212
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
1313
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
1414
gem.name = "ajax-datatables-rails"
1515
gem.require_paths = ["lib"]
16-
gem.version = Ajax::Datatables::Rails::VERSION
16+
gem.version = AjaxDatatablesRails::VERSION
17+
18+
gem.add_runtime_dependency 'jquery-datatables-rails', '~> 1.9.1'
1719
end

lib/ajax-datatables-rails.rb

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
1-
require "ajax-datatables-rails/version"
1+
require 'rails'
22

3-
module Ajax
4-
module Datatables
5-
module Rails
6-
# Your code goes here...
3+
class AjaxDatatablesRails
4+
5+
VERSION = '0.0.1'
6+
7+
class << self
8+
9+
def columns(column_array)
10+
@@columns ||= column_array
11+
end
12+
13+
def model_name(model_name)
14+
@@model_name ||= model_name
15+
end
16+
17+
def searchable_columns(columns_array)
18+
@@searchable_columns ||= columns_array
19+
end
20+
21+
end
22+
23+
def initialize(view)
24+
@view = view
25+
end
26+
27+
def method_missing(meth, *args, &block)
28+
@view.send(meth, *args, &block)
29+
end
30+
31+
def as_json(options = {})
32+
{
33+
sEcho: params[:sEcho].to_i,
34+
iTotalRecords: @@model_name.count,
35+
iTotalDisplayRecords: get_raw_records.count,
36+
aaData: data
37+
}
38+
end
39+
40+
private
41+
42+
def get_raw_records
43+
raise
44+
end
45+
46+
def fetch_records
47+
search_records(sort_records(paginate_records(get_raw_records)))
48+
end
49+
50+
def paginate_records(records)
51+
records.page(page).per(per_page)
52+
end
53+
54+
def sort_records(records)
55+
records.order("#{sort_column} #{sort_direction}")
56+
end
57+
58+
def search_records(records)
59+
if params[:sSearch].present?
60+
query = @@searchable_columns.map do |column|
61+
"#{column} LIKE :search"
62+
end.join(" OR ")
63+
records = records.where(query, search: "%#{params[:sSearch]}%")
764
end
65+
return records
66+
end
67+
68+
def page
69+
params[:iDisplayStart].to_i/per_page + 1
70+
end
71+
72+
def per_page
73+
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
74+
end
75+
76+
def sort_column
77+
@@columns[params[:iSortCol_0].to_i]
78+
end
79+
80+
def sort_direction
81+
params[:sSortDir_0] == "desc" ? "DESC" : "ASC"
882
end
983
end

lib/ajax-datatables-rails/version.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/generators/ajaxdatatable/USAGE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Description:
2+
The cancan:ability generator creates an Ability class in the models
3+
directory. You can move this file anywhere you want as long as it
4+
is in the load path.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AjaxdatatableGenerator < Rails::Generators::Base
2+
source_root File.expand_path('../templates', __FILE__)
3+
argument :model, type: :string
4+
5+
def generate_ajaxdatatable
6+
template 'datatable.rb', File.join('app/datatables', "#{model.tableize}_datatable.rb")
7+
end
8+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class <%= model.classify %>Datatable < AjaxDatatableRails
2+
model_name <%= model.classify %>
3+
columns # insert array of column names here
4+
searchable_columns #insert array of columns that will be searched
5+
6+
private
7+
8+
def data
9+
<%= model.tableize %>.map do |<%= model.tableize.singularize %>|
10+
[
11+
# comma separated list of the values for each cell of a table row
12+
]
13+
end
14+
end
15+
16+
def <%= model.tableize %>
17+
@<%= model.tableize %> ||= fetch_records
18+
end
19+
20+
def get_raw_records
21+
# insert query here
22+
end
23+
24+
# ==== Insert 'presenter'-like methods below if necessary
25+
end

lib/generators/datatable_generator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class DatatableGenerator < Rails::Generators::Base
2+
create_file "app/datatables/datatable.rb", "# test"
3+
end

0 commit comments

Comments
 (0)