snaptable 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +68 -4
- data/app/assets/javascripts/{autotable.js → snaptable.js} +13 -6
- data/app/assets/stylesheets/{autotable.css.scss → snaptable.css.scss} +1 -1
- data/app/views/base.html.erb +2 -2
- data/app/views/sort.js.erb +2 -1
- data/lib/snaptable/constructor/base_table.rb +6 -18
- data/lib/snaptable/constructor/collection.rb +6 -2
- data/lib/snaptable/constructor/renderer.rb +29 -0
- data/lib/snaptable/helpers/table.rb +0 -21
- data/lib/snaptable/rails/version.rb +1 -1
- data/lib/snaptable/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb7ab05db59cf1a238e3efb6b1d7d091314a0b60
|
4
|
+
data.tar.gz: 97e0fad19495df1c621b997f549dcacdbdc4fe4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6d9e2e50baf967dc1f41172fa698d24aa48d831806f1423518c90bc194a9bd2736cd0f692c18bae243ad1d4370770c2698ddf1289629043a05a402b1d53a8f0
|
7
|
+
data.tar.gz: 8b97555bd23e36319578a1f4aeb3ab983cece15f4b4403f1e260c5c5814eda48230170627eb33fe04aabab8a5b328a9b79244522c24b46f7c0789a4a69979bed
|
data/README.md
CHANGED
@@ -18,6 +18,8 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install snaptable
|
20
20
|
|
21
|
+
The gem requires also jQuery.
|
22
|
+
|
21
23
|
## Usage
|
22
24
|
|
23
25
|
### Basic table
|
@@ -47,6 +49,8 @@ Finally, in your view, generate the table where you wish.
|
|
47
49
|
</div>
|
48
50
|
```
|
49
51
|
|
52
|
+
The elements in the table are clickable. Click on an element and use the links above the table to edit or destroy it. If you double-click, you are directly redirect to the edit page.
|
53
|
+
|
50
54
|
### Options
|
51
55
|
|
52
56
|
You can customize the table when you instanciate it. Pass you own collection in the third argument.
|
@@ -67,18 +71,78 @@ Table.new(self, Article, nil, { search: true, buttons: false })
|
|
67
71
|
|
68
72
|
### Custom class
|
69
73
|
|
70
|
-
|
74
|
+
If you need more control on the displayed fields or on the search, you can easily create your own table.
|
75
|
+
Create a directory `app/tables`. Then create a file `my_model_table.rb`. Inside declare a class `MyModelTable` that inherits from `BaseTable`.
|
76
|
+
You must necessarily write a method called `model` that returns the model to use for your table.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# article_table.rb
|
80
|
+
class ArticleTable < BaseTable
|
81
|
+
|
82
|
+
def model
|
83
|
+
Article
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
From that point, you have a working table, but it acts exactly the same than the basic table. You have few possibilites to change the behavior.
|
90
|
+
If you want to change the table's columns, write a method `attributes` that return an array of the model's attributes you want to display. It supports associations by allowing you to put a hash.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
def attributes
|
94
|
+
[:title, :content, { user: :name }]
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
You can also change how the URL to edit and delete an element is generated. By default it uses the element's id, but you can specify an other attribute. Write a method `url` that returns the attribute.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
def url
|
102
|
+
:slug
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
By default, the search is done on the string fields of the model. If you want to search on the associations, create a module `Search` inside the class. Then declare a method `self.fields` that returns a hash and `self.associations` that returns an array. Be careful, the search is only possible on string fields.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class ArticleTable < BaseTable
|
110
|
+
|
111
|
+
def model
|
112
|
+
Article
|
113
|
+
end
|
114
|
+
|
115
|
+
def attributes
|
116
|
+
[:title, :content, { user: :name }]
|
117
|
+
end
|
118
|
+
|
119
|
+
def url
|
120
|
+
:slug
|
121
|
+
end
|
122
|
+
|
123
|
+
module Search
|
124
|
+
|
125
|
+
def self.associations
|
126
|
+
[:user, :category]
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.fields
|
130
|
+
{ articles: [:title, :content], user: [:name, :email], category: [:name] }
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
```
|
71
135
|
|
72
|
-
###
|
136
|
+
### Permission
|
73
137
|
|
74
138
|
if you want to use a permission system, you can enable it in an initializer.
|
75
139
|
|
76
140
|
```ruby
|
77
|
-
# snaptable.rb
|
141
|
+
# initializers/snaptable.rb
|
78
142
|
Snaptable.use_permission = true
|
79
143
|
```
|
80
144
|
|
81
|
-
When the table fetches the data, it will use `current_permission.records(controller, model, token)`. It is up to you to implement
|
145
|
+
When the table fetches the data, it will use `current_permission.records(controller, model, token)`. It is up to you to implement the class and its method that respond to those three arguments.
|
82
146
|
|
83
147
|
## Contributing
|
84
148
|
|
@@ -1,21 +1,22 @@
|
|
1
1
|
/* Admin table */
|
2
2
|
|
3
|
-
|
3
|
+
function snapifyTable() {
|
4
4
|
|
5
|
-
var
|
5
|
+
var snaptable = $('#snaptable')
|
6
|
+
|
7
|
+
var tableButtons = snaptable.find('.table_buttons'),
|
6
8
|
editButton = tableButtons.find('a[class="edit"]'),
|
7
9
|
deleteButton = tableButtons.find('a[class="delete"]'),
|
8
10
|
path = window.location.pathname + '/';
|
9
|
-
table = $("#admin_table");
|
10
11
|
|
11
12
|
// add ajax to the pagination
|
12
|
-
|
13
|
+
snaptable.on("click", ".pagination a", function() {
|
13
14
|
$.getScript(this.href);
|
14
15
|
return false;
|
15
16
|
});
|
16
17
|
|
17
18
|
// line clickable
|
18
|
-
|
19
|
+
snaptable.on("click", "tbody tr", function(e) {
|
19
20
|
var id = $(this).data('url') ;
|
20
21
|
if ( typeof id !== 'undefined' && !$(this).hasClass('selected') ) {
|
21
22
|
$('tr.selected').removeClass('selected');
|
@@ -27,11 +28,17 @@ $(document).on("ready page:load", function() {
|
|
27
28
|
});
|
28
29
|
|
29
30
|
// Double click
|
30
|
-
|
31
|
+
snaptable.on("dblclick", "tbody tr", function() {
|
31
32
|
var id = $(this).data('url');
|
32
33
|
if ( typeof id !== 'undefined' ) {
|
33
34
|
window.location = path + id + '/edit';
|
34
35
|
}
|
35
36
|
});
|
36
37
|
|
38
|
+
}
|
39
|
+
|
40
|
+
$(document).on("ready page:load", function() {
|
41
|
+
|
42
|
+
snapifyTable();
|
43
|
+
|
37
44
|
});
|
data/app/views/base.html.erb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
<div id="
|
1
|
+
<div id="snaptable">
|
2
2
|
|
3
3
|
<%= render 'search_field' if presenter.options[:search] == true %>
|
4
4
|
<%= render 'buttons' unless presenter.options[:buttons] == false %>
|
@@ -13,7 +13,7 @@
|
|
13
13
|
<tbody>
|
14
14
|
<% if presenter.records.any? %>
|
15
15
|
<% presenter.records.each do |record| %>
|
16
|
-
<tr class="<%= cycle('odd', 'even') %>" data-url="<%= presenter.url
|
16
|
+
<tr class="<%= cycle('odd', 'even') %>" data-url="<%= record.send(presenter.url) %>">
|
17
17
|
<% presenter.values(record).each do |value| %>
|
18
18
|
<td><%= truncate((strip_tags value), length: 40) %></td>
|
19
19
|
<% end %>
|
data/app/views/sort.js.erb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
$('#
|
1
|
+
$('#snaptable').replaceWith('<%= j @table.present %>') ;
|
2
|
+
snapifyTable();
|
@@ -3,7 +3,7 @@ require 'snaptable/constructor/collection'
|
|
3
3
|
require 'snaptable/constructor/renderer'
|
4
4
|
|
5
5
|
module Snaptable
|
6
|
-
module Constructor
|
6
|
+
module Constructor
|
7
7
|
|
8
8
|
class BaseTable < SimpleDelegator
|
9
9
|
include Sortable
|
@@ -21,22 +21,8 @@ module Snaptable
|
|
21
21
|
@options
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
def url(e)
|
29
|
-
e.id
|
30
|
-
end
|
31
|
-
|
32
|
-
def values(element)
|
33
|
-
attributes.map do |attribute|
|
34
|
-
if attribute.is_a? Symbol
|
35
|
-
element.send(attribute)
|
36
|
-
else
|
37
|
-
element.send(*attribute.keys).send(*attribute.values)
|
38
|
-
end.to_s
|
39
|
-
end
|
24
|
+
def url
|
25
|
+
:id
|
40
26
|
end
|
41
27
|
|
42
28
|
private
|
@@ -48,4 +34,6 @@ module Snaptable
|
|
48
34
|
end
|
49
35
|
|
50
36
|
end
|
51
|
-
end
|
37
|
+
end
|
38
|
+
|
39
|
+
BaseTable = Snaptable::Constructor::BaseTable
|
@@ -33,11 +33,15 @@ module Snaptable
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def query_fields
|
36
|
-
self.class
|
36
|
+
if self.class.const_defined?(:Search)
|
37
|
+
self.class::Search.fields
|
38
|
+
else
|
39
|
+
{ model.table_name => model.columns.select{ |c| c.type == :string }.map{ |c| c.name } }
|
40
|
+
end
|
37
41
|
end
|
38
42
|
|
39
43
|
def search_associations
|
40
|
-
self.class::Search.associations
|
44
|
+
self.class::Search.associations if self.class.const_defined?(:Search)
|
41
45
|
end
|
42
46
|
|
43
47
|
def belongs_to_associations
|
@@ -13,6 +13,35 @@ module Snaptable
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
def attributes
|
17
|
+
model.attribute_names
|
18
|
+
end
|
19
|
+
|
20
|
+
def column_names
|
21
|
+
attributes.map do |a|
|
22
|
+
(a.is_a?(Hash) ? a.keys[0] : a).to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def values(element)
|
27
|
+
attributes.map do |attribute|
|
28
|
+
attr_value = if attribute.is_a?(Symbol) || attribute.is_a?(String)
|
29
|
+
element.send(attribute)
|
30
|
+
else
|
31
|
+
element.send(*attribute.keys).send(*attribute.values)
|
32
|
+
end
|
33
|
+
format_if_date(attr_value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def format_if_date(attr_value)
|
38
|
+
if attr_value.is_a?(Date) || attr_value.is_a?(Time) || attr_value.is_a?(DateTime)
|
39
|
+
attr_value.strftime("%d.%m.%y %H:%M")
|
40
|
+
else
|
41
|
+
attr_value
|
42
|
+
end.to_s
|
43
|
+
end
|
44
|
+
|
16
45
|
end
|
17
46
|
end
|
18
47
|
end
|
@@ -10,31 +10,10 @@ module Snaptable
|
|
10
10
|
@model = model
|
11
11
|
end
|
12
12
|
|
13
|
-
def values(element)
|
14
|
-
element.attributes.map do |attr_name, attr_value|
|
15
|
-
if attr_value.is_a?(Date) || attr_value.is_a?(Time) || attr_value.is_a?(DateTime)
|
16
|
-
l(attr_value, format: :short)
|
17
|
-
else
|
18
|
-
attr_value
|
19
|
-
end.to_s
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
13
|
def model
|
24
14
|
@model
|
25
15
|
end
|
26
16
|
|
27
|
-
module Search
|
28
|
-
|
29
|
-
def self.associations
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.fields
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
17
|
end
|
39
18
|
end
|
40
19
|
end
|
data/lib/snaptable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snaptable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khcr
|
@@ -80,8 +80,8 @@ files:
|
|
80
80
|
- LICENSE.txt
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
-
- app/assets/javascripts/
|
84
|
-
- app/assets/stylesheets/
|
83
|
+
- app/assets/javascripts/snaptable.js
|
84
|
+
- app/assets/stylesheets/snaptable.css.scss
|
85
85
|
- app/views/application/_buttons.html.erb
|
86
86
|
- app/views/application/_search_field.html.erb
|
87
87
|
- app/views/base.html.erb
|