ajax-datatables-rails-alt-api 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +45 -3
- data/lib/ajax-datatables-rails/alt-api/datatable.rb +4 -21
- data/lib/ajax-datatables-rails/alt-api/datatable/column_def.rb +5 -3
- data/lib/ajax-datatables-rails/alt-api/datatable/row_context.rb +31 -0
- data/lib/ajax-datatables-rails/alt-api/datatable/view_columns.rb +2 -0
- data/lib/ajax-datatables-rails/alt-api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6522d9b2e6ae68225c8473af907a91fc026bbf0c16ba3a39f12beaa01cb1ed2
|
4
|
+
data.tar.gz: ea4a1ac52a37dd2af6ecc4e3ecd8236c0f6797bbe6d03a9c892497726de74a68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5f7b57f278fbb6af63483bdbb6cf167df7577707bd317e88ba3658b73da45821b8ba992515c5565b6fe5914a9ece00d6acd33b8735e62aa833fc45824bed957
|
7
|
+
data.tar.gz: f6cfd7ebaf80da820d37ebf4a2e159791bf4ac6b7b45904eb6ae6a2ec731192fe4f3360eaac3044b2a2238c39abd77a7bb2eee9ec162bad7f8335d1611086e28
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# AjaxDatatablesRails::AltApi
|
2
2
|
|
3
|
-
This is an alternative API to the
|
3
|
+
This is an alternative API to the [ajax-datatables-rails](https://github.com/jbox-web/ajax-datatables-rails) gem. The motivation for this was that we had a lot of datatables written against an older version of ajax-datatables-rails. The newer version of ajax-datatables-rails was incompatible with our older implementation of datatables, so it required a major refactor effort. There were certain things in the recent ajax-datatables-rails API felt redundant, and if a major refactor was needed, a reimagined API started to be developed.
|
4
4
|
|
5
|
-
This uses ajax-datatables-rails under the hood.
|
5
|
+
This uses ajax-datatables-rails under the hood. Ideally, this or something similar may influence future versions of ajax-datatables-rails.
|
6
6
|
|
7
7
|
## Features
|
8
8
|
|
@@ -13,7 +13,7 @@ There are some additional features this API provides.
|
|
13
13
|
* Columns are defined once, with the goal of reducing redundancy.
|
14
14
|
* Debugging mismatch problems with jQuery datatables can be frustrating. There is some code to help debug these tricky situations. There are still many improvements that can be done with this, but it is a start.
|
15
15
|
|
16
|
-
## Usage
|
16
|
+
## Basic Usage
|
17
17
|
|
18
18
|
Inside your application datatable or the individual datatables, include the module.
|
19
19
|
|
@@ -57,6 +57,48 @@ class UserDatatable < ApplicationDatatable
|
|
57
57
|
end
|
58
58
|
```
|
59
59
|
|
60
|
+
### Front-end usage
|
61
|
+
|
62
|
+
Newer versions of jQuery Datatables expects the columns to be defined when the table is initialized. This gem generates that info based on the definition in the datatable.
|
63
|
+
|
64
|
+
```Ruby
|
65
|
+
# users_controller.rb
|
66
|
+
class UsersController < ApplicationController
|
67
|
+
def index
|
68
|
+
@users_datatable = UserDatatable.new(params, view_context: view_context)
|
69
|
+
|
70
|
+
respond_to do |fmt|
|
71
|
+
fmt.html
|
72
|
+
fmt.json do
|
73
|
+
render json: @customer_datatable
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
```ERB
|
81
|
+
// users/index.html.erb
|
82
|
+
|
83
|
+
<table id="users-datatable"
|
84
|
+
data-ajax-url=""
|
85
|
+
data-datatable-columns=<%= @users_datatable.js_columns %>>
|
86
|
+
<th>Name</th>
|
87
|
+
<th>Company</th>
|
88
|
+
<th>Address</th>
|
89
|
+
<th>Last updated</th>
|
90
|
+
<th>links</th>
|
91
|
+
</table>
|
92
|
+
|
93
|
+
<script>
|
94
|
+
$table = $('#users-datatable');
|
95
|
+
$table.DataTable({
|
96
|
+
ajax: $table.data('ajax-url'),
|
97
|
+
columns: $table.data('datatable-columns')
|
98
|
+
})
|
99
|
+
</script>
|
100
|
+
```
|
101
|
+
|
60
102
|
## Development
|
61
103
|
|
62
104
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ajax-datatables-rails/alt-api/datatable/column_def'
|
4
|
+
require 'ajax-datatables-rails/alt-api/datatable/row_context'
|
4
5
|
require 'ajax-datatables-rails/alt-api/datatable/view_columns'
|
5
6
|
|
6
7
|
module AjaxDatatablesRails
|
@@ -117,33 +118,15 @@ module AjaxDatatablesRails
|
|
117
118
|
raise NotImplementedError if columns.empty?
|
118
119
|
|
119
120
|
records_with_error_logging.map do |record|
|
120
|
-
|
121
|
-
delegate_to_view_and_record(record)
|
121
|
+
row_context = RowContext.new(@view, record)
|
122
122
|
|
123
123
|
columns.each_with_object({}) do |col, row|
|
124
|
-
row[col.attr_name] = col.render(
|
124
|
+
row[col.attr_name] = col.render(row_context)
|
125
125
|
row
|
126
126
|
end
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
|
-
def delegate_to_view_and_record(record) # rubocop:disable Lint/MethodLength,Lint/UnusedMethodArgument
|
131
|
-
singleton_class.class_eval do
|
132
|
-
def method_missing(meth, *args)
|
133
|
-
if record.respond_to?(meth)
|
134
|
-
record.send(meth, *args)
|
135
|
-
elsif view.respond_to?(meth)
|
136
|
-
view.send(meth, *args)
|
137
|
-
else
|
138
|
-
super
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def respond_to_missing?(meth, _include_all)
|
143
|
-
record.respond_to?(meth) || view.respond_to?(meth) || super
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
130
|
|
148
131
|
# There are some hard to debug scenarios that come up with ajax-datatables-rails.
|
149
132
|
# This helps debug some problems. Usually the bug is caused by some mismatched
|
@@ -152,7 +135,7 @@ module AjaxDatatablesRails
|
|
152
135
|
@records ||= records
|
153
136
|
rescue NoMethodError => e
|
154
137
|
if e.name == :fetch && e.receiver.nil?
|
155
|
-
Rails.logger.error "#{self.class.name} column problem. view_columns: #{view_columns.pretty_inspect}"
|
138
|
+
Rails.logger.error String.new("#{self.class.name} column problem. view_columns: #{view_columns.pretty_inspect}")
|
156
139
|
end
|
157
140
|
raise e
|
158
141
|
end
|
@@ -61,14 +61,16 @@ module AjaxDatatablesRails
|
|
61
61
|
visible && condition_met?
|
62
62
|
end
|
63
63
|
|
64
|
-
def render(
|
64
|
+
def render(row_context) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
65
65
|
return unless render?
|
66
66
|
|
67
|
+
record = row_context.record
|
68
|
+
|
67
69
|
if cell_renderer
|
68
70
|
if cell_renderer.arity == 1
|
69
|
-
|
71
|
+
row_context.instance_exec(record, &cell_renderer)
|
70
72
|
else
|
71
|
-
|
73
|
+
row_context.instance_exec(&cell_renderer)
|
72
74
|
end
|
73
75
|
elsif record.respond_to?(attr_name)
|
74
76
|
record.send(attr_name)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module AjaxDatatablesRails
|
2
|
+
module AltApi
|
3
|
+
module Datatable
|
4
|
+
# This is used by the column.cell_renderer and seamlessly
|
5
|
+
# delegates to the view or the record
|
6
|
+
class RowContext
|
7
|
+
attr_reader :view, :record
|
8
|
+
|
9
|
+
def initialize(view, record)
|
10
|
+
@view = view
|
11
|
+
@record = record
|
12
|
+
end
|
13
|
+
|
14
|
+
# This allows seamless delegation to @current_record or @view
|
15
|
+
def method_missing(meth, *args)
|
16
|
+
if @record.respond_to?(meth)
|
17
|
+
@record.send(meth, *args)
|
18
|
+
elsif @view.respond_to?(meth)
|
19
|
+
@view.send(meth, *args)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to_missing?(meth, _include_all)
|
26
|
+
@record.respond_to?(meth) || @view.respond_to?(meth) || super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ajax-datatables-rails-alt-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean McCleary
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ajax-datatables-rails
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- bin/setup
|
118
118
|
- lib/ajax-datatables-rails/alt-api/datatable.rb
|
119
119
|
- lib/ajax-datatables-rails/alt-api/datatable/column_def.rb
|
120
|
+
- lib/ajax-datatables-rails/alt-api/datatable/row_context.rb
|
120
121
|
- lib/ajax-datatables-rails/alt-api/datatable/view_columns.rb
|
121
122
|
- lib/ajax-datatables-rails/alt-api/version.rb
|
122
123
|
- lib/ajax-datatables-rails/alt_api.rb
|