snaptable 0.2.1 → 0.3.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 +23 -4
- data/lib/snaptable.rb +7 -1
- data/lib/snaptable/constructor/base_table.rb +1 -1
- data/lib/snaptable/constructor/collection.rb +12 -4
- data/lib/snaptable/helpers/table.rb +11 -0
- data/lib/snaptable/rails/version.rb +1 -1
- data/lib/snaptable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4eb56b243781d4c42a2d7c5b9b2c63057ce3a3db
|
4
|
+
data.tar.gz: 78499e0f5de6a5d9fc83ee6f1dc30d83755b05e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73550f23e475b6e13ec28dec5df99e92993ed3fa6428fca7fe4bf4ac8cf5e66a2bab831866ce2837ff1ddaa0385915211953840ce24093bc8dca7c37c0281d80
|
7
|
+
data.tar.gz: 7aad41b65884219bf7ea6ac661a04820522ca164ebbf22095569d9f0dee9f99170672c4186d38d562e749a6bf6270d9b32f78d209ac55c3e08c1fe33cbf4116d
|
data/README.md
CHANGED
@@ -25,14 +25,18 @@ Or install it yourself as:
|
|
25
25
|
In your controller, instanciate a new `Table` with minimum two arguments: the controller itself and the model to use.
|
26
26
|
|
27
27
|
```ruby
|
28
|
-
|
28
|
+
def index
|
29
|
+
@table = Table.new(self, Article)
|
30
|
+
end
|
29
31
|
```
|
30
32
|
|
31
33
|
Then, in order to enable sorting, call the method `respond` on the table.
|
32
34
|
|
33
35
|
```ruby
|
34
|
-
|
35
|
-
@table.
|
36
|
+
def index
|
37
|
+
@table = Table.new(self, Article)
|
38
|
+
@table.respond
|
39
|
+
end
|
36
40
|
```
|
37
41
|
|
38
42
|
Finally, in your view, generate the table where you wish.
|
@@ -54,13 +58,28 @@ Table.new(self, Article, @articles)
|
|
54
58
|
|
55
59
|
Pass the options in the fourth argument. Here is a list:
|
56
60
|
|
57
|
-
* buttons [true]:
|
61
|
+
* buttons [true]: enable the buttons above the table to add, edit or destroy an element.
|
58
62
|
* search [false]: enable searching. Add a search field above the table.
|
59
63
|
|
60
64
|
```ruby
|
61
65
|
Table.new(self, Article, nil, { search: true, buttons: false })
|
62
66
|
```
|
63
67
|
|
68
|
+
### Custom class
|
69
|
+
|
70
|
+
TODO
|
71
|
+
|
72
|
+
### Permissions
|
73
|
+
|
74
|
+
if you want to use a permission system, you can enable it in an initializer.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# snaptable.rb
|
78
|
+
Snaptable.use_permission = true
|
79
|
+
```
|
80
|
+
|
81
|
+
When the table fetches the data, it will use `current_permission.records(controller, model, token)`. It is up to you to implement a class and its method that respond to those three arguments.
|
82
|
+
|
64
83
|
## Contributing
|
65
84
|
|
66
85
|
1. Fork it ( https://github.com/khcr/snaptable/fork )
|
data/lib/snaptable.rb
CHANGED
@@ -3,7 +3,11 @@ module Snaptable
|
|
3
3
|
module Collection
|
4
4
|
|
5
5
|
def collection
|
6
|
-
@collection ||=
|
6
|
+
@collection ||= if Snaptable.use_permission
|
7
|
+
current_permission.records(params[:controller], model, @token)
|
8
|
+
else
|
9
|
+
model
|
10
|
+
end.includes(belongs_to_associations)
|
7
11
|
end
|
8
12
|
|
9
13
|
def records
|
@@ -12,7 +16,7 @@ module Snaptable
|
|
12
16
|
|
13
17
|
def filter(collection)
|
14
18
|
if options[:search] == true
|
15
|
-
collection.joins(search_associations).where(
|
19
|
+
collection.joins(search_associations).where(query, query: "%#{params[:query]}%", id: params[:query].to_i)
|
16
20
|
else
|
17
21
|
collection
|
18
22
|
end
|
@@ -20,14 +24,18 @@ module Snaptable
|
|
20
24
|
|
21
25
|
private
|
22
26
|
|
23
|
-
def
|
24
|
-
|
27
|
+
def query
|
28
|
+
query_fields.map do |key, values|
|
25
29
|
values.map do |value|
|
26
30
|
values.map{ |v| "#{key}.#{v} LIKE :query OR"}.join(" ")
|
27
31
|
end
|
28
32
|
end.join(" ") + " #{column_name('id')} = :id"
|
29
33
|
end
|
30
34
|
|
35
|
+
def query_fields
|
36
|
+
self.class::Search.fields || { model.table_name => model.columns.select{ |c| c.type == :string }.map{ |c| c.name } }
|
37
|
+
end
|
38
|
+
|
31
39
|
def search_associations
|
32
40
|
self.class::Search.associations
|
33
41
|
end
|
data/lib/snaptable/version.rb
CHANGED