effective_datatables 2.8.0 → 2.9.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 +4 -4
- data/README.md +85 -142
- data/lib/effective_datatables/engine.rb +1 -1
- data/lib/effective_datatables/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9720cc5d9e1023fcb5acaf50e746a9fef4fc23cc
|
4
|
+
data.tar.gz: f7e27f1764ef6c995222f2fdc0a5590bc3ea6071
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea28e65e30b7dd9d5e7f7e3da335bdec3ef4718748598f28d9d1e294b2bc4a1669cd5d4534019de809242b08204339a3f91212a3f176f4d6daf234a5f8572dd
|
7
|
+
data.tar.gz: 5ed6a4f01e6f5f7071730162bbadec00a8cf5cd569034ef80c51640fab482e9644724af3d1ded7d795cf6a8b1d6700b75ce444ef2f4b8b5b3dfa8a6decfa557e
|
data/README.md
CHANGED
@@ -48,33 +48,27 @@ We create a model, initialize it within our controller, then render it from a vi
|
|
48
48
|
|
49
49
|
### The Model
|
50
50
|
|
51
|
-
Start by creating a
|
52
|
-
|
53
|
-
Any `Effective::Datatable` models that exist in this directory will be automatically detected and 'just work'.
|
51
|
+
Start by creating a new datatable.
|
54
52
|
|
55
53
|
Below is a very simple example file, which we will expand upon later.
|
56
54
|
|
57
|
-
This model exists at `/app/
|
55
|
+
This model exists at `/app/datatables/posts_datatable.rb`:
|
58
56
|
|
59
57
|
```ruby
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
actions_column
|
70
|
-
end
|
71
|
-
|
72
|
-
def collection
|
73
|
-
Post.all
|
74
|
-
end
|
58
|
+
class PostsDatatable < Effective::Datatable
|
59
|
+
datatable do
|
60
|
+
table_column :id
|
61
|
+
table_column :user # if Post belongs_to :user
|
62
|
+
table_column :comments # if Post has_many :comments
|
63
|
+
table_column :title
|
64
|
+
table_column :created_at
|
65
|
+
actions_column
|
66
|
+
end
|
75
67
|
|
76
|
-
|
68
|
+
def collection
|
69
|
+
Post.all
|
77
70
|
end
|
71
|
+
|
78
72
|
end
|
79
73
|
```
|
80
74
|
|
@@ -85,7 +79,7 @@ We're going to display this DataTable on the posts#index action
|
|
85
79
|
```ruby
|
86
80
|
class PostsController < ApplicationController
|
87
81
|
def index
|
88
|
-
@datatable =
|
82
|
+
@datatable = PostsDatatable.new
|
89
83
|
end
|
90
84
|
end
|
91
85
|
```
|
@@ -116,48 +110,42 @@ Once your controller and view are set up to render a Datatable, the model is the
|
|
116
110
|
|
117
111
|
This single model file contains just 1 required method and responds to only 3 DSL commands.
|
118
112
|
|
119
|
-
|
120
|
-
|
121
|
-
For example: `/app/models/effective/datatables/posts.rb`:
|
113
|
+
For example: `/app/datatables/posts_datatable.rb`:
|
122
114
|
|
123
115
|
```ruby
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
default_order :created_at, :desc
|
129
|
-
default_entries 25
|
130
|
-
|
131
|
-
table_column :id, :visible => false
|
116
|
+
class PostsDatatable < Effective::Datatable
|
117
|
+
datatable do
|
118
|
+
default_order :created_at, :desc
|
119
|
+
default_entries 25
|
132
120
|
|
133
|
-
|
121
|
+
table_column :id, :visible => false
|
134
122
|
|
135
|
-
|
123
|
+
table_column :created_at, :width => '25%'
|
136
124
|
|
137
|
-
|
125
|
+
table_column :updated_at, :proc => Proc.new { |post| nicetime(post.updated_at) } # just a standard helper as defined in helpers/application_helper.rb
|
138
126
|
|
139
|
-
|
140
|
-
post.post_category.name.titleize
|
141
|
-
end
|
127
|
+
table_column :user
|
142
128
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
content_tag(:li, comment.title)
|
147
|
-
end.join('').html_safe
|
148
|
-
end
|
149
|
-
end
|
129
|
+
table_column :post_category_id, :filter => {:as => :select, :collection => Proc.new { PostCategory.all } } do |post|
|
130
|
+
post.post_category.name.titleize
|
131
|
+
end
|
150
132
|
|
151
|
-
|
152
|
-
|
133
|
+
array_column :comments do |post|
|
134
|
+
content_tag(:ul) do
|
135
|
+
post.comments.where(:archived => false).map do |comment|
|
136
|
+
content_tag(:li, comment.title)
|
137
|
+
end.join('').html_safe
|
153
138
|
end
|
139
|
+
end
|
154
140
|
|
155
|
-
|
156
|
-
|
157
|
-
|
141
|
+
table_column :title, :label => 'Post Title', :class => 'col-title'
|
142
|
+
actions_column
|
143
|
+
end
|
158
144
|
|
159
|
-
|
145
|
+
def collection
|
146
|
+
Post.where(:archived => false).includes(:post_category)
|
160
147
|
end
|
148
|
+
|
161
149
|
end
|
162
150
|
```
|
163
151
|
|
@@ -169,7 +157,7 @@ It can be as simple or as complex as you'd like:
|
|
169
157
|
|
170
158
|
```ruby
|
171
159
|
def collection
|
172
|
-
|
160
|
+
Post.all
|
173
161
|
end
|
174
162
|
```
|
175
163
|
|
@@ -206,27 +194,23 @@ Don't want to use ActiveRecord? Not a problem.
|
|
206
194
|
Define your collection as an Array of Arrays, declare only array_columns, and everything works as expected.
|
207
195
|
|
208
196
|
```ruby
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
array_column :email
|
217
|
-
end
|
218
|
-
|
219
|
-
def collection
|
220
|
-
[
|
221
|
-
[1, 'June', 'Huang', 'june@einstein.com'],
|
222
|
-
[2, 'Leo', 'Stubbs', 'leo@einstein.com'],
|
223
|
-
[3, 'Quincy', 'Pompey', 'quincy@einstein.com'],
|
224
|
-
[4, 'Annie', 'Wojcik', 'annie@einstein.com'],
|
225
|
-
]
|
226
|
-
end
|
197
|
+
class ArrayBackedDatatable < Effective::Datatable
|
198
|
+
datatable do
|
199
|
+
array_column :id
|
200
|
+
array_column :first_name
|
201
|
+
array_column :last_name
|
202
|
+
array_column :email
|
203
|
+
end
|
227
204
|
|
228
|
-
|
205
|
+
def collection
|
206
|
+
[
|
207
|
+
[1, 'June', 'Huang', 'june@einstein.com'],
|
208
|
+
[2, 'Leo', 'Stubbs', 'leo@einstein.com'],
|
209
|
+
[3, 'Quincy', 'Pompey', 'quincy@einstein.com'],
|
210
|
+
[4, 'Annie', 'Wojcik', 'annie@einstein.com'],
|
211
|
+
]
|
229
212
|
end
|
213
|
+
|
230
214
|
end
|
231
215
|
```
|
232
216
|
|
@@ -562,7 +546,7 @@ end
|
|
562
546
|
As well, you need to change the controller where you define the datatable to be aware of the scope params.
|
563
547
|
|
564
548
|
```ruby
|
565
|
-
@datatable =
|
549
|
+
@datatable = PostsDatatable.new(params[:scopes])
|
566
550
|
```
|
567
551
|
|
568
552
|
And to display the scopes anywhere in your view:
|
@@ -741,7 +725,7 @@ In your controller:
|
|
741
725
|
```ruby
|
742
726
|
class PostsController < ApplicationController
|
743
727
|
def index
|
744
|
-
@datatable =
|
728
|
+
@datatable = PostsDatatable.new(:user_id => current_user.try(:id))
|
745
729
|
end
|
746
730
|
end
|
747
731
|
```
|
@@ -749,23 +733,18 @@ end
|
|
749
733
|
And then in your datatable:
|
750
734
|
|
751
735
|
```ruby
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
end
|
759
|
-
end
|
760
|
-
|
761
|
-
def collection
|
762
|
-
if attributes[:user_id]
|
763
|
-
Post.where(user_id: attributes[:user_id])
|
764
|
-
else
|
765
|
-
Post.all
|
766
|
-
end
|
767
|
-
end
|
736
|
+
class PostsDatatable < Effective::Datatable
|
737
|
+
datatable do
|
738
|
+
if attributes[:user_id].blank?
|
739
|
+
table_column :user_id { |post| post.user.email }
|
740
|
+
end
|
741
|
+
end
|
768
742
|
|
743
|
+
def collection
|
744
|
+
if attributes[:user_id]
|
745
|
+
Post.where(user_id: attributes[:user_id])
|
746
|
+
else
|
747
|
+
Post.all
|
769
748
|
end
|
770
749
|
end
|
771
750
|
end
|
@@ -776,28 +755,24 @@ end
|
|
776
755
|
Any non-private methods defined in the datatable model will be available to your table_columns and evaluated in the view_context.
|
777
756
|
|
778
757
|
```ruby
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
end
|
788
|
-
end
|
789
|
-
|
790
|
-
datatable do
|
791
|
-
table_column :title do |post|
|
792
|
-
format_post_title(post)
|
793
|
-
end
|
794
|
-
end
|
758
|
+
class PostsDatatable < Effective::Datatable
|
759
|
+
def format_post_title(post)
|
760
|
+
if post.title.start_with?('important')
|
761
|
+
link_to(post.title.upcase, post_path(post))
|
762
|
+
else
|
763
|
+
link_to(post.title, post_path(post))
|
764
|
+
end
|
765
|
+
end
|
795
766
|
|
796
|
-
|
797
|
-
|
798
|
-
|
767
|
+
datatable do
|
768
|
+
table_column :title do |post|
|
769
|
+
format_post_title(post)
|
799
770
|
end
|
800
771
|
end
|
772
|
+
|
773
|
+
def collection
|
774
|
+
Post.all
|
775
|
+
end
|
801
776
|
end
|
802
777
|
```
|
803
778
|
|
@@ -809,13 +784,8 @@ end
|
|
809
784
|
```
|
810
785
|
|
811
786
|
```ruby
|
812
|
-
|
813
|
-
|
814
|
-
class Posts < Effective::Datatable
|
815
|
-
include PostsHelper
|
816
|
-
|
817
|
-
end
|
818
|
-
end
|
787
|
+
class PostsDatatable < Effective::Datatable
|
788
|
+
include PostsHelper
|
819
789
|
end
|
820
790
|
```
|
821
791
|
|
@@ -928,33 +898,6 @@ rescue_from Effective::AccessDenied do |exception|
|
|
928
898
|
end
|
929
899
|
```
|
930
900
|
|
931
|
-
## Examples
|
932
|
-
|
933
|
-
### Search by a belongs_to objects' field
|
934
|
-
|
935
|
-
In this example, a User belongs_to an Applicant. But instead of using the built in belongs_to functionality and displaying a dropdown of users, instead we want to search by the user's email address:
|
936
|
-
|
937
|
-
```ruby
|
938
|
-
module Effective
|
939
|
-
module Datatables
|
940
|
-
class Applicants < Effective::Datatable
|
941
|
-
datatable do
|
942
|
-
table_column :id, visible: true
|
943
|
-
|
944
|
-
table_column :user, :type => :string, :column => 'users.email' do |applicant|
|
945
|
-
link_to applicant.user.try(:email), edit_admin_user_path(applicant.user)
|
946
|
-
end
|
947
|
-
end
|
948
|
-
|
949
|
-
def collection
|
950
|
-
col = Applicant.joins(:user).includes(:user).references(:user)
|
951
|
-
end
|
952
|
-
end
|
953
|
-
end
|
954
|
-
end
|
955
|
-
```
|
956
|
-
|
957
|
-
|
958
901
|
## License
|
959
902
|
|
960
903
|
MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
|
@@ -2,7 +2,7 @@ module EffectiveDatatables
|
|
2
2
|
class Engine < ::Rails::Engine
|
3
3
|
engine_name 'effective_datatables'
|
4
4
|
|
5
|
-
config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
|
5
|
+
config.autoload_paths += Dir["#{config.root}/app/models/concerns", '/app/datatables/**/']
|
6
6
|
|
7
7
|
# Include Helpers to base application
|
8
8
|
initializer 'effective_datatables.action_controller' do |app|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_datatables
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|