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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a329765ca3e389f8a39ad7df923654c54badf3c
4
- data.tar.gz: b4d83a2d3295dc3cc22945a372500894144203da
3
+ metadata.gz: 4eb56b243781d4c42a2d7c5b9b2c63057ce3a3db
4
+ data.tar.gz: 78499e0f5de6a5d9fc83ee6f1dc30d83755b05e8
5
5
  SHA512:
6
- metadata.gz: e9342e4c99c083163d97f48253a21063ec3fd5b5ffa488d5992f4d189e04503edbb2bf6c873cefe671734ba11a8f8264d09fde359a3fd5b0493ca8a682c57143
7
- data.tar.gz: 157be0ae6d0afd21014f3c83ccb848f3a7f8d065e78ac9af42e33dbcc58a1b2b6d56761a363a8495ec830478839391795e840c196a5373631a1acf46b2565256
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
- @table = Table.new(self, Article)
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
- @table = Table.new(self, Article)
35
- @table.respond
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]: disable the buttons above the table to add, edit or destroy an element.
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 )
@@ -1,3 +1,9 @@
1
1
  require 'snaptable/version'
2
2
  require 'snaptable/railtie' if defined?(Rails)
3
- require 'snaptable/rails'
3
+ require 'snaptable/rails'
4
+
5
+ module Snaptable
6
+ @@use_permission = false
7
+
8
+ mattr_accessor :use_permission
9
+ end
@@ -14,7 +14,7 @@ module Snaptable
14
14
  super(parent)
15
15
  @collection = collection
16
16
  @options = options
17
- @token = stored_token
17
+ @token = stored_token if Snaptable.use_permission
18
18
  end
19
19
 
20
20
  def options
@@ -3,7 +3,11 @@ module Snaptable
3
3
  module Collection
4
4
 
5
5
  def collection
6
- @collection ||= model.includes(belongs_to_associations)
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(query_fields, query: "%#{params[:query]}%", id: params[:query].to_i)
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 query_fields
24
- self.class::Search.fields.map do |key, values|
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
@@ -24,6 +24,17 @@ module Snaptable
24
24
  @model
25
25
  end
26
26
 
27
+ module Search
28
+
29
+ def self.associations
30
+ nil
31
+ end
32
+
33
+ def self.fields
34
+ nil
35
+ end
36
+
37
+ end
27
38
  end
28
39
  end
29
40
  end
@@ -1,5 +1,5 @@
1
1
  module Snaptable
2
2
  module Rails
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module Snaptable
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khcr