flexible_datatables 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a13f848d1e7169a66d108830a47d8289e6476c33
4
+ data.tar.gz: 3d2bfa335369d77870971e5085a5a70bb26171ed
5
+ SHA512:
6
+ metadata.gz: d1dae4b484f9791480a47c4a9ffdb968f074f5c09bf13a9e21a4b9aa347a21a05e479e38b2ddfe2bcd468d54249f4c536fa48d9c1f29d5ab2f3dbd15f3122759
7
+ data.tar.gz: eb9b00b5cf65177cd9f05671a4b51ebe91e9319e68c3ce5cf0f031f0e765486d26cafb827c0550230a67bab8df1f985ef2cb53ede217e7cda5393cfe6c9f720b
data/CHANGELOG ADDED
@@ -0,0 +1,9 @@
1
+ Change Log
2
+ ==========
3
+
4
+ All notable changes to the project are documented in this file.
5
+
6
+ [1.0.0] - 2015-02-14
7
+ --------------------
8
+
9
+ Version 1 release of the library.
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ Change Log
2
+ ==========
3
+
4
+ All notable changes to the project are documented in this file.
5
+
6
+ [1.0.0] - 2015-02-14
7
+ --------------------
8
+
9
+ Version 1 release of the library.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # Flexible Datatables
2
+
3
+ Easily integrate [JQuery Datatables](https://www.datatables.net) (with Ajax) into your Ruby / Rails web applications.
4
+
5
+ ## Introduction
6
+ Flexible Datatables wraps array-like objects (ActiveRecord::Relation collections, etc) that JQuery Datatables can read (via ajax) from your Ruby / Rails web application.
7
+
8
+ ## Features
9
+ * Any arbitrary array-like collection of data, from plain old ruby arrays to complicated ActiveRecord (or Arel, or plain SQL) query results, can be passed on to JQuery Datatables with very little configuration overhead.
10
+ * Server-side pagination with [Kaminari](https://github.com/amatsuda/kaminari) can be used, out of the box.
11
+
12
+ ## Background
13
+ This library was inspired by Ryan Bates' excellent [Railscast episode](http://railscasts.com/episodes/340-datatables) and the [ajax-datatables-rails](https://github.com/antillas21/ajax-datatables-rails) gem.
14
+
15
+ So why not just use [ajax-datatables-rails](https://github.com/antillas21/ajax-datatables-rails)?
16
+
17
+ 1. I wanted to be free to use (abuse) SQL to generate the result set to be passed to DataTables. The [ajax-datatables-rails](https://github.com/antillas21/ajax-datatables-rails) library constructs ActiveRecord queries on the application's behalf, based on user-supplied column names.
18
+ 2. I also wanted to the ability to sort on any kind of column, from integer column types to non-string / non-text based columns, to aggregate functions, and so on.
19
+ 3. Although I usually choose PostgreSQL as my Rails DB backend of choice, I wanted to be able to pass in any kind of array-like collection (or even use a different database backend altogether).
20
+
21
+ ## Installation
22
+ 1. First, make sure you have [DataTables](https://www.datatables.net) installed.
23
+
24
+ You have a few choices here. If you are integrating DataTables with a Rails project, the easiest route is via the [jquery-datatables-rails](https://github.com/rweng/jquery-datatables-rails) gem. Be sure to follow the installation instructions if you choose this route.
25
+
26
+ You can also use [bower](https://github.com/rharriso/bower-rails), or you can even leverage DataTables' [cdn](https://cdn.datatables.net/).
27
+
28
+ 2. Add this line to your application's `Gemfile`:
29
+
30
+ gem 'flexible_datatables'
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install flexible_datatables
39
+ 3. (optional) Create the file `config/initializers/flexible_datatables.rb`. You can use this to override default values (such as the choice of pagination).
40
+ ```ruby
41
+ FlexibleDatatables.configuration do |settings|
42
+ # settings.paginator = FlexibleDatatables::KaminariPaginator
43
+ settings.paginator = FlexibleDatatables::SimplePaginator
44
+ # settings.items_per_page = 10
45
+ end
46
+ ```
47
+
48
+ ## Usage
49
+ ### Rails
50
+ *NOTE*
51
+
52
+ Flexible Datatables isn't required if you wish to use DataTables to transform an html `table` without using ajax (see the first part of the [DataTables RailsCast](http://railscasts.com/episodes/340-datatables?view=asciicast) for reference).
53
+
54
+ The benefit of using Flexible Datatables comes when one wishes to have DataTables tables displaying data from a Rails backend using ajax.
55
+
56
+ #### Controller
57
+ Flexible Datatables requires the `draw`, `start`, `length`, and `order` keys from the `params` hash. Initialize a new `FlexibleDatatables::Datatable` object and pass in those values.
58
+ ```ruby
59
+ datatable = FlexibleDatatables::Datatable.new(params.slice(:draw, :start, :length, :order))
60
+ ```
61
+ Pass in a collection of objects (which can come from anywhere; an array, an ActiveRecord query, etc)
62
+ ```ruby
63
+ datatable.collection = MyModel.all
64
+ ```
65
+ Tell your `Datatable` object what your columns are and how to arrange them into rows.
66
+ ```ruby
67
+ datatable.format_grid(%w(first_column, second_column, third_column) do |record|
68
+ [record.first_column, record.second_column, record.third_column]
69
+ end
70
+ ```
71
+ Instruct Rails to render your `Datatable` object as `JSON`. Your controller may look something like this:
72
+ ```ruby
73
+ respond_to do |format|
74
+ format.html
75
+ format.json do
76
+ datatable = FlexibleDatatables::Datatable.new(params.slice(:draw, :start, :length, :order))
77
+ datatable.collection = MyModel.all
78
+ datatable.format_grid(%w(first_column, second_column, third_column) do |record|
79
+ [record.first_column, record.second_column, record.third_column]
80
+ end
81
+ render json: datatable
82
+ end
83
+ end
84
+ ```
85
+
86
+ #### View
87
+ Define a `table` with an `id` attribute.
88
+ Give the `table` a `data-source` attribute that points to the route that DataTables will use to get the json data over ajax.
89
+ Give this `table` a `thead` with some header columns, and an empty `tbody` declaration.
90
+
91
+ #### JavaScript
92
+ Finally, in a javascript file, call `.DataTable()` and pass in values for the `processing`, `serverSide`, `ajax`, and `pagingType` parameters.
93
+ ```Coffeescript
94
+ $ ->
95
+ $('#my_table_id').DataTable
96
+ processing: true
97
+ serverSide: true
98
+ ajax: $('#my_table_id').data('source')
99
+ pagingType: "full_numbers"
100
+ ```
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create new Pull Request
109
+
110
+ ## License
111
+
112
+ GPL-3.0+
@@ -0,0 +1,16 @@
1
+ module FlexibleDatatables
2
+ def self.configuration
3
+ Configurator.configuration do |config|
4
+ yield(config)
5
+ end
6
+ end
7
+
8
+ def self.settings
9
+ Configurator.settings
10
+ end
11
+ end
12
+
13
+ require 'ostruct'
14
+ require 'flexible_datatables/datatable'
15
+ require 'flexible_datatables/configurator'
16
+ require 'flexible_datatables/paginators'
@@ -0,0 +1,21 @@
1
+ module FlexibleDatatables
2
+ module Configurator
3
+ class Settings
4
+ attr_accessor :items_per_page, :paginator
5
+
6
+ def initialize
7
+ @items_per_page = 10
8
+ @paginator = DummyPaginator
9
+ end
10
+ end
11
+
12
+ def self.configuration
13
+ @@settings ||= Settings.new
14
+ yield(@@settings)
15
+ end
16
+
17
+ def self.settings
18
+ @@settings
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ module FlexibleDatatables
2
+ class Datatable
3
+ attr_accessor :collection, :columns
4
+
5
+ def initialize(args = {})
6
+ @columns = []
7
+ @draw = args.fetch(:draw).to_i
8
+ @grid = []
9
+ @length = args.fetch(:length, Configurator.settings.items_per_page).to_i
10
+ @order = args.fetch(:order, {})
11
+ @paginator = args.fetch(:paginator, Configurator.settings.paginator)
12
+ @start = args.fetch(:start, 0).to_i
13
+ end
14
+
15
+ def as_json(options = {})
16
+ {
17
+ draw: draw,
18
+ recordsTotal: collection.length,
19
+ recordsFiltered: collection.length,
20
+ data: @grid
21
+ }
22
+ end
23
+
24
+ def format_grid(cols)
25
+ @columns = cols unless cols.blank?
26
+ records = collection.order("#{sort_column} #{sort_direction}")
27
+ @grid = paginate(records).map { |record| yield(record) }
28
+ end
29
+
30
+ private
31
+ attr_reader :draw,
32
+ :length,
33
+ :order,
34
+ :paginator,
35
+ :start
36
+
37
+ def sort_column
38
+ @columns[order["0"].fetch(:column).to_i]
39
+ end
40
+
41
+ def sort_direction
42
+ order["0"].fetch(:dir) == "desc" ? "desc" : "asc"
43
+ end
44
+
45
+ def paginate(records)
46
+ paginator.paginate(start: start, length: length, records: records)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ require 'flexible_datatables/paginators/dummy_paginator'
2
+ require 'flexible_datatables/paginators/simple_paginator'
3
+ require 'flexible_datatables/paginators/kaminari_paginator'
@@ -0,0 +1,7 @@
1
+ module FlexibleDatatables
2
+ module DummyPaginator
3
+ def self.paginate(args = Hash.new)
4
+ args.fetch(:records, [])
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module FlexibleDatatables
2
+ module KaminariPaginator
3
+ def self.paginate(args = Hash.new)
4
+ per_page = args.fetch(:length, 0).to_i
5
+ per_page = Configurator.settings.items_per_page unless per_page > 0
6
+ page = args.fetch(:start, 0).to_i / per_page + 1
7
+
8
+ records = args.fetch(:records, [])
9
+ records.page(page).per(per_page)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module FlexibleDatatables
2
+ module SimplePaginator
3
+ def self.paginate(args = Hash.new)
4
+ per_page = args.fetch(:length, 0).to_i
5
+ per_page = Configurator.settings.items_per_page unless per_page > 0
6
+ start = args.fetch(:start, 0).to_i
7
+
8
+ records = args.fetch(:records, [])
9
+ records.slice(start, per_page)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module FlexibleDatatables
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexible_datatables
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dave Corning
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Flexible Datatables wraps array-like objects (ActiveRecord::Relation
14
+ collections, etc) that JQuery Datatables can read (via ajax) from your Ruby / Rails
15
+ web application.
16
+ email: dave@davecorning.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG
22
+ - CHANGELOG.md
23
+ - README.md
24
+ - lib/flexible_datatables.rb
25
+ - lib/flexible_datatables/configurator.rb
26
+ - lib/flexible_datatables/datatable.rb
27
+ - lib/flexible_datatables/paginators.rb
28
+ - lib/flexible_datatables/paginators/dummy_paginator.rb
29
+ - lib/flexible_datatables/paginators/kaminari_paginator.rb
30
+ - lib/flexible_datatables/paginators/simple_paginator.rb
31
+ - lib/flexible_datatables/version.rb
32
+ homepage: https://github.com/davecorning/flexible_datatables
33
+ licenses:
34
+ - GPL-3.0+
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.4.5
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Easily integrate JQuery Datatables (with Ajax) into your Ruby / Rails web
56
+ applications.
57
+ test_files: []