ajax-datatables-rails-alt-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +98 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/ajax-datatables-rails-alt-api.gemspec +45 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ajax-datatables-rails/alt-api/datatable.rb +161 -0
- data/lib/ajax-datatables-rails/alt-api/datatable/column_def.rb +115 -0
- data/lib/ajax-datatables-rails/alt-api/datatable/view_columns.rb +17 -0
- data/lib/ajax-datatables-rails/alt-api/version.rb +5 -0
- data/lib/ajax-datatables-rails/alt_api.rb +11 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 633a87bfa8ebe8db19aa7d3a44f1a0f8765f7b8fb3896cc1500f02497a7695ab
|
4
|
+
data.tar.gz: cca2fac25cd0e05b640a294af7b8a7a5adf22fecb36f303753b39d562ab6bc69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fcdd7838a0ee3548681031d4bcd915d596d76694f5494b4414d115993a94df74f541296ac89228f99d081017abbb9e362d00cfbd1451407b7e273c741a1c7e8d
|
7
|
+
data.tar.gz: fef94a972f61f816a420229e44043a9d24f9ee4ce7bacb79974f2a5247a567e4fe732786b2ac8daf7f86c0748624436fe7a3f8e22ce336154af022f75d6b9473
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ajax-datatables-rails-alt-api (0.1.0)
|
5
|
+
ajax-datatables-rails (~> 1.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (5.2.3)
|
11
|
+
actionview (= 5.2.3)
|
12
|
+
activesupport (= 5.2.3)
|
13
|
+
rack (~> 2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
+
actionview (5.2.3)
|
18
|
+
activesupport (= 5.2.3)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
+
activesupport (5.2.3)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (>= 0.7, < 2)
|
26
|
+
minitest (~> 5.1)
|
27
|
+
tzinfo (~> 1.1)
|
28
|
+
ajax-datatables-rails (1.0.0)
|
29
|
+
railties (>= 4.2)
|
30
|
+
builder (3.2.3)
|
31
|
+
byebug (11.0.1)
|
32
|
+
coderay (1.1.2)
|
33
|
+
concurrent-ruby (1.1.5)
|
34
|
+
crass (1.0.4)
|
35
|
+
diff-lcs (1.3)
|
36
|
+
erubi (1.8.0)
|
37
|
+
i18n (1.6.0)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
loofah (2.2.3)
|
40
|
+
crass (~> 1.0.2)
|
41
|
+
nokogiri (>= 1.5.9)
|
42
|
+
method_source (0.9.2)
|
43
|
+
mini_portile2 (2.4.0)
|
44
|
+
minitest (5.11.3)
|
45
|
+
nokogiri (1.10.3)
|
46
|
+
mini_portile2 (~> 2.4.0)
|
47
|
+
pry (0.12.2)
|
48
|
+
coderay (~> 1.1.0)
|
49
|
+
method_source (~> 0.9.0)
|
50
|
+
pry-byebug (3.7.0)
|
51
|
+
byebug (~> 11.0)
|
52
|
+
pry (~> 0.10)
|
53
|
+
rack (2.0.7)
|
54
|
+
rack-test (1.1.0)
|
55
|
+
rack (>= 1.0, < 3)
|
56
|
+
rails-dom-testing (2.0.3)
|
57
|
+
activesupport (>= 4.2.0)
|
58
|
+
nokogiri (>= 1.6)
|
59
|
+
rails-html-sanitizer (1.0.4)
|
60
|
+
loofah (~> 2.2, >= 2.2.2)
|
61
|
+
railties (5.2.3)
|
62
|
+
actionpack (= 5.2.3)
|
63
|
+
activesupport (= 5.2.3)
|
64
|
+
method_source
|
65
|
+
rake (>= 0.8.7)
|
66
|
+
thor (>= 0.19.0, < 2.0)
|
67
|
+
rake (10.5.0)
|
68
|
+
rspec (3.8.0)
|
69
|
+
rspec-core (~> 3.8.0)
|
70
|
+
rspec-expectations (~> 3.8.0)
|
71
|
+
rspec-mocks (~> 3.8.0)
|
72
|
+
rspec-core (3.8.0)
|
73
|
+
rspec-support (~> 3.8.0)
|
74
|
+
rspec-expectations (3.8.3)
|
75
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
76
|
+
rspec-support (~> 3.8.0)
|
77
|
+
rspec-mocks (3.8.0)
|
78
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
79
|
+
rspec-support (~> 3.8.0)
|
80
|
+
rspec-support (3.8.0)
|
81
|
+
thor (0.20.3)
|
82
|
+
thread_safe (0.3.6)
|
83
|
+
tzinfo (1.2.5)
|
84
|
+
thread_safe (~> 0.1)
|
85
|
+
|
86
|
+
PLATFORMS
|
87
|
+
ruby
|
88
|
+
|
89
|
+
DEPENDENCIES
|
90
|
+
ajax-datatables-rails-alt-api!
|
91
|
+
bundler (~> 2.0)
|
92
|
+
pry (~> 0.12)
|
93
|
+
pry-byebug (~> 3)
|
94
|
+
rake (~> 10.0)
|
95
|
+
rspec (~> 3.0)
|
96
|
+
|
97
|
+
BUNDLED WITH
|
98
|
+
2.0.1
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# AjaxDatatablesRails::AltApi
|
2
|
+
|
3
|
+
This is an alternative API to the `ajax-datatables-rails gem`. The motivation for this was that we had a lot of datatables written agains 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 devloped.
|
4
|
+
|
5
|
+
This uses ajax-datatables-rails under the hood. Idealy, this or something similar may influence future versions of ajax-datatables-rails.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
There are some additional features this API provides.
|
10
|
+
|
11
|
+
* Makes it easy to define the column definitions in the client JS. The column definitions are based on the same definitions that you declare in the datatable.
|
12
|
+
* Cells rendering automatically can delegate to the record or view. So the cell rendering blocks can easily access method defined on the view or the record.
|
13
|
+
* Columns are defined once, with the goal of reducing redundancy.
|
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
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
Inside your application datatable or the individual datatables, include the module.
|
19
|
+
|
20
|
+
```Ruby
|
21
|
+
class ApplicationDatatable < AjaxDatatablesRails::ActiveRecord
|
22
|
+
include AjaxDatatablesRails::AltApi
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
Example datatable:
|
27
|
+
|
28
|
+
```Ruby
|
29
|
+
class UserDatatable < ApplicationDatatable
|
30
|
+
base_model 'User'
|
31
|
+
|
32
|
+
# The default behavior is that the column is searchable, sortable, and renders the value.
|
33
|
+
column(:first_name)
|
34
|
+
# Column names can be arbitrary. This example references a relationship
|
35
|
+
# alternatively, the block can be more explicit { |user| user.company.name } or { record.company.name }
|
36
|
+
column(:company_name, source: 'Company.name') { company.name }
|
37
|
+
column(:address, display_only: true) { format_address(address) }
|
38
|
+
|
39
|
+
# This example auto uses both the view's `l` method and `updated_at` (from the user record)
|
40
|
+
column(:updated_at, searchable: false) { l(updated_at, :short) }
|
41
|
+
# In this example, `:links` is not tied to the record. So `display_only` is used so it is not searchable or sortable.
|
42
|
+
# You can call `record` in the block to refer to the record passed to the cell renderer.
|
43
|
+
column(:links, display_only: true) { link_to("Show", user_path(record)) }
|
44
|
+
|
45
|
+
|
46
|
+
# assuming there is an address relationship to the user, you could expose searchable attrs this way
|
47
|
+
search_only_attributes %w[Address.city
|
48
|
+
Address.state_name
|
49
|
+
Address.country
|
50
|
+
Address.country_name
|
51
|
+
Address.postal_code]
|
52
|
+
|
53
|
+
# some method called by a cell renderer block
|
54
|
+
def format_address(address)
|
55
|
+
# makes the address pretty
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
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.
|
63
|
+
|
64
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ajax-datatables-rails-alt-api.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ajax-datatables-rails/alt-api/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ajax-datatables-rails-alt-api"
|
8
|
+
spec.version = AjaxDatatablesRails::AltApi::VERSION
|
9
|
+
spec.authors = ["Sean McCleary"]
|
10
|
+
spec.email = ["seanmcc@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{This is an alternate API to ajax-datatables-rails.}
|
13
|
+
spec.description = <<~DESCRIPTION
|
14
|
+
The goal of this gem is to provide a backwards compatible extension of the
|
15
|
+
ajax-datatables-rails gem that provides an alternate API that reduces
|
16
|
+
duplication.
|
17
|
+
DESCRIPTION
|
18
|
+
spec.homepage = "https://github.com/Shipstr/fleet/#{spec.name}"
|
19
|
+
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
23
|
+
spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/fleet/#{spec.name}/master/CHANGELOG.md"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_dependency "ajax-datatables-rails", "~> 1.0.0"
|
39
|
+
|
40
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
41
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
42
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
43
|
+
spec.add_development_dependency "pry", "~> 0.12"
|
44
|
+
spec.add_development_dependency "pry-byebug", "~> 3"
|
45
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ajax/datatables/rails/alt/api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ajax-datatables-rails/alt-api/datatable/column_def'
|
4
|
+
require 'ajax-datatables-rails/alt-api/datatable/view_columns'
|
5
|
+
|
6
|
+
module AjaxDatatablesRails
|
7
|
+
module AltApi
|
8
|
+
# Just include this in your application datatable
|
9
|
+
# `include 'ajax-datatables-rails/alt-api/datatable'`
|
10
|
+
module Datatable
|
11
|
+
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
13
|
+
|
14
|
+
extend Forwardable
|
15
|
+
|
16
|
+
attr_reader :view, :current_record
|
17
|
+
alias_method :record, :current_record
|
18
|
+
alias_method :r, :record
|
19
|
+
end
|
20
|
+
|
21
|
+
# module class methods
|
22
|
+
module ClassMethods
|
23
|
+
# This is the primary ActiveRecord model the datatable represents.
|
24
|
+
# For example, if you have writing a UserDatatable, and that is
|
25
|
+
# a list of User records, 'User' would be the base model.
|
26
|
+
def base_model(model_class)
|
27
|
+
@base_model = model_class
|
28
|
+
end
|
29
|
+
|
30
|
+
# This adds a ColumnDef to the columns. Order in which column is called
|
31
|
+
# is important. The column order is the same as they will be returned
|
32
|
+
# to the client. To see a full list of options, see the ColumnDef class.
|
33
|
+
def column(*new_column_args, &block)
|
34
|
+
columns << ColumnDef.new(*new_column_args, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def columns
|
38
|
+
@columns ||= []
|
39
|
+
end
|
40
|
+
|
41
|
+
# Use this in the `columns` option in the JS initialization of Datatable.
|
42
|
+
# Call this method in the view.
|
43
|
+
# There are times that when conditionally showing columns, the column
|
44
|
+
# conditionals need access to the datatable instance.
|
45
|
+
# Example:
|
46
|
+
# <table data-datatable-columns="<%= UsersDatatable.js_columns %>">
|
47
|
+
# <th>Name</th>
|
48
|
+
# <th>Email</th>
|
49
|
+
# </table>
|
50
|
+
# <script>
|
51
|
+
# var table = $('#my-datatable');
|
52
|
+
# table.DataTable({
|
53
|
+
# columns: JSON.parse(table.data('datatable-columns'))
|
54
|
+
# })
|
55
|
+
# </script>
|
56
|
+
def js_columns(only: [], exclude: [])
|
57
|
+
cols = if only.present?
|
58
|
+
columns.select { |c| only.include?(c.attr_name) }
|
59
|
+
elsif exclude.present?
|
60
|
+
columns.reject { |c| exclude.include?(c.attr_name) }
|
61
|
+
else
|
62
|
+
columns
|
63
|
+
end
|
64
|
+
cols.reject! { |c| c.attr_name.to_s.starts_with?('search_only') }
|
65
|
+
cols.map(&:as_json).compact.to_json
|
66
|
+
end
|
67
|
+
|
68
|
+
# This is used for tests
|
69
|
+
def column_params
|
70
|
+
new({}).columns.map(&:as_json).each_with_index.reduce({}) do |accum, (h, i)|
|
71
|
+
accum[i] = { **h, search: { value: '', regex: 'false' } }
|
72
|
+
accum
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def search_only_attributes(attrs)
|
77
|
+
attrs.each do |attr|
|
78
|
+
key_name = :"search_only__#{attr.downcase.tr('.', '_')}"
|
79
|
+
columns << ColumnDef.new(key_name, source: attr, search_only: true)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def initialize(params, opts = {})
|
85
|
+
@view = opts[:view_context]
|
86
|
+
super
|
87
|
+
end
|
88
|
+
|
89
|
+
# applies the current datatable instance to the columns
|
90
|
+
def columns
|
91
|
+
@columns ||= self.class.columns.each { |c| c.datatable = self }
|
92
|
+
end
|
93
|
+
|
94
|
+
# There are times that when conditionally showing columns, the column
|
95
|
+
# conditionals need access to the datatable instance.
|
96
|
+
def js_columns(only: [], exclude: [])
|
97
|
+
columns
|
98
|
+
self.class.js_columns(only: only, exclude: exclude)
|
99
|
+
end
|
100
|
+
|
101
|
+
def base_model
|
102
|
+
self.class.instance_variable_get(:@base_model)
|
103
|
+
end
|
104
|
+
|
105
|
+
def view_columns
|
106
|
+
raise NotImplementedError, 'Columns not defined' if columns.empty?
|
107
|
+
|
108
|
+
@view_columns ||= begin
|
109
|
+
columns.each_with_object(ViewColumns.new) do |col, cols|
|
110
|
+
cols[col.attr_name] = col.view_column
|
111
|
+
cols
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def data
|
117
|
+
raise NotImplementedError if columns.empty?
|
118
|
+
|
119
|
+
records_with_error_logging.map do |record|
|
120
|
+
@current_record = record
|
121
|
+
delegate_to_view_and_record(record)
|
122
|
+
|
123
|
+
columns.each_with_object({}) do |col, row|
|
124
|
+
row[col.attr_name] = col.render(record)
|
125
|
+
row
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
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
|
+
|
148
|
+
# There are some hard to debug scenarios that come up with ajax-datatables-rails.
|
149
|
+
# This helps debug some problems. Usually the bug is caused by some mismatched
|
150
|
+
# expectation between what the view_columns are and how it is being used.
|
151
|
+
def records_with_error_logging
|
152
|
+
@records ||= records
|
153
|
+
rescue NoMethodError => e
|
154
|
+
if e.name == :fetch && e.receiver.nil?
|
155
|
+
Rails.logger.error "#{self.class.name} column problem. view_columns: #{view_columns.pretty_inspect}"
|
156
|
+
end
|
157
|
+
raise e
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AjaxDatatablesRails
|
4
|
+
module AltApi
|
5
|
+
module Datatable
|
6
|
+
# This is a datatable column definition
|
7
|
+
class ColumnDef
|
8
|
+
ColumnDefError = Class.new(StandardError)
|
9
|
+
|
10
|
+
attr_reader :attr_name, :source, :sortable, :visible, :searchable,
|
11
|
+
:search_only, :cond, :cell_renderer, :condition
|
12
|
+
attr_accessor :datatable
|
13
|
+
|
14
|
+
def initialize(attr_name, # rubocop:disable Metrics/ParameterLists,Metrics/AbcSize
|
15
|
+
source: nil,
|
16
|
+
sortable: true,
|
17
|
+
visible: true,
|
18
|
+
searchable: true,
|
19
|
+
condition: nil,
|
20
|
+
cond: :like,
|
21
|
+
search_only: false,
|
22
|
+
display_only: false,
|
23
|
+
&cell_renderer)
|
24
|
+
@attr_name = attr_name
|
25
|
+
@source = source
|
26
|
+
if display_only
|
27
|
+
visible = true
|
28
|
+
searchable = false
|
29
|
+
sortable = false
|
30
|
+
end
|
31
|
+
if search_only
|
32
|
+
visible = false
|
33
|
+
searchable = true
|
34
|
+
sortable = false
|
35
|
+
end
|
36
|
+
@condition = condition
|
37
|
+
@visible = visible
|
38
|
+
@sortable = visible ? sortable : false
|
39
|
+
@cell_renderer = cell_renderer
|
40
|
+
@searchable = searchable
|
41
|
+
@search_only = search_only
|
42
|
+
@cond = cond
|
43
|
+
@datatable = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
# Used to serialize options passed to the JS datatable initializer columns
|
47
|
+
def as_json
|
48
|
+
if condition_met?
|
49
|
+
{data: attr_name, visible: visible, sortable: sortable}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def view_column
|
54
|
+
{source: search_source,
|
55
|
+
orderable: to_bool(sortable),
|
56
|
+
searchable: to_bool(searchable),
|
57
|
+
cond: cond}
|
58
|
+
end
|
59
|
+
|
60
|
+
def render?
|
61
|
+
visible && condition_met?
|
62
|
+
end
|
63
|
+
|
64
|
+
def render(record) # rubocop:disable Metrics/AbcSize
|
65
|
+
return unless render?
|
66
|
+
|
67
|
+
if cell_renderer
|
68
|
+
if cell_renderer.arity == 1
|
69
|
+
datatable.instance_exec(record, &cell_renderer)
|
70
|
+
else
|
71
|
+
datatable.instance_exec(&cell_renderer)
|
72
|
+
end
|
73
|
+
elsif record.respond_to?(attr_name)
|
74
|
+
record.send(attr_name)
|
75
|
+
else
|
76
|
+
raise ColumnDefError, "Unable to render #{attr_name} for datatable: #{datatable.class.name}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def search_source
|
83
|
+
base_model = datatable.base_model
|
84
|
+
src = source
|
85
|
+
src ||= "#{base_model}.#{attr_name}" if base_model
|
86
|
+
unless src
|
87
|
+
raise ColumnDefError, "Unable to infer source for column #{attr_name}"
|
88
|
+
end
|
89
|
+
|
90
|
+
src
|
91
|
+
end
|
92
|
+
|
93
|
+
def condition_met?
|
94
|
+
if condition.present? && condition.is_a?(Proc)
|
95
|
+
unless datatable
|
96
|
+
raise ColumnDefError, "`datatable` is nil when evaling condition: #{attr_name}"
|
97
|
+
end
|
98
|
+
|
99
|
+
datatable.instance_exec(&condition)
|
100
|
+
else
|
101
|
+
true
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_bool(val)
|
106
|
+
if val.is_a?(Proc)
|
107
|
+
datatable.instance_exec(&val)
|
108
|
+
else
|
109
|
+
val
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module AjaxDatatablesRails
|
2
|
+
module AltApi
|
3
|
+
module Datatable
|
4
|
+
# DataTables can be really tricky to debug when attributes don't align.
|
5
|
+
# I added this to help a little
|
6
|
+
class ViewColumns < Hash
|
7
|
+
def [](key)
|
8
|
+
super(key).tap do |r|
|
9
|
+
if r.nil?
|
10
|
+
Rails.logger.warn "-- Datatable view column key missing: #{key}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ajax-datatables-rails'
|
4
|
+
require 'ajax-datatables-rails/alt-api/version'
|
5
|
+
require 'ajax-datatables-rails/alt-api/datatable'
|
6
|
+
|
7
|
+
module AjaxDatatablesRails
|
8
|
+
# This is an alternate API for ajax_datatables_rails
|
9
|
+
module AltApi
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ajax-datatables-rails-alt-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean McCleary
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ajax-datatables-rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.12'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3'
|
97
|
+
description: |
|
98
|
+
The goal of this gem is to provide a backwards compatible extension of the
|
99
|
+
ajax-datatables-rails gem that provides an alternate API that reduces
|
100
|
+
duplication.
|
101
|
+
email:
|
102
|
+
- seanmcc@gmail.com
|
103
|
+
executables: []
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rspec"
|
109
|
+
- ".travis.yml"
|
110
|
+
- CHANGELOG.md
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- ajax-datatables-rails-alt-api.gemspec
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- lib/ajax-datatables-rails/alt-api/datatable.rb
|
119
|
+
- lib/ajax-datatables-rails/alt-api/datatable/column_def.rb
|
120
|
+
- lib/ajax-datatables-rails/alt-api/datatable/view_columns.rb
|
121
|
+
- lib/ajax-datatables-rails/alt-api/version.rb
|
122
|
+
- lib/ajax-datatables-rails/alt_api.rb
|
123
|
+
homepage: https://github.com/Shipstr/fleet/ajax-datatables-rails-alt-api
|
124
|
+
licenses: []
|
125
|
+
metadata:
|
126
|
+
homepage_uri: https://github.com/Shipstr/fleet/ajax-datatables-rails-alt-api
|
127
|
+
source_code_uri: https://github.com/Shipstr/fleet/ajax-datatables-rails-alt-api
|
128
|
+
changelog_uri: https://raw.githubusercontent.com/fleet/ajax-datatables-rails-alt-api/master/CHANGELOG.md
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubygems_version: 3.0.1
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: This is an alternate API to ajax-datatables-rails.
|
148
|
+
test_files: []
|